Eternity Async Tutoring

OK, you need to break it down into smaller parts. I’ll get you started:

def find_pair(hand)
  # return rank (as 1-character string) of highest ranked pair in hand
  pair_ranks = find_all_pairs(hand)
  sorted_pair_ranks = pair_ranks.sort_by {|rank| rank_to_number(rank)}
  return sorted_pair_ranks.last
end

def find_all_pairs(hand)
  # returns an empty array for no pairs, otherwise an array with the ranks of the pairs found. cards can't be reused in multiple pairs, so 3 jacks form one pair and 4 jacks form 2 pairs.
  hand_ranks = hand_to_ranks(hand)
  # more code
end

def hand_to_ranks(hand)
  # input is hand array with cards as 2-character strings formatted "RS"  (rank then suit). Output is an array of 1-character strings formatted "R"
  # code
end

def rank_to_number(rank)
  # J = 11, Q = 12, K = 13, A = 14. For now we'll ignore how A can sometimes be 1.
  # code
end

Do you understand how find_pair works above?

As a general technique, you can pretend the sub-functions you need already exist, and just write out how to solve your problem using them. Then go through and create each needed sub-function afterwards.

You can run sub-functions individually and test that they’re working (although find_all_pairs has a sub-sub-function in it, so it can’t be run all by itself). You can also frequently run one or a few lines of code from a function to test the behavior of that part by itself, or use similar code. For example:

Does this make sense and make the problem look approachable for you?

1 Like