Ad
  • Default User Avatar

    And now I know about the sorted() function...I love it.

  • Custom User Avatar

    Is this bad practice?

  • Default User Avatar

    It feels like staring at the code a GPU driver.
    Clever.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Same here. And I have been writing JS for more than half a decade now..
    Thanks! :)

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Thats just awesome, great way to learn programming.
    If only my teacher would implement this kind of teaching when i was in school i wouldnt have problems to pass to the next year.

    Props for you!

  • Custom User Avatar

    Certainly not coincidence, he is one of my students ;)

  • Custom User Avatar

    Huh thats interesting,im curious if its coincidente or what?

    Compare your solution to @cerbin:

    Yours

    function cutCancerCells (organism) {
      let types = {
        advance:   (c) => (c == 'C'),
        initial:   (c) => (c == 'c'),
        normal:    (c) => (c == c.toLowerCase()),
        important: (c) => (c == c.toUpperCase()),
        unknown:   (c) => (true)
      }
      let classify = (c) => Object.keys(types).find(key => types[key](c))
      let cells = [...organism].map(c => ({ cell: c, type: classify(c) }))
    
      let mark_resect = (cell) => { if (cell) cell.resect = true }
      let clear_resect = (cell) => { if (cell) cell.resect = false }
      let rules = {
        advance:   (idx) => { mark_resect(cells[idx]), 
                              mark_resect(cells[idx-1]), 
                              mark_resect(cells[idx+1]) },
        initial:   (idx) => { mark_resect(cells[idx]) },
        important: (idx) => { clear_resect(cells[idx]) }
      }
    
      for (let rule in rules)
        cells.forEach((cell, idx) => {if (cell.type == rule) rules[rule](idx)})
      let remaining = cells.filter(cell => !cell.resect)
      return remaining.map(cell => cell.cell).join('')
    }
    

    @cerbin

    function cutCancerCells(organism){
      let types = {
        advance:   (c) => (c == 'C'),
        initial:   (c) => (c == 'c'),
        normal:    (c) => (c == c.toLowerCase()),
        important: (c) => (c == c.toUpperCase()),
        unkown:    (c) => true
    }
      
      let classify = (c) => Object.keys(types).find((key) => types[key](c))
      let cells = [...organism].map(c => ({ cell: c, type: classify(c) }))
      
      let mark_resect = (cell) => { if(cell) cell.resect = true }
      let clear_resect = (cell) => { if(cell) cell.resect = false }
    
      let rules = {
        advance: (index) => { mark_resect(cells[index]),
        mark_resect(cells[index - 1]), mark_resect(cells[index + 1])
        },
        initial: (index) => { mark_resect(cells[index])},
        important: (index) => { clear_resect(cells[index])}
      }
      
      for (let rule in rules)
        cells.forEach((cell, index) => {if(cell.type == rule) rules[rule](index)})
      
      let remaining = cells.filter(cell => !cell.resect)
      console.log(organism)
      console.log(cells)
      console.log(remaining)
      return remaining.map(cell => cell.cell).join('')
      }
    
  • Custom User Avatar

    Try comparing this solution with https://www.codewars.com/kata/reviews/57a3503a7cfe50ebcb00014e/groups/58d061753f25db3d9600003f which is about 30% faster, although it still requires some attention to integer type choices.

  • Custom User Avatar

    And I thought I was the one going for the overkill with a recursive approach...

    Nice usage of C++ though :)