From 4f2b0c70d8340837bed2c92c5dfbd816d6c612e8 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 12 Feb 2022 14:40:18 +0100 Subject: Challenge 151 LK Task 2 Python --- challenge-151/lubos-kolouch/python/ch-2.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 challenge-151/lubos-kolouch/python/ch-2.py diff --git a/challenge-151/lubos-kolouch/python/ch-2.py b/challenge-151/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..d71588785d --- /dev/null +++ b/challenge-151/lubos-kolouch/python/ch-2.py @@ -0,0 +1,26 @@ +cache = {} + + +def get_houses_max(houses: list): + """Calculate the best way through the houses""" + + try: + return cache[",".join(map(str, houses))] + except KeyError: + pass + + max_value = 0 + house_index = 0 + + for house_index, _ in enumerate(houses[2:]): + next_houses_values = get_houses_max(houses[2 + house_index :]) + if next_houses_values > max_value: + max_value = next_houses_values + + cache[",".join(map(str, houses))] = houses[0] + max_value + return houses[0] + max_value + + +assert get_houses_max([2, 4, 5]) == 7 +cache = {} +assert get_houses_max([4, 2, 3, 6, 5, 3]) == 13 -- cgit