def fannkuch(n: int) -> int: count: List[int] = list(range(1, n + 1)) max_flips: int64 = 0 m: int = n - 1 n64: int64 = int64(n) r: int64 = n64 perm1: List[int] = list(range(n)) perm: List[int] = list(range(n)) perm1_ins: Callable[[int, int], None] = perm1.insert perm1_pop: Callable[[int], int] = perm1.pop while 1: while r != 1: count[r - 1] = box(r) r -= 1 if perm1[0] != 0 and perm1[m] != m: perm = perm1[:] flips_count: int64 = 0 k: int = perm[0] while k: perm[: k + 1] = perm[k::-1] flips_count += 1 k = perm[0] if flips_count > max_flips: max_flips = flips_count while r != n64: perm1_ins(box(r), perm1_pop(0)) count[r] -= 1 if count[r] > 0: break r += 1 else: return box(max_flips)
def create_array(start: int64, end: int64, step: int64) -> ArrayI64: """ Function that creates an array that contains elements from start (inclusive) to end (non-inclusve) increasing the given steps Note: if It is not possible to go from start to end, an empty array will be returned. For example: create_array(2,7,2) -> (2,4,6) ; create_array(1,4,1)->(1,2,3) """ c: int64 = start i: int64 = 0 if (end - start) * step <= 0: return ArrayI64(0) size: int64 = int64((static_abs(end - start) - 1) / static_abs(step) + 1) a: ArrayI64 = ArrayI64(box(size)) while i < size: a[i] = c c = c + step i = i + 1 return a
def fn(self, pkt: Optional[Packet], r: WorkerTaskRec) -> Task: w: WorkerTaskRec = r if pkt is None: return self.waitTask() if w.destination == I_HANDLERA: dest = I_HANDLERB else: dest = I_HANDLERA w.destination = dest pkt.ident = dest pkt.datum = 0 i = 0 while i < BUFSIZE: x: int32 = w.count + 1 w.count = x if w.count > 26: w.count = 1 pkt.data[i] = A + box(w.count) - 1 i = i + 1 return self.qpkt(pkt)
def test_box(self): self.assertEqual(box(1), 1)