Last active
October 26, 2018 03:09
-
-
Save sadmicrowave/6a1280ce0143d0444ceddc3857fc19da to your computer and use it in GitHub Desktop.
Challenge: 10.25.2018 - Longest Word
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
#!/usr/local/bin/python3 | |
import re, cProfile | |
# --------------------------------- CHALLENGE CONTEXT -------------------------------------- # | |
# Have the function LongestWord(sen) take the sen parameter being passed and return the | |
# largest word in the string. If there are two or more words that are the same length, | |
# return the first word from the string with that length. Ignore punctuation and assume | |
# sen will not be empty. | |
# | |
# Sample Test Cases | |
# Input:"fun&!! time" | |
# Output:"time" | |
# | |
# Input:"I love dogs" | |
# Output:"love" | |
# | |
# --------------------------------- PROCESS WIREFRAME -------------------------------------- # | |
# | |
# 1. Need to find length of each word in sentence | |
# 1a. To accomplish, we break apart the sentence on each space (" ") character; | |
# resulting in an array of words as array elements | |
# 2. Special characters can't count toward word length/count; ergo, special characters | |
# must be removed | |
# 2a. Use regular expression (regex) to remove unwanted special characters from full sentence | |
# 3. Need to determine the longest word in the sentence | |
# 3a. Sort the array by element length (longest to shortest) | |
# 4. Extract the longest word in the array | |
# 4a. At this point, because we have already sorted the array (longest to shortest), the first | |
# element in the array will be the longest word. Simply return the first element. | |
# | |
# --------------------------------- PROPOSED SOLUTION -------------------------------------- # | |
def LongestWord ( sen ) : | |
# Profiler: 320 function calls in 0.002 seconds | |
# | |
# 1. Remove all special characters from entire sentence | |
# 1a. This could be done on "w" for each iterated word; however, this wouldn't be optimal as | |
# we can call it once instead to save gas | |
# 2. Split sentence by space character (' ') to create list/array of each separate word | |
# 3. Sort the list/array based on length of element value (largest to smallest) using lambda function | |
# 4. "[0:1]" select the first in the list | |
return sorted(re.sub(r'[^a-zA-Z0-9 ]', '', sen).split(' '), key=lambda item: (-len(item), item))[0:1] | |
# --------------------------------- ALTERNATE SOLUTIONS ------------------------------------ # | |
def LongestWord_sortrev ( sen ) : | |
# Profiler: 232 function calls in 0.003 seconds | |
# | |
# 1. Remove all special characters from entire sentence | |
# 1a. This could be done on "w" for each iterated word; however, this wouldn't be optimal as | |
# we can call it once instead to save gas | |
# 2. Split sentence by space character (' ') to create list/array of each separate word | |
# 3. Sort the list/array based on length of element value (largest to smallest) | |
# 4. "[0:1]" select the first in the list | |
return sorted(re.sub(r'[^a-zA-Z0-9 ]', '', sen).split(' '), key=len, reverse=True)[0:1] | |
def LongestWord_wordreg ( sen ) : | |
# Profiler: 352 function calls in 0.003 seconds | |
# | |
# 1. Split sentence based on space character (' ') to create list/array of each word | |
# 2. Remove all special characters from each word in sentence, iterated with list comprehension | |
# 3. Sort the list/array based on length of element value (largest to smallest) | |
# 4. "[0:1]" select the first in the list | |
return sorted([ re.sub(r'[^a-zA-Z0-9 ]', '', word) for word in sen.split(' ') ], key=len, reverse=True)[0:1] | |
# ------------------------------------ TEST SCENARIOS -------------------------------------- # | |
# | |
# Test method definition to test LongestWord definition. Pass multiple sentences to | |
# function to test output/logic. | |
def test_LongestWord () : | |
sentences = [ "()this#@ !#@is#!! !@#!#@%%$@#*^my$ sentence sentense!" | |
,"Hello World" | |
,"This is a sentence with multiple spaces." | |
,"My brother has 3 dogs." | |
,"There... was a racoon in my trash can yesterday!" | |
,"The Trash Panda was eating left-over lasagna." | |
] | |
# Iterate over each sentence in the list of sentences to test | |
for sentence in sentences : | |
# Pass the sentence to the LongestWord() function, and format the printed output | |
print( "%s - %s" % ( LongestWord( sentence ), sentence ) ) | |
# --------------------------------- MAIN EXECUTION BLOCK ----------------------------------- # | |
# | |
# Profile the current setup | |
cProfile.run( 'test_LongestWord()' ) | |
# keep this function call here, where "input()" is user input to create custom sentence | |
print( LongestWord( input() ) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment