Created
February 11, 2020 07:31
-
-
Save seasonedgeek/3ee05f2c619e0a707989a2dcc58d030b to your computer and use it in GitHub Desktop.
Exercism.io/Python: Twelve Days (Practice Mode)
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
# -*- coding: utf-8 -*- | |
from json_database import JsonDatabase | |
"""Adds selected lyrics from the song: 'The Twelve Days of Christmas' | |
to a list. | |
Uses JsonDatabase. See details @: | |
- https://pypi.org/project/json-database/ | |
- https://github.com/OpenJarbas/json_database | |
A Exercism.io/Python practice problem utilizing lists, strings, | |
and text formatting. | |
Platform darwin -- Python 3.8.1, pytest-5.3.5 | |
Plugins: pythonpath-0.7.3 | |
Linter: flake8-3.7.9 (ignore=E133, E501), black-19.10b0 | |
""" | |
def recite(start_verse, end_verse): | |
"""Recites the selected verse(s) of the song. | |
Args: | |
start_verse (int): beginning verse of song. | |
end_verse (int): ending verse of song. | |
Returns: | |
list of string(s). | |
""" | |
# create smart database | |
with JsonDatabase("song", "twelve_gift_days.db") as db: | |
# add verses to the song database | |
for song in [ | |
{ | |
"stanza": 1, | |
"lyrics": "On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree.", | |
}, | |
{ | |
"stanza": 2, | |
"lyrics": "On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree.", | |
}, | |
{ | |
"stanza": 3, | |
"lyrics": "On the third day of Christmas my true love gave to me: three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.", | |
}, | |
{ | |
"stanza": 4, | |
"lyrics": "On the fourth day of Christmas my true love gave to me: four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.", | |
}, | |
{ | |
"stanza": 5, | |
"lyrics": "On the fifth day of Christmas my true love gave to me: five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.", | |
}, | |
{ | |
"stanza": 6, | |
"lyrics": "On the sixth day of Christmas my true love gave to me: six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.", | |
}, | |
{ | |
"stanza": 7, | |
"lyrics": "On the seventh day of Christmas my true love gave to me: seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.", | |
}, | |
{ | |
"stanza": 8, | |
"lyrics": "On the eighth day of Christmas my true love gave to me: eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.", | |
}, | |
{ | |
"stanza": 9, | |
"lyrics": "On the ninth day of Christmas my true love gave to me: nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.", | |
}, | |
{ | |
"stanza": 10, | |
"lyrics": "On the tenth day of Christmas my true love gave to me: ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.", | |
}, | |
{ | |
"stanza": 11, | |
"lyrics": "On the eleventh day of Christmas my true love gave to me: eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.", | |
}, | |
{ | |
"stanza": 12, | |
"lyrics": "On the twelfth day of Christmas my true love gave to me: twelve Drummers Drumming, eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.", | |
}, | |
]: | |
db.add_item(song) | |
# recite single verse or range of verses | |
is_single_verse = start_verse == end_verse | |
song_verses = [] | |
if is_single_verse: | |
# single verse: utilize less-than test | |
end_verse = end_verse + 1 | |
# retrieve value: search in-memory db by key | |
while start_verse < end_verse: | |
verse_values = db.search_by_value("stanza", start_verse) | |
# expecting one verse per search | |
for verse in verse_values: | |
# reformat text of verse for readability | |
lyrics = verse["lyrics"] | |
song_verses.append(lyrics) | |
start_verse = start_verse + 1 | |
else: | |
# multiple verses: utilize less-than-or-equal test | |
# retrieve value: search in-memory db by key | |
while start_verse <= end_verse: | |
verse_values = db.search_by_value("stanza", start_verse) | |
# expecting one verse per search | |
for verse in verse_values: | |
# reformat text of verse for readability | |
lyrics = verse["lyrics"] | |
song_verses.append(lyrics) | |
start_verse = start_verse + 1 | |
return song_verses | |
# if __name__ == "__main__": | |
# replay = recite(2, 2) | |
# print(replay) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment