6 kyu

2D Cellular Neighbourhood - Part 2

142 of 183sgerodes
Description
Loading description...
Algorithms
Data Structures
Arrays
Matrix
  • Please sign in or sign up to leave a comment.
  • trashy_incel Avatar

    Python:

    • test framework is not properly used (missing describe/it)
    • tests suite generates warnings

    fork fixing both

  • azotamiota Avatar

    6 kyu? Hmmm... I spent 2 days to solve this, and still not elegant comparing to other solutions. I solved other 6 kyu katas less than an hour sometimes. Is it only me or do you also think that difficulties need some review?

  • NunoOliveira Avatar

    JavaScript: random tests still contain errors in both neighbourhood types. @rekill has a potential fix below.

  • anter69 Avatar

    Python (at least): empty matrix [] is never tested, only [[]]

    Perhaps updating the description is enough.

  • Jomopipi Avatar

    javascript:

    tests are contradicting with each other... some want the middle included, some don't? also

    
    von_neumann
    [ [ 18, 71, 7, 72, 59, 89, 54, 40, 69, 90 ],
      [ 58, 64, 72, 55, 79, 28, 20, 91, 100, 28 ],
      [ 67, 82, 65, 79, 30, 78, 78, 95, 89, 12 ],
      [ 68, 89, 14, 70, 31, 72, 42, 38, 57, 24 ],
      [ 39, 51, 1, 80, 54, 24, 83, 58, 55, 53 ],
      [ 73, 14, 38, 70, 88, 21, 29, 65, 18, 93 ],
      [ 37, 58, 13, 79, 56, 50, 48, 73, 31, 29 ],
      [ 18, 64, 89, 16, 77, 55, 19, 47, 86, 27 ],
      [ 17, 22, 13, 53, 89, 3, 25, 77, 54, 74 ],
      [ 92, 19, 80, 81, 46, 71, 91, 78, 95, 15 ] ]
      
    Expected: '[24, 78]', instead got: '[24, 31, 42, 78]'
    

    surprised anyone has passed this in JS

  • Len512 Avatar

    Either I have misunderstood this kata entirely, or there is some error in the random tests (I pass all the basic tests, though). For example, for the von Neumann case with coordinates = [3, 7], distance = 1, and

    matrix = 
    [ [ 38, 87, 75, 37, 54, 11, 5, 2, 36, 57 ],
      [ 50, 60, 56, 26, 32, 22, 46, 60, 75, 65 ],
      [ 93, 58, 91, 90, 50, 45, 3, 35, 39, 23 ],
      [ 4, 0, 71, 33, 67, 19, 76, 44, 62, 57 ],
      [ 42, 37, 16, 39, 91, 60, 100, 55, 90, 76 ],
      [ 69, 17, 22, 49, 4, 87, 59, 57, 60, 64 ],
      [ 90, 62, 91, 16, 84, 76, 20, 9, 42, 32 ],
      [ 67, 97, 12, 23, 57, 83, 12, 34, 47, 48 ],
      [ 19, 25, 17, 4, 27, 61, 2, 53, 81, 97 ],
      [ 36, 31, 16, 66, 32, 14, 54, 48, 20, 67 ] ]
    

    I get this:

    Expected: '[35]', instead got: '[35, 55, 62, 76]'
    
  • rekill Avatar

    Bug in tests?

    matrix = [ [ 100, 25, 59, 35, 22, 11, 42, 75, 99, 7 ], [ 35, 61, 76, 74, 72, 45, 31, 7, 70, 82 ], [ 46, 37, 30, 55, 14, 51, 18, 44, 6, 35 ], [ 41, 2, 34, 34, 43, 56, 13, 71, 44, 17 ], [ 1, 8, 61, 35, 19, 16, 66, 25, 4, 39 ], [ 9, 71, 49, 11, 49, 53, 50, 81, 86, 62 ], [ 97, 19, 87, 16, 15, 37, 95, 38, 37, 21 ], [ 2, 55, 49, 96, 88, 21, 79, 76, 60, 54 ], [ 75, 86, 98, 92, 91, 73, 75, 77, 100, 20 ], [ 96, 82, 49, 5, 49, 81, 60, 60, 51, 40 ] ];

    get_neighbourhood("von_neumann", matrix, [ 5, 8 ], 1);

    Expected: '[4]', instead got: '[37, 4, 62, 81]'

    Why 4?

  • Voile Avatar

    This also goes to every kata in the series: neighbourhood type only makes sense for distance 1, for every other distance the only notation that makes sense is "metric norm", aka how distance is counted.

    In the case of Moore and Von Neumann metric, the former evaluates to the maximum distance along one dimension, while the latter evaluates to the sum of the distances along each dimension.

    (Alternatively, they are just L_infinity and L_1 metric respectively.)

    (Also alternatively, metric can be extended to other forms like L_2 (Pythagorean metric).)

  • Voile Avatar

    This goes to every kata in the series: You did not mention what are the order of the coordinates.

    e.g in these two 2D katas, you only said m,n in the table, but what does the coordinate mean? (m,n) or (n,m)? Does the coordinate (a,b,c,...) mean mat[a][b][c]..., or mat...[c][b][a]?

    It also doesn't help that all your examples have the same m and n.

  • FArekkusu Avatar

    Moore neighborhood != manhattan distance. Don't mix up different things.

    And, of course, cells can't be called neighbors if the distance between them is > 1:

    X | X | X | X | X         + - center
    -----------------         O - neighbor
    X | O | O | O | X         X - not a neighbor
    -----------------
    X | O | + | O | X
    -----------------
    X | O | O | O | X
    -----------------
    X | X | X | X | X
    
  • ZED.CWT Avatar

    Sample tests expect function name to be get_neighborhood instead of get_neighbourhood