From e18a3b4909665a6d9b0db5f0217e0b626ef9cb89 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 15 Mar 2021 14:37:32 +0100 Subject: Lua, Node.js, Python and Ruby solutions for week 104, part 1 --- challenge-104/abigail/python/ch-1.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 challenge-104/abigail/python/ch-1.py (limited to 'challenge-104/abigail/python') diff --git a/challenge-104/abigail/python/ch-1.py b/challenge-104/abigail/python/ch-1.py new file mode 100644 index 0000000000..8a7dac03e9 --- /dev/null +++ b/challenge-104/abigail/python/ch-1.py @@ -0,0 +1,12 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as python ch-1.py +# + +print ("0 1 1 2 1 3 2 3 1 4 3 5 2 5 3 4 1 5 4 7 3 8 5 7 2 7 " + + "5 8 3 7 4 5 1 6 5 9 4 11 7 10 3 11 8 13 5 12 7 9 2 9") -- cgit From bb79f3de7a0f949a94446679cc2c433c3781605b Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 16 Mar 2021 14:32:30 +0100 Subject: Fix typo --- challenge-104/abigail/python/ch-1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-104/abigail/python') diff --git a/challenge-104/abigail/python/ch-1.py b/challenge-104/abigail/python/ch-1.py index 8a7dac03e9..8ba4c41512 100644 --- a/challenge-104/abigail/python/ch-1.py +++ b/challenge-104/abigail/python/ch-1.py @@ -5,7 +5,7 @@ # # -# Run as python ch-1.py +# Run as: python ch-1.py # print ("0 1 1 2 1 3 2 3 1 4 3 5 2 5 3 4 1 5 4 7 3 8 5 7 2 7 " + -- cgit From 591c536cad2604ffdea1a83891a329030c1cca69 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 16 Mar 2021 20:10:44 +0100 Subject: Python solution for week 104, part 2 --- challenge-104/abigail/python/ch-2.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 challenge-104/abigail/python/ch-2.py (limited to 'challenge-104/abigail/python') diff --git a/challenge-104/abigail/python/ch-2.py b/challenge-104/abigail/python/ch-2.py new file mode 100644 index 0000000000..b3f787dfe8 --- /dev/null +++ b/challenge-104/abigail/python/ch-2.py @@ -0,0 +1,28 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py +# + +tokens = 12 +max_take = 3 + + +while tokens > 0: + prompt = "How many tokens do you take? ({:2d} token{:s} are left) " . \ + format (tokens, "" if tokens == 1 else "s") + take = input (prompt) + if take . isnumeric (): + take = int (take) + if 1 <= take <= max_take: + takes = max_take + 1 - take + print ("Computer takes {:d} token{:s}" . \ + format (takes, "" if takes == 1 else "s")) + tokens = tokens - (max_take + 1) + + +print ("Computer wins") -- cgit From 88f893b7dfa2e8e240a7bc086c8a66215a9174b3 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 18 Mar 2021 02:28:15 +0100 Subject: Alternative Python solution for week 104, part 1 --- challenge-104/abigail/python/ch-1a.py | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 challenge-104/abigail/python/ch-1a.py (limited to 'challenge-104/abigail/python') diff --git a/challenge-104/abigail/python/ch-1a.py b/challenge-104/abigail/python/ch-1a.py new file mode 100644 index 0000000000..6eea537ff9 --- /dev/null +++ b/challenge-104/abigail/python/ch-1a.py @@ -0,0 +1,41 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1a.py +# + +# +# Initialize the cache +# +cache = {0: 0, 1: 1} +max = 50 + +# +# Fusc sequence is defined as: +# ( n, 0 <= n <= 1 +# fusc (n) = { fusc (n / 2), n > 1 && n even +# ( fusc ((n - 1) / 2) + fusc ((n + 1) / 2), n > 1 && n odd +# +def fusc (n): + if n not in cache: + if n % 2 == 1: + cache [n] = fusc ((n - 1) / 2) + fusc ((n + 1) / 2) + else: + cache [n] = fusc (n / 2) + + return cache [n] + + +# +# Calculate the first results of the fucs series, and print them +# +for i in range (max): + if i > 0: + print (' ', end = "") + print (fusc (i), end = "") + +print ("") -- cgit