Created
December 23, 2020 18:08
-
-
Save aeturrell/1931dc0f09711ad2b05f0862b2abe73b to your computer and use it in GitHub Desktop.
The 12 days of Christmas in code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| presents = { | |
| 1: 'A partridge in a pear tree', | |
| 2: 'Two turtle doves', | |
| 3: 'Three french hens', | |
| 4: 'Four calling birds', | |
| 5: 'Five golden rings', | |
| 6: 'Six geese a-laying', | |
| 7: 'Seven swans a-swimming', | |
| 8: 'Eight maids a-milking', | |
| 9: 'Nine ladies dancing', | |
| 10: 'Ten lords a-leaping', | |
| 11: 'Eleven pipers piping', | |
| 12: 'Twelve drummers drumming', | |
| } | |
| def ordinal(n): return "%d%s" % (n, "tsnrhtdd"[(n//10 % 10 != 1)*(n % 10 < 4) * n % 10 ::4]) | |
| def stem(day): return f'On the {ordinal(day)} day of Christmas, my true love gave to me: ' | |
| def presents_day(day): | |
| return ('and ' + presents[1] + '.' if day <= 1 | |
| else presents[day] + '; ' + presents_day(day-1)).lower() | |
| for day in range(1, 13): | |
| song_line = stem(day) + presents_day(day) | |
| print(song_line.replace(' and ', ' ')) if day == 1 else print(song_line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment