Example #1
0
def binary_insert_sort(tList):
    if not tList:
        return
    for i in range(1, len(tList)):
        t = tList[i]
        idx = search(tList, t, 0, i-1).index
        for j in range(i, idx, -1):
            tList[j] = tList[j-1]
        tList[idx] = t
Example #2
0
def anagrams(lexicon, charsToUse, prefix=''):
  """
  Return a list of anagrams, formed with prefix followed by charsToUse.

  lexicon          a list of words (presumed to be alphabetized)
  charsToUse       a string which represents the characters to be arranged
  prefix           a prefix which is presumed to come before the arrangement
                   of the charsToUse (default empty string)
  """
  solutions = []
  if len(charsToUse) > 1:
    for i in range(len(charsToUse)):         # pick charsToUse[i] next
      newPrefix = prefix + charsToUse[i]
      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
Example #3
0
from BinarySearch import search

input = [1, 2, 3, 4, 4]
print search(input, 1)  # prints True
print search(input, 2)  # prints True
print search(input, 3)  # prints True
print search(input, 4)  # prints True
print search(input, 5)  # prints False
from BinarySearch import search

arr1 = [1, 3, 5, 7, 8, 9]
arr2 = [1, 2, 3, 4, 5, 7, 8, 9]

print(search(arr1, 5, 0, len(arr1)))
print(search(arr1, 6, 0, len(arr1)))
print(search(arr2, 5, 0, len(arr2)))
print(search(arr2, 6, 0, len(arr2)))
Example #5
0
from BinarySearch import search

input = [1,2,3,4,4]
print search(input, 1) # prints True
print search(input, 2) # prints True
print search(input, 3) # prints True
print search(input, 4) # prints True
print search(input, 5) # prints False