brewersfan1976

ch3-ex6.py

Jul 16th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. # This program with count the number of occurrences of each substring in a word
  2. # and display them in descending order
  3.  
  4. from collections import Counter
  5.  
  6. def consecutive_groups(iterable):
  7.     s = tuple(iterable)
  8.    
  9.     for size in range(1, len(s) + 1):
  10.         for index in range(len(s) + 1 - size):
  11.             yield iterable[index:index+size]
  12.      
  13. userInput = raw_input("Please enter a word or * when done: ")
  14. substring_list = []
  15.  
  16. while (userInput != "*"):
  17.       substring_list = substring_list + list(consecutive_groups(userInput))
  18.       userInput = raw_input("Please enter a word or * when done: ")
  19.  
  20. substring_count = Counter(substring_list)
  21. substring_descending_order = substring_count.most_common()
  22.  
  23. print substring_descending_order
Advertisement
Add Comment
Please, Sign In to add comment