A problem from Coderbyte. Find the longest word in a sentence after removing the punctuations and if two or more words are of same length return the first word
Below is my solution:
def cleanWord(word):
# To remove punctuations
punctuations = ['!','.','\'','"',',','&']
new_word = ''
for c in word:
if c in punctuations:
continue
else:
new_word += c
return new_word
def LongestWord(sen):
words = sen.split(' ')
word_lengths = {}
# finding length of each word
for word in words:
clean_word = cleanWord(word)
if len(clean_word) not in word_lengths.keys():
word_lengths[len(clean_word)] = word
longest_word_length = max(word_lengths.keys())
longest_word = word_lengths[longest_word_length]
#print(word_lengths)
return longest_word