diff options
| author | Abigail <abigail@abigail.be> | 2021-03-09 00:16:37 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-03-14 19:59:45 +0100 |
| commit | 6f3ab8c4168aec362fdd50bf6445680bd62cac49 (patch) | |
| tree | 9d684a072758072d5de59d12c9c249181ad0be15 | |
| parent | 30b59df5174d391d0becb21538fe3c2ea298346c (diff) | |
| download | perlweeklychallenge-club-6f3ab8c4168aec362fdd50bf6445680bd62cac49.tar.gz perlweeklychallenge-club-6f3ab8c4168aec362fdd50bf6445680bd62cac49.tar.bz2 perlweeklychallenge-club-6f3ab8c4168aec362fdd50bf6445680bd62cac49.zip | |
Python solution for week 103, part 1
| -rw-r--r-- | challenge-103/abigail/README.md | 2 | ||||
| -rw-r--r-- | challenge-103/abigail/python/ch-1.py | 45 |
2 files changed, 47 insertions, 0 deletions
diff --git a/challenge-103/abigail/README.md b/challenge-103/abigail/README.md index e59555b95d..6c40380e9f 100644 --- a/challenge-103/abigail/README.md +++ b/challenge-103/abigail/README.md @@ -52,7 +52,9 @@ output. * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) * [C](c/ch-1.c) +* [Lua](lua/ch-1.lua) * [Perl](perl/ch-1.pl) +* [Python](python/ch-1.py) ### Blog diff --git a/challenge-103/abigail/python/ch-1.py b/challenge-103/abigail/python/ch-1.py new file mode 100644 index 0000000000..ebe2362239 --- /dev/null +++ b/challenge-103/abigail/python/ch-1.py @@ -0,0 +1,45 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as python ch-1.py < input-file +# + +# +# We're reading years from standard input, one year per line, outputting +# years from the sexagenary cycle [1]. This is slightly more than what +# the challenge ask; the challenge asks to output the heavenly stem [2], +# and the earthly branch [3]. But we also output its Yin/Yang. +# +# [1] https://en.wikipedia.org/wiki/Sexagenary_cycle +# [2] https://en.wikipedia.org/wiki/Heavenly_Stems +# [3] https://en.wikipedia.org/wiki/Earthly_Branches +# + +# +# Each of the cycles have been rotated so the first entry corresponds to +# the year 0 in the Proleptic Gregorian calendar. (We're using the +# convention of having a year 0, as per ISO 8601). +# That way, we can just mod the year with the number of entries, without +# first having to subtract something from the year. +# +# The heavenly stems last for 2 years, so we just duplicate the entries. +# + +yin_yang = ["Yang", "Yin"] +heavenly_stems = ["Metal", "Metal", "Water", "Water", "Wood", "Wood", + "Fire", "Fire", "Earth", "Earth"] +earthly_branches = ["Monkey", "Rooster", "Dog", "Pig", "Rat", "Ox", + "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat"] + + +import fileinput + +for year in fileinput . input (): + year = int (year) + print (yin_yang [year % len (yin_yang)], + heavenly_stems [year % len (heavenly_stems)], + earthly_branches [year % len (earthly_branches)]) |
