Advertisement
Guest User

Abling python test attempt

a guest
Jul 23rd, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. worktext='''Now
  2. is the time
  3. for all good-men
  4. to come to the-
  5. aid of -their
  6. party. NOW IS THE TIME FOR all Good men to
  7. come to the AID of their party. party-their now-is time-the-for all-good
  8. men to -come aid of party a&b%c*(d)
  9. a1b2c3d4-
  10. a
  11.  b
  12. b  c   defg
  13. thisisaverylongWORDwithnospacesbetweenthewordsandhowwillitbehandled?
  14. thisisaverylongWORDwithnospacesbetweenthewordsandhowwillitbehandled2?
  15. thisisaverylongWORDwithnospacesbetweenthewordsandhowwillitbehandled3?
  16.  
  17. !"£$%^&*()''' #I don't actually know file I/O in python yet, sorry
  18.               #Hopefully this isn't a problem! :)
  19.  
  20. worktext=worktext.lower() #Make all letters lowercase before starting
  21.  
  22. currword='' #Hold words in progress
  23. ansmap={} #Using a dict to map 'word':appearancecount
  24.  
  25. for currchar in worktext:
  26.   if currchar.isalpha():
  27.     currword+=currchar
  28.   elif currword: #Checking currword to avoid counting empty words
  29.     try: #Let's not assume currword is in ansmap.
  30.       ansmap[currword]+=1
  31.     except:
  32.       ansmap[currword]=1
  33.     currword='' #remember to reset the current word upon reaching its end!
  34. #Since the for loop only adds words upon reaching a non-letter/word separator,
  35. #if there is a word at the end of the file it will not be counted.
  36. if currword:
  37.   try:
  38.     ansmap[currword]+=1
  39.   except:
  40.     ansmap[currword]=1
  41. #Now ansmap contains all of the words. I could print them out now, but
  42. #they are not in alphabetical order as required by the problem, so I
  43. #will put them in a list first and use the built-in list.sort() method.
  44. wordnumlist=[]
  45. for currword in ansmap:
  46.   wordnumlist+=[(currword,ansmap[currword])]
  47. wordnumlist.sort()
  48. for currtuple in wordnumlist:
  49.   print currtuple[0]+': '+str(currtuple[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement