Skip to content

Instantly share code, notes, and snippets.

@shinokada
Created May 1, 2014 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinokada/11445426 to your computer and use it in GitHub Desktop.
Save shinokada/11445426 to your computer and use it in GitHub Desktop.
# find number of item which has at least 3 numbers are the same
arr1 = [1, 2, 3, 4]
arr2 = [[2, 2, 2, 4],[1, 4, 3, 4],[1, 2, 1, 4],[1, 2, 3, 1],[2, 1, 3, 4]]
n = 0
arr2.map{|e| e.zip(arr1).collect{| x, y| x==y}}.each do |e|
if e.count(true) >=3
n += 1
end
end
puts n
# Notes
# After zip
#[[[2, 1], [2, 2], [3, 3], [4, 4]], [[1, 1], [4, 2], [3, 3], [4, 4]], [[1, 1], [2, 2], [1, 3], [4, 4]], [[1, 1], [2, 2], [3, 3], [1, 4]], [[2, 1], [1, 2], [3, 3], [4, 4]]]
#
# After collect
# [[false, true, true, true], [true, false, true, true], [true, true, false, true], [true, true, true, false], [false, false, true, true]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment