id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

peter@norvig.com > segment_words
prev  |  next  |  chance

You know how some web sites have a URL where several words run together? Like "choosespain.com", which is supposed to get you to "Choose Spain" as a travel destination, but unfortunately also has the interpretation "Chooses Pain". In this exercise, write segment_words(text) which will take a string of characters with no spaces and return a list of words, all in the dictionary (given below), that can be concatenated together to form the original string. Words can be any case, as long as their upper-case version is in the dictionary. The result you return should have all the letters in the same case as the input. If there are several possible segmentations into words, always choose the longest possible word first. If there is no segmentation, return None. dictionary = set("""ONE TWO THREE FOUR FIVE SIX HELL HELLO WORLD ROLLED ROLL WELL OXYGEN OXYGENATED OXYGENIZE LOW OTHER MOTHER HER THE THERE WELL ELL TEE TOE OXEN ZOO ZOOM LOX MOXIE TOXIC TOXICOLOGY ZOOLOGY THIS THAT THE CHOOSE SPAIN CHOOSES PAIN WORD""".split())


segment_words('Helloworld') → ['Hello', 'world']
segment_words('zoomother') → ['zoom', 'other']
segment_words('TWOOXENROLLEDBY') → ['TWO', 'OXEN', 'ROLLED', 'BY']

...Save, Compile, Run (ctrl-enter)

def segment_words(text):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Difficulty: 490 Post-solution available

Copyright Nick Parlante 2017 - privacy