fork download
  1. from itertools import groupby
  2.  
  3. def sequences(data):
  4. # [1, 3, 4, 6] -> [1], [3, 4], [6]
  5. data = sorted(data)
  6. for _, g in groupby(enumerate(data), lambda x: x[0] - x[1]):
  7. yield [v for _,v in g]
  8.  
  9. def has_straight(cards):
  10. # Straight is a sequence of 5 cards or 4 cards + Ace!
  11. ranks = {card for card in cards}
  12. required = 4 if 1 in ranks else 5
  13. return len(ranks) >= 5 and any(len(x) >= required for x in sequences(ranks))
  14.  
  15. print(has_straight([1, 2, 3, 4, 5]))
  16. print(has_straight([1, 6, 3, 4, 5]))
  17. print(has_straight([3, 4, 5, 6, 7]))
  18.  
  19. print(has_straight([3, 4, 5, 6, 8]))
  20. print(has_straight([1, 10, 3, 4, 5]))
  21. print(has_straight([]))
  22. print(has_straight([1, 2, 3, 4]))
  23. print(has_straight([1, 1, 2, 3]))
  24. print(has_straight([1, 1, 2, 3, 4])) # должно ли тут быть true?
Success #stdin #stdout 0.01s 28384KB
stdin
Standard input is empty
stdout
True
True
True
False
False
False
False
False
False