Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- worktext='''Now
- is the time
- for all good-men
- to come to the-
- aid of -their
- party. NOW IS THE TIME FOR all Good men to
- come to the AID of their party. party-their now-is time-the-for all-good
- men to -come aid of party a&b%c*(d)
- a1b2c3d4-
- a
- b
- b c defg
- thisisaverylongWORDwithnospacesbetweenthewordsandhowwillitbehandled?
- thisisaverylongWORDwithnospacesbetweenthewordsandhowwillitbehandled2?
- thisisaverylongWORDwithnospacesbetweenthewordsandhowwillitbehandled3?
- !"£$%^&*()''' #I don't actually know file I/O in python yet, sorry
- #Hopefully this isn't a problem! :)
- worktext=worktext.lower() #Make all letters lowercase before starting
- currword='' #Hold words in progress
- ansmap={} #Using a dict to map 'word':appearancecount
- for currchar in worktext:
- if currchar.isalpha():
- currword+=currchar
- elif currword: #Checking currword to avoid counting empty words
- try: #Let's not assume currword is in ansmap.
- ansmap[currword]+=1
- except:
- ansmap[currword]=1
- currword='' #remember to reset the current word upon reaching its end!
- #Since the for loop only adds words upon reaching a non-letter/word separator,
- #if there is a word at the end of the file it will not be counted.
- if currword:
- try:
- ansmap[currword]+=1
- except:
- ansmap[currword]=1
- #Now ansmap contains all of the words. I could print them out now, but
- #they are not in alphabetical order as required by the problem, so I
- #will put them in a list first and use the built-in list.sort() method.
- wordnumlist=[]
- for currword in ansmap:
- wordnumlist+=[(currword,ansmap[currword])]
- wordnumlist.sort()
- for currtuple in wordnumlist:
- print currtuple[0]+': '+str(currtuple[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement