-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path3.12-3.19 mutabl data.py
84 lines (64 loc) · 1.57 KB
/
3.12-3.19 mutabl data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from orderedPair import *
# 3.12
# response = (b, None)
# response = (b, (c, (d, None)))
# 3.13
# ('a', ('b', ('c', ('a', ('b', ('c', ('a', ('b', ('c',...
# 3.14
# v = ('a', ('b', ('c', None)))
# w = ('c', ('b', ('a', None)))
# 3.17
def eq(sy1, sy2):
if sy1 is sy2:
return True
else:
return False
def memq(sy, seq):
if seq is None:
return False
elif eq(sy,car(seq)):
return True
else:
return memq(sy, cdr(seq))
def count_pairs(x):
memo_list = None
def inner(k):
nonlocal memo_list
if not isinstance(k, tuple) or memq(k, memo_list):
return 0
else:
cons(k, memo_list)
return inner(car(k), memo_list) + inner(cdr(k), memo_list) + 1
return inner(x)
# 3.18
def is_loop(lst):
identity = cons(None, None)
def iter(remain):
nonlocal identity
if remain is None:
return False
elif eq(identity,car(remain)):
return True
else:
set_car(remain,identity)
return iter(cdr(remain))
return iter(lst)
# 3.19
def is_loop_new(lst):
def list_walk(step, seq):
if seq is None:
return None
elif not step:
return seq
else:
return list_walk(step - 1, cdr(seq))
def iter(x, y):
xl = list_walk(1, seq)
yl = list_walk(2, seq)
if xl is None or yl is None:
return False
elif eq(xl, yl):
return True
else:
return iter(xl, yl)
return iter(lst, lst)