diff options
| author | 冯昶 <seaker@qq.com> | 2021-04-23 11:23:51 +0800 |
|---|---|---|
| committer | 冯昶 <seaker@qq.com> | 2021-04-23 11:23:51 +0800 |
| commit | 9f5a7a1af0399189b53be44ee94d534606cb8bdd (patch) | |
| tree | cf1e2bfe0ff9e63af11cb3dea9f7a79c1d2b726d /challenge-104/abigail/python | |
| parent | f07721a447942936d7189b4beec3c2acae0c635d (diff) | |
| parent | 9571f967ddd4d11a0195c2b9f8f3fbb63fad9a15 (diff) | |
| download | perlweeklychallenge-club-9f5a7a1af0399189b53be44ee94d534606cb8bdd.tar.gz perlweeklychallenge-club-9f5a7a1af0399189b53be44ee94d534606cb8bdd.tar.bz2 perlweeklychallenge-club-9f5a7a1af0399189b53be44ee94d534606cb8bdd.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-104/abigail/python')
| -rw-r--r-- | challenge-104/abigail/python/ch-1.py | 12 | ||||
| -rw-r--r-- | challenge-104/abigail/python/ch-1a.py | 41 | ||||
| -rw-r--r-- | challenge-104/abigail/python/ch-2.py | 28 |
3 files changed, 81 insertions, 0 deletions
diff --git a/challenge-104/abigail/python/ch-1.py b/challenge-104/abigail/python/ch-1.py new file mode 100644 index 0000000000..8ba4c41512 --- /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") 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 ("") 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") |
