From acbd8f6b2a7471edbe77f9ea08ab82db66e1c304 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 8 Feb 2022 15:12:14 +0100 Subject: Week 151: Python solutions --- challenge-151/abigail/python/ch-1.py | 32 ++++++++++++++++++++++++++++++++ challenge-151/abigail/python/ch-2.py | 18 ++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 challenge-151/abigail/python/ch-1.py create mode 100644 challenge-151/abigail/python/ch-2.py (limited to 'challenge-151/abigail/python') diff --git a/challenge-151/abigail/python/ch-1.py b/challenge-151/abigail/python/ch-1.py new file mode 100644 index 0000000000..12dec88d86 --- /dev/null +++ b/challenge-151/abigail/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/local/bin/python3 + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput + +for line in fileinput . input (): + tree = list (map (lambda row: row . strip () . split (), + line . strip () . split ("|"))) + for d in range (len (tree)): + if (d == len (tree) - 1): + print (d + 1) + break + done = False + for i in range (len (tree [d])): + if tree [d] [i] != "*": + ch1 = 2 * i + ch2 = 2 * i + 1 + if ch1 >= len (tree [d + 1]) or ( + (tree [d + 1] [ch1] == "*" and ( + ch2 >= len (tree [d + 1]) or tree [d + 1] [ch2] == "*"))): + print (d + 1) + done = True + break + if done: + break diff --git a/challenge-151/abigail/python/ch-2.py b/challenge-151/abigail/python/ch-2.py new file mode 100644 index 0000000000..a20a18329b --- /dev/null +++ b/challenge-151/abigail/python/ch-2.py @@ -0,0 +1,18 @@ +#!/usr/local/bin/python3 + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +# + +# +# Run as: python ch-2.py < input-file +# + +import fileinput + +for line in fileinput . input (): + houses = list (map (lambda x: int (x), line . strip () . split ())) + best = [0] * (len (houses) + 2) + for i in range (len (houses) - 1, 1, -1): + best [i] = max (houses [i] + best [i + 2], best [i + 1]) + print (houses [0] + best [2]) -- cgit From c6bb015992c65d4248161ca12c22a1af6dd29711 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 8 Feb 2022 18:33:35 +0100 Subject: Week 151: Make the algorithm for part 2 even simpler. No need for an additional array: add two 0's to the array of valuables, and modify in situ. --- challenge-151/abigail/python/ch-2.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'challenge-151/abigail/python') diff --git a/challenge-151/abigail/python/ch-2.py b/challenge-151/abigail/python/ch-2.py index a20a18329b..52522eff0b 100644 --- a/challenge-151/abigail/python/ch-2.py +++ b/challenge-151/abigail/python/ch-2.py @@ -11,8 +11,9 @@ import fileinput for line in fileinput . input (): - houses = list (map (lambda x: int (x), line . strip () . split ())) - best = [0] * (len (houses) + 2) - for i in range (len (houses) - 1, 1, -1): - best [i] = max (houses [i] + best [i + 2], best [i + 1]) - print (houses [0] + best [2]) + h = list (map (lambda x: int (x), line . strip () . split ())) + h . append (0) + h . append (0) + for i in range (len (h) - 3, 1, -1): + h [i] = max (h [i] + h [i + 2], h [i + 1]) + print (h [0] + h [2]) -- cgit