From 109d3d61baea1ab789f425c052f26fe4d93c112c Mon Sep 17 00:00:00 2001 From: lolabird Date: Sun, 19 Nov 2023 19:13:34 -0700 Subject: [PATCH] Add solution for problem 5 --- thinkcspy/Chapter-12-Exercises/Problem5.py | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 thinkcspy/Chapter-12-Exercises/Problem5.py diff --git a/thinkcspy/Chapter-12-Exercises/Problem5.py b/thinkcspy/Chapter-12-Exercises/Problem5.py new file mode 100644 index 0000000..45f7e05 --- /dev/null +++ b/thinkcspy/Chapter-12-Exercises/Problem5.py @@ -0,0 +1,42 @@ +def translator(english): + + pirate = {} + pirate['sir'] = 'matey' + pirate['hotel'] = 'fleabag inn' + pirate['student'] = 'swabbie' + pirate['boy'] = 'matey' + pirate['restaurant'] = 'galley' + pirate['hello'] = 'avast' + pirate['madam'] = 'proud beauty' + pirate['professor'] = 'foul blaggart' + pirate['your'] = 'yer' + pirate['excuse'] = 'arr' + pirate['are'] = 'be' + pirate['lawyer'] = 'foul blaggart' + pirate['the'] = 'th\'' + pirate['restroom'] = 'head' + pirate['my'] = 'me' + pirate['is'] = 'be' + pirate['man'] = 'matey' + + englishList = english.split() + [" "] + + for i in range(len(englishList) - 1): + word = englishList[i] + word = word.lower() + newWord = "" + + for char in word: + if char.isalpha(): + newWord += char + + if newWord in pirate: + englishList[i] = pirate[newWord] + + translation = " ".join(englishList) + + return translation + + +print(translator(input("What would you like to say?"))) +