diff --git a/README.md b/README.md index 77cf77b4..9bd45c28 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ We have created starter projects in Java, Scala, and Python that you can use to - [Scala Project](scala/README.md) - [Python Project](python/README.md) -Note: We will be coding in Scala for the Hours with Experts course but for this challenge you can choose any language you like. +Note: We will be coding in Python for the Hours with Experts course and strongly recommend you submit your code in python, but you are welcome to choose any language you would like and we'll take that into account. #### Important When you are ready to start working on your solution, create a new branch called - yournameChallengeThree - ex) nickraffertyChallengeThree diff --git a/python/decoded_recipe.txt b/python/decoded_recipe.txt new file mode 100644 index 00000000..47af286f --- /dev/null +++ b/python/decoded_recipe.txt @@ -0,0 +1,13 @@ +1 cup butter +1 cup dark brown sugar, packed +1 cup granulated sugar +2 eggs +1 teaspoon vanilla +2 1/2 cups oatmeal +2 cups flour +1/2 teaspoon salt +1 teaspoon baking soda +1 teaspoon baking powder +12 ounces chocolate chips +1 4-ounce milk chocolate bar +1 1/2 cups chopped nuts diff --git a/python/secret_recipe_decoder.py b/python/secret_recipe_decoder.py index 813f2f7a..4605897f 100755 --- a/python/secret_recipe_decoder.py +++ b/python/secret_recipe_decoder.py @@ -54,18 +54,58 @@ def __init__(self, amount, description) -> None: def decode_string(str): """Given a string named str, use the Caesar encoding above to return the decoded string.""" # TODO: implement me - return '1 cup' + + decoded_string = '' + + for string in str.split(' '): + decoded_string += ' ' + for letter in string: + if letter in ENCODING.keys(): + for key,value in ENCODING.items(): + if letter in key: + decoded_string += value + else: + decoded_string += letter + + decoded_string = decoded_string.replace('#',' ') + + return decoded_string.strip() def decode_ingredient(line): """Given an ingredient, decode the amount and description, and return a new Ingredient""" # TODO: implement me - return Ingredient("1 cup", "butter") + + if '#' in line: + amount, description = line.split('#') + def parser(recipe_word:str) -> str: + """ Helper function to decode and parse amount and description separately""" + + decoded_line_helper = '' + for word in recipe_word.split(' '): + decoded_line_helper += ' ' + decoded_line_helper += decode_string(word) + + return decoded_line_helper.strip() + + amount_decoded = parser(amount) + description_decoded = parser(description) + + return Ingredient(amount_decoded,description_decoded) + def main(): """A program that decodes a secret recipe""" # TODO: implement me + + with open('secret_recipe.txt','r') as reader: + encoded_recipe = reader.readlines() + + with open('decoded_recipe.txt', 'w') as writer: + for line in encoded_recipe: + writer.write(f'{decode_string(line)}\n') if __name__ == "__main__": + main()