#559 · Dictionaries and sets · Beginner · 30 XP · about 5 min
Write translate(sentence, words) that translates a sentence one word at a time. words is a dictionary that maps some words to their replacements. Each word of the sentence that is a key in words is replaced, and the other words stay as they are.
The words in sentence are lowercase and separated by single spaces, and the result uses single spaces too. Only whole words are replaced: a word that merely starts with a key stays as it is.
pirate = {'hello': 'ahoy', 'my': 'me', 'friend': 'matey', 'yes': 'aye'} translate('hello my friend', pirate) # returns 'ahoy me matey' translate('yes we sail at dawn', pirate) # returns 'aye we sail at dawn' translate('a friendly wave', pirate) # returns 'a friendly wave'
First solves only, ranked by stars and then time.