import collections def count_start_word(filename): ctr = collections.Counter() with open(filename, encoding="latin-1") as infile: for line in infile: for word in line.split(): word = word.lower().strip("[&(*,.;'\"'`?!") if word: ##we could have stripped the word to nothing ctr[word[0]] += 1 return ctr.most_common() def count_end_word(filename): ctr = collections.Counter() with open(filename, encoding="latin-1") as infile: for line in infile: for word in line.split(): word = word.lower().strip("[&(*,.;:'\"'`?!") if word: ##we could have stripped the word to nothing ctr[word[-1]] += 1 return ctr.most_common() def replace(string, dictionary): result = [] for letter in string: if letter not in dictionary: result.append(letter) else: result.append(dictionary[letter]) return "".join(result) def inane(n): best_seen = n for ones in range(n+1): for fours in range(n+1): for fives in range(n+1): if ones*1+fours*4+fives*5 == n: if ones+fours+fives < best_seen: best_seen = ones+fours+fives return best_seen def lick(amount): if amount==8: print("Trap") if amount == 0: return 0 if amount < 4: return amount if amount == 4: return 1 if amount == 5: return 1 return min([lick(amount-1), lick(amount-4), lick(amount-5)])+1 lick_dictionary = {0:0, 1:1, 2:2, 3:3, 4:1, 5:1} def mlick(amount): if amount in lick_dictionary: return lick_dictionary[amount] else: result = min([mlick(amount-1), mlick(amount-4), mlick(amount-5)])+1 lick_dictionary[amount] = result return result