Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 committed Dec 18, 2023
1 parent fd596ba commit a2d74eb
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions Final Round/programming_paths_part_1_2.py3
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,23 @@ def build(K):
def iter_dfs(x):
if x == 0:
return
stk = [(1, (x,))]
while stk:
step, args = stk.pop()
if step == 1:
x = args[0]
if x == 1:
double_plus_one()
continue
if x%2 == 0:
stk.append((2, (double_plus_zero,)))
stk.append((1, (x//2,)))
continue
if x%4 == 1:
stk.append((2, (double_plus_zero, double_plus_one)))
stk.append((1, (x//4,)))
continue
if x%4 == 3:
stk.append((2, (double_plus_zero, double_minus_one)))
stk.append((1, (x//4+1,)))
continue
elif step == 2:
for fn in args:
fn()
fns = []
while x != 1:
if x%2 == 0:
x //= 2
fns.append(lambda: double_plus_zero())
continue
if x%4 == 1:
x //= 4
fns.append(lambda: (double_plus_zero(), double_plus_one()))
continue
if x%4 == 3:
x = x//4+1
fns.append(lambda: (double_plus_zero(), double_minus_one()))
continue
double_plus_one()
for fn in reversed(fns):
fn()

iter_dfs(K)
return result
Expand Down

0 comments on commit a2d74eb

Please sign in to comment.