def _reduce_path(path): if not len(path): return '' segments = [] cur_dir = None last = None segment = [] for cur in path: if last is not None: new_dir = a2num(cur) - a2num(last) if abs(new_dir) > 1: # jump sign = 1 if new_dir > 0 else -1 segments.append((segment + [last], cur_dir or 0)) segments.append(([ rotate_alphabet(last, sign), rotate_alphabet(cur, -sign) ], 'Jump')) segment = [] new_dir = None elif cur_dir is None or new_dir == cur_dir: # continue segment.append(last) elif new_dir == 0: # sloped to flat segments.append((segment, cur_dir)) segment = [last] elif cur_dir == 0: # go from flat to sloped segments.append((segment + [last], cur_dir)) segment = [] else: # V or ^ shape segments.append((segment, cur_dir)) segments.append(([last], 0)) segment = [] cur_dir = new_dir last = cur assert last is not None segments.append((segment + [last], cur_dir or 0)) result = [] # print('segments', segments) for i, (segment, dir) in enumerate(segments): assert dir in [-1, 1, 0, 'Jump'], dir if dir == 0: if i > 0 and i < len(segments) - 1 and segments[ i - 1][1] == segments[i + 1][1] and segments[ i - 1][1] != 'Jump': # 1,0,1 or -1,0,-1 pattern segment = segment[1:] if dir in [-1, 1]: if (i == 0 or segments[i - 1][1] != 'Jump') and ( i == len(segments) - 1 or segments[i + 1][1] != 'Jump'): # chain breaks if i == 0: result.append(segment[0]) if i == len(segments) - 1: result.append(segment[-1]) segment = [] result.extend(segment) # print('segment', segment, 'new result', result) # print('final result', result) return ''.join(result)
def extend_sequences(x): prev = None direction = None result = [] for char in x + ' ': cur = char if prev is not None: if direction is not None: target = rotate_alphabet(prev, direction) if cur != target: result.append(target) new_direction = None for possible_dir in [1, 0, -1]: if cur == rotate_alphabet(prev, possible_dir): new_direction = possible_dir if direction is None and new_direction is None: result.append(prev) direction = new_direction prev = cur if is_alphabet(cur) or cur == ' ' else None return ''.join(result)
def corrupt(x, extra_index=0, extra_rotate=0): """ Corruption by adding amount to a single position of each word Amount/position both determined by sum of letters Everything is mod 27 instead of 26 to make things work out better """ if not len(x): return x total = sum([a2num(l, with_spaces=True) for l in x]) index = (total - 1 + extra_index) % len(x) new = rotate_alphabet(x[index], total + extra_rotate, with_spaces=True) return x[:index] + new + x[index + 1:]
def corrupt_final(x): """ Corruption by adding amount to a single position of each word Amount/position both determined by sum of letters Everything is mod 27 instead of 26 to make things work out better """ n = len(x) if not n: return x total = sum([a2num(l, with_spaces=True) for l in x]) index = (total + (n // 3)) % len(x) new = rotate_alphabet(x[index], total + (n // 8), with_spaces=True) return x[:index] + new + x[index + 1:]
def tournament(x): vals = [0 for _ in range(len(x))] def parent(i): if i == 0: return None else: pow2 = 1 while i % (pow2 * 2) == 0: pow2 *= 2 return i - pow2 for i, l in enumerate(x): j = parent(i) while j is not None: vals[j] += a2num(l, with_spaces=True) j = parent(j) return ''.join(rotate_alphabet(l, vals[i], with_spaces=True) for i, l in enumerate(x))
def fn(x): # print('x', x) s = ' ' offset = 0 i = 0 for char in x: if char == ' ': offset = len(s) s = s + s i = 0 else: ind = offset + (i % (len(s) - offset)) new_l = rotate_alphabet(s[ind], a2num(char, with_spaces=True), with_spaces=True).lower() # i = a2num(char, with_spaces=False) # print(x, i, len(s)) s = s[:ind] + new_l + s[ind + 1:] i += 1 # print(s) return s