Skip to content

Instantly share code, notes, and snippets.

@anthonyto
Last active May 12, 2017 18:08
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 anthonyto/ed358bf5e3f170d603b4851b4c33d41c to your computer and use it in GitHub Desktop.
Save anthonyto/ed358bf5e3f170d603b4851b4c33d41c to your computer and use it in GitHub Desktop.

May 10, 2017

def openOrSenior(data)
  data.map { |m| senior?(m) ? 'Senior' : 'Open' }
end

def senior?(arr)
  arr[0] > 54 && arr[1] > 7
end

May 11, 2017

VALID_ARR = [1,2,3,4,5,6,7,8,9]
FAILED    = 'Try again!'

def valid_arr?(arr)
  arr.sort == VALID_ARR
end

def done_or_not(board)
  # check each row
  board.each {|r| return FAILED unless valid_arr?(r) }
  # check each column
  board.transpose.each { |r| return FAILED unless valid_arr?(r) }
  # check each 3x3 grid
  board.each_slice(3) do |g|
    # g.map{|f| f[0..2]}.reduce(:+) here can prob be extracted to another method,
    # but I don't know how to pass in [0..2] as an argument. Maybe use a proc?
    return FAILED unless valid_arr?(g.map{|f| f[0..2]}.reduce(:+))
    return FAILED unless valid_arr?(g.map{|f| f[3..5]}.reduce(:+))
    return FAILED unless valid_arr?(g.map{|f| f[6..8]}.reduce(:+))
  end
  'Finished!'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment