Advertisement
Guest User

Programming Challenge

a guest
Oct 23rd, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. wordlist = []
  2.  
  3.  
  4. def analyse(text):
  5.     i = 0
  6.     lastword = 0
  7.     for char in text:
  8.         i = i + 1
  9.         if char == " ":
  10.             word = text[lastword: i - 1]
  11.             lastword = i
  12.             wordlist.append(word)
  13.         elif i == len(text):
  14.             word = text[lastword: i]
  15.             lastword = i
  16.             wordlist.append(word)
  17.     return wordlist
  18.  
  19.  
  20.  
  21. def sortlist(list):
  22.     latestword = ''
  23.     amountlist = []
  24.     sortedlist = sorted(list)
  25.     for word in sortedlist:
  26.         i = 1
  27.         if word == latestword:
  28.             amountlist[len(amountlist) - 1] = amountlist[len(amountlist) - 1] + 1
  29.             lastestword = word
  30.         else:
  31.             amountlist.append(i)
  32.             latestword = word
  33.     return amountlist
  34.  
  35.  
  36. def duplicates(list):
  37.     i = 0
  38.     lastword = ''
  39.     finallist = []
  40.     for word in list:
  41.         if word == lastword:
  42.             i = i + 1
  43.         else:
  44.             finallist.append(word)
  45.             lastword = word
  46.     return finallist
  47.  
  48.  
  49. def mostusedwords(numbers, list):
  50.     while len(numbers) > 0:
  51.         index = numbers.index(max(numbers))
  52.         print (list[index] + " times " + str(numbers[index]))
  53.         numbers.remove(numbers[index])
  54.         list.remove(list[index])
  55.  
  56.  
  57. def finalise(string):
  58.     # the number of times each words is entered
  59.     numbersinlist = sortlist(analyse(text))
  60.     #the words according to the above list
  61.     listnodups = duplicates(sorted(analyse(text)))
  62.     return mostusedwords(numbersinlist, listnodups)
  63.  
  64. text = 'she sells sea shells by the sea shore'
  65. finalise(text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement