str = '' data = {} letters = [] with open('data', 'r') as fp: for line in fp: if(' -> ' in line): pair = line.strip().split(' -> ') data[pair[0]] = pair[1] elif(len(line) > 1): str = line.strip() for ch in str: if(not ch in letters): letters.append(ch) letters.sort() # # DEBUG(dev): # print(data) # print(str) # print(letters) for step in range(0, 40): print('BEFORE:', str) new_str = '' for i in range(0, len(str) - 1): lookup = str[i:i+2] print(lookup) if(lookup in data.keys()): new_str += str[i] + data[lookup] else: new_str += str[i] new_str += str[-1] str = new_str counts = {} for ch in str: if(ch in counts): counts[ch] += 1 else: counts[ch] = 1 counts_list = list(counts.values()) counts_list.sort() print(counts_list[-1] - counts_list[0])