Created
February 10, 2015 18:28
-
-
Save EduardoLopes/53925dd22b26d8e52c06 to your computer and use it in GitHub Desktop.
A program made in python that shows the longest word in a setence
This file contains 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
import re | |
filter_regex = re.compile('[^a-zA-Z\s]+') | |
def string_filter(word): | |
'''(str) > str | |
Remove marks from a string | |
>>> string_filter('hi!') | |
hi | |
>>> string_filter('hey! how are you doing, Mark?') | |
hey how are you doing Mark | |
''' | |
return filter_regex.sub('', word) | |
def longest_word(sentence): | |
'''(str) > int | |
Returns the longest word in a <string></string> | |
>>>longest_word(did you know what parangaricutirimicuaro means?) | |
parangaricutirimicuaro | |
>>>longest_word(I like cake and potato) | |
potato | |
''' | |
words = string_filter(sentence).split() | |
max_letter = 0 | |
for word in words: | |
max_letter = max(len(word), max_letter) | |
for word in words: | |
if max_letter == len(word): | |
return word | |
def print_longest_word(sentence): | |
print('\nthe longest word in the setence\n"'+sentence+'" is:\n', longest_word(sentence)) | |
#parangaricutirimicuaro is the longest word | |
print_longest_word("did you know what parangaricutirimicuaro means?") | |
#potato is the longest word | |
print_longest_word("I like cake and potato!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment