Royal run hand check
EasyOnline Assessment
Find the best run of consecutive ranks in a hand of playing cards.
Cards are given by rank: 1 is the ace, 2 to 10 are the number cards, 11 is the jack, 12 the queen and 13 the king. Suits do not matter. A run is a set of ranks that follow each other with no gaps, such as 5, 6, 7. A hand may hold several cards of the same rank; a repeated rank still counts only once inside a run.
The ace is flexible: it may be played low, just below the 2, or high, just above the king, and you choose which way once for the whole hand (every ace in the hand plays the same way). Runs do not wrap around, so K, A, 2 is not a run. When the ace is high, Q, K, A is a run of length 3 whose top rank is 14.
Look at both choices for the ace and keep the best run found, where a longer run beats a shorter one and, between runs of equal length, the one with the higher top rank wins.
Input: ranks, the ranks of the cards in the hand, in any order.
Output: a list [length, top]: the length of the best run and its highest rank (an ace played high counts as 14). For an empty hand return [0, 0].
Example 1
- Input:
- [[5,3,7,4,6]]
- Output:
- [5,7]
- Explanation
- With the ace low the hand has the single run
3, 4, 5, 6, 7: length 5, top rank 7. There are no aces, so the ace choice changes nothing. The answer is[5, 7].
Example 2
- Input:
- [[12,1,3,13,2]]
- Output:
- [3,14]
- Explanation
- Playing the ace low gives
1, 2, 3(length 3, top 3). Playing it high gives12, 13, 14(length 3, top 14). The lengths tie, so the higher top rank wins and the answer is[3, 14]. The cards do not wrap into one long run.
Example 3
- Input:
- [[9,11,9,10,11]]
- Output:
- [3,11]
- Explanation
- The repeated 9 and repeated 11 count once each, so the ranks present are 9, 10 and 11: a run of length 3 with top 11. The hand has five cards but only three distinct ranks, so the answer is
[3, 11].
Constraints
0 <= len(ranks) <= 401 <= ranks[i] <= 13