scubbo

subwords.py

Mar 12th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import urllib2
  2. parent_word=raw_input('What parent word should I use? >> ')
  3. llen=len(parent_word)
  4.  
  5. def index_inc(index_list):
  6. for index in range(len(index_list)):
  7. if index_list[-(index+1)]<llen-index-1:
  8. index_list[-(index+1)]+=1
  9. for i in range(index): #At least one problem is here
  10. index_list[-(index-i)] = index_list[-(index+1)]+i+1
  11. break
  12. return index_list
  13.  
  14. def generate_word(index_list):
  15. ###Given a list of indices, returns the corresponding sub-word
  16. return reduce(lambda x, y: x+y, [parent_word[i] for i in index_list])
  17.  
  18. def find_words(length):
  19. ###This finds all sub-words of the parent word
  20. word_list=[]
  21. index_list=[]
  22. for i in range(length):
  23. index_list.append(i)
  24. while index_list[0] != llen - len(index_list):
  25. word_list.append(generate_word(index_list))
  26. index_list = index_inc(index_list)
  27. return word_list
  28.  
  29. ###Now do stuff
  30.  
  31. print 'Getting word list...'
  32. words=urllib2.urlopen('http://www.puzzlers.org/pub/wordlists/enable1.txt').readlines()
  33. words=[word[:-1] for word in words]
  34.  
  35. word_list=[]
  36. for length in range(llen):
  37. print 'Finding prospective words of length '+str(length+1)
  38. word_list+=find_words(length+1)
  39.  
  40. genuine_word_list=[]
  41.  
  42. print 'We have '+str(len(word_list))+' possible words...'
  43. print 'Testing words for genuineness...'
  44. for word_number in range(len(word_list)):
  45. if word_number % 1000 == 0 and word_number > 0:
  46. print 'Testing word number '+str(word_number)
  47. if word_list[word_number] in words and word_list[word_number] not in genuine_word_list:
  48. genuine_word_list.append(word_list[word_number])
  49. genuine_word_list.append(parent_word)
  50.  
  51. print 'There are '+str(len(genuine_word_list))+' such words:\n'
  52. print genuine_word_list
  53. print '[Press Enter to close]'
  54. wait_to_exit = raw_input()
Advertisement
Add Comment
Please, Sign In to add comment