Ejemplo n.º 1
0
  if stop is None:
    stop = len(lexicon)
  if start >= stop:
    return False
  else:
    midIndex = (start + stop)//2
    if lexicon[midIndex].startswith(target):                # found prefix
      return True
    elif target < lexicon[midIndex]:                        # check left side
      return prefixSearch(lexicon, target, start, midIndex)
    else:                                                   # check right side
      return prefixSearch(lexicon, target, midIndex+1, stop)

if __name__ == '__main__':
  from FileUtilities import readWordFile
  lexicon = readWordFile()
  lexicon.sort()

  goal = raw_input('enter a potential word [return to quit]: ')
  while goal:
    if search(lexicon, goal):
      print goal, 'as a word: found'
    else:
      print goal, 'as a word: not found'

    if prefixSearch(lexicon, goal):
      print goal, 'as a prefix: found'
    else:
      print goal, 'as a prefix: not found'

    goal = raw_input('enter a potential word [return to quit]: ')
Ejemplo n.º 2
0
  """
  solutions = []
  if len(charsToUse) > 1:
    for i in range(len(charsToUse)):         # pick charsToUse[i] next
      newPrefix = prefix + charsToUse[i]
      if prefixSearch(lexicon, newPrefix):   # worth exploring
        newCharsToUse = charsToUse[ : i] + charsToUse[i+1 : ]
        solutions.extend(anagrams(lexicon, newCharsToUse, newPrefix))
  else:     # check to see if we have a good solution
    candidate = prefix + charsToUse
    if search(lexicon, candidate):           # use binary search
      solutions.append(candidate)
  return solutions

if __name__ == '__main__':
    from FileUtilities import readWordFile
    print 'Will begin by reading the file of words.'
    words = readWordFile()
    words.sort()

    puzzle = raw_input('enter a string of characters to use: ')
    while puzzle:
        solutions = anagrams(words, puzzle)
        if solutions:
            solutions = list(set(solutions))    # remove duplicates
            solutions.sort()
            print 'Solutions include:'
            print '\n'.join(solutions)

        puzzle = raw_input('enter a string of characters to use: ')