def interlock(word_list, word): """Checks whether a word contains two interleaved words. word_list: list of strings word: string """ evens = word[::2] odds = word[1::2] return in_bisect(word_list, evens) and in_bisect(word_list, odds)
def reverse_pair(word_list, word): """Checks whether a reversed word appears in word_list. word_list: list of strings word: string """ rev_word = word[::-1] return in_bisect(word_list, rev_word)
def interlock(word_list, word): """Checks whether a word contains two interleaved words. word_list: list of strings word: string """ word1 = word[0::2] word2 = word[1::2] return in_bisect(word_list, word1) and in_bisect(word_list, word2)
def interlock(word_list, word): """Sprawdza, czy słowo zawiera dwa „zazębiające się” słowa. word_list: lista łańcuchów word: string """ evens = word[::2] odds = word[1::2] return in_bisect(word_list, evens) and in_bisect(word_list, odds)
def reverse_pair(word_list, word): """Sprawdza, czy słowo zastrzeżone pojawia się w word_list. word_list: lista łańcuchów word: łańcuch """ rev_word = word[::-1] return in_bisect(word_list, rev_word)
def find_reverse_pairs(t): pairs = [] for i in range(len(t)): word = t[i] reverse_word = word[::-1] if reverse_word != word and in_bisect(t[i:], reverse_word): pairs.append(word) pairs.append(reverse_word) return pairs
def interlock_general(word_list, word, n=3): """Checks whether a word contains n interleaved words. word_list: list of strings word: string n: number of interleaved words """ for i in range(n): inter = word[i::n] if not in_bisect(word_list, inter): return False return True
def in_bisect(word_list, word): """Checks whether a word is in a list using bisection search. Precondition: the words in the list are sorted word_list: list of strings word: string returns: True if the word is in the list; False otherwise """ if len(word_list) == 0: return False i = len(word_list) // 2 if word_list[i] == word: return True if word_list[i] > word: # search the first half return in_bisect(word_list[:i], word) else: # search the second half return in_bisect(word_list[i+1:], word)
def interlock_general(word_list, word, n=3): """Sprawdza, czy słowo zawiera n „zazębiających się” słów. word_list: lista łańcuchów word: string n: liczba „zazębiających się” słów """ for i in range(n): inter = word[i::n] if not in_bisect(word_list, inter): return False return True
def interlock_general(word_list, word, n=3): """Checks whether a word contains n interleaved words. word_list: list of strings word: string n: number of interleaved words """ wordlist = [] for i in range(n): wordlist.append(word[i::n]) for wordinlist in wordlist: if not in_bisect(word_list, wordinlist): return False return True
def interLock(wordList, word): evens = word[::2] odds = word[1::2] return in_bisect(wordList, evens) and in_bisect(wordList, odds)
"""Checks whether a word is in a list using bisection search. Precondition: the words in the list are sorted word_list: list of strings word: string """ i = bisect.bisect_left(word_list, word) if i == len(word_list): return False return word_list[i] == word #%% if __name__ == '__main__': word_list = make_word_list() for word in ['aa', 'alien', 'allen', 'zymurgy']: print(word, 'in list', in_bisect(word_list, word)) for word in ['aa', 'alien', 'allen', 'zymurgy']: print(word, 'in list', in_bisect_cheat(word_list, word)) # ============================================================================= # reverse pair # ============================================================================= from inlist import in_bisect, make_word_list def reverse_pair(word_list, word): """Checks whether a reversed word appears in word_list. word_list: list of strings word: string """ rev_word = word[::-1] return in_bisect(word_list, rev_word)