From ffff673956ccd2b4c8749c24f860b130ea70acfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Brand=C3=A3o?= Date: Wed, 1 Jun 2022 21:42:25 -0300 Subject: [PATCH] recursion-and-dp: add winning-hand alternative solution from Isla --- problems/recursion-and-dp/winning_hand.py | 70 +++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/problems/recursion-and-dp/winning_hand.py b/problems/recursion-and-dp/winning_hand.py index bd30df5..0e9d8d9 100644 --- a/problems/recursion-and-dp/winning_hand.py +++ b/problems/recursion-and-dp/winning_hand.py @@ -189,3 +189,73 @@ def test(tiles, expected_answer): test([1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 9], False) test([1, 2, 3, 3, 4, 4, 5, 6, 6, 6, 7, 8, 8, 9], False) print("All tests passed!") + + +""" +Also liked this solution from Isla: +https://gist.github.com/IslaMurtazaev/b9801d7561d27762f8df4eb64394386c +""" + +tiles = [1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5] + + +def can_win(tiles): + counter = dict() + for tile in tiles: + counter[tile] = counter.get(tile, 0) + 1 + + for key, value in counter.items(): + if value < 2: + continue + + counter_copy = counter.copy() + counter_copy[key] -= 2 + if check_triplets(counter_copy): + return True + + return False + + +def check_triplets(counter): + if is_empty(counter): + return True + + counter_copy = counter.copy() + if remove_street(counter_copy) and check_triplets(counter_copy): + return True + + counter_copy = counter.copy() + if remove_set(counter_copy) and check_triplets(counter_copy): + return True + + return False + + +def is_empty(counter): + for value in counter.values(): + if value > 0: + return False + return True + + +def remove_street(counter): + for key in counter.keys(): + has_street = all([counter.get(i, 0) > 0 for i in range(key, key + 3)]) + + if has_street: + for i in range(key, key + 3): + counter[i] -= 1 + return True + + return False + + +def remove_set(counter): + for key, value in counter.items(): + if value >= 3: + counter[key] -= 3 + return True + return False + + +print(can_win(tiles))