SHARE
TWEET

Untitled

a guest Oct 12th, 2017 65 in 8 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. input1 = [
  2.   [3, 2, 3, 0]
  3.   [2, 0, 1, 0]
  4.   [4, 9, 1, 1]
  5.   [0, 2, 1, -44]
  6. ]
  7.  
  8. input2 = [
  9.   [3, 2, 1, 0]
  10.   [2, 0, 9, 0]
  11.   [4, 9, 1, 1]
  12.   [9, 2, 3, -44]
  13. ]
  14.  
  15. input3 = [
  16.   [3, 2, 3, 3]
  17.   [2, 0, 10, 0]
  18.   [4, 9, 1, 1]
  19.   [0, 2, 3, -44]
  20. ]
  21.  
  22. input4 = [
  23.   [3, 2, 1, 0]
  24.   [2, 0, 1, 0]
  25.   [4, 9, 2, 1]
  26.   [0, 2, 2, -44]
  27. ]
  28.  
  29. hasDups = (matrix) ->
  30.   n = matrix[0].length - 1
  31.   dupsMap = {
  32.     hor: null
  33.     ver: null
  34.     diag1: {}
  35.     diag2: {}
  36.   }
  37.  
  38.   for i in [0..n]
  39.     dupsMap.hor = {}
  40.     dupsMap.ver = {}
  41.  
  42.     for j in [0..n]
  43.       horVal = verVal = diag1Val = diag2Val = null
  44.      
  45.       horVal = matrix[i][j]
  46.       verVal = matrix[j][i]
  47.  
  48.       if i == j then diag1Val = matrix[i][j]
  49.       if i + j == n then diag2Val = matrix[i][j]
  50.      
  51.       if dupsMap.hor[horVal]?
  52.         if ++dupsMap.hor[horVal] == 3
  53.           return console.log(i + 1, 'row has 3 dups of ', horVal)
  54.       else
  55.         dupsMap.hor[horVal] = 1
  56.  
  57.       if dupsMap.ver[verVal]?
  58.         if ++dupsMap.ver[verVal] == 3
  59.           return console.log(i + 1, 'col has 3 dups of ', verVal)
  60.       else
  61.         dupsMap.ver[verVal] = 1
  62.        
  63.       if diag1Val?
  64.         if dupsMap.diag1[diag1Val]?
  65.           if ++dupsMap.diag1[diag1Val] == 3
  66.             return console.log('1 diag has 3 dups of ', diag1Val)
  67.         else
  68.           dupsMap.diag1[diag1Val] = 1
  69.  
  70.       if diag2Val?
  71.         if dupsMap.diag2[diag2Val]?
  72.           if ++dupsMap.diag2[diag2Val] == 3
  73.             return console.log('2 diag has 3 dups of ', diag2Val)
  74.         else
  75.           dupsMap.diag2[diag2Val] = 1
  76.  
  77.   console.log('Rows with 3 or more dups have not been found')
  78.  
  79. console.log(hasDups(input1))
  80. console.log(hasDups(input2))
  81. console.log(hasDups(input3))
  82. console.log(hasDups(input4))
RAW Paste Data
Top