-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAr.py
108 lines (102 loc) · 3.68 KB
/
Ar.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
''''; xterm-send "echo -ne \"\\033c\"; python3 $0" ; exit 1 ;
'''
inc = lambda a: a if type(a)==str else a+1
sub = lambda a,b: a if type(a)==str else (b if type(b)==str else a-b)
mul = lambda a,b: a if type(a)==str else (b if type(b)==str else a*b)
div = lambda a,b: a if type(a)==str else (b if type(b)==str else (str(a) if b==0 else (int(a//b) if a*b>=0 else -int(-a//b))))
zero = lambda a: sub(a,a)
one = lambda a: inc(zero(a))
two = lambda a: inc(one(a))
three = lambda a: inc(two(a))
dec = lambda a: sub(a,one(a))
negate = lambda a: sub(zero(a),a)
add = lambda a,b: sub(a,negate(b))
rem = lambda a,b: sub(a,mul(b,div(a,b)))
square = lambda a: mul(a,a)
isNonZero = lambda a: div(square(sub(square(a),one(a))),square(add(square(a),one(a))))
isZero = lambda a: isNonZero(isNonZero(a))
Not = lambda a: isNonZero(a)
And = lambda a,b: isZero(add(isZero(a),isZero(b)))
Or = lambda a,b: isZero(mul(a,b))
eq = lambda a,b: isZero(sub(a,b))
notEq = lambda a,b: Not(eq(a,b))
divz = lambda a,b: div(a,add(b,isNonZero(b)))
If = lambda a,b,c: add(mul(isNonZero(a),b),mul(isZero(a),c))
isNegative = lambda a: If(isZero(a),one(a),isNonZero(divz(sub(one(a),a),a)))
isPositive = lambda a: Not(isNegative(a))
lt = lambda a,b: If(eq(a,b),one(a),If(And(isNegative(a),isPositive(b)),zero(a),If(And(isPositive(a),isNegative(b)),one(a),If(And(isNegative(a),isNegative(b)),isZero(divz(b,a)),isZero(divz(a,b))))))
gt = lambda a,b: And(Not(lt(a,b)),Not(eq(a,b)))
lte = lambda a,b: Or(eq(a,b),lt(a,b))
gte = lambda a,b: Or(eq(a,b),gt(a,b))
sign = lambda a: If(isNegative(a),sub(zero(a),one(a)),If(eq(a,zero(a)),zero(a),one(a)))
tsub = lambda a,b: If(lt(a,b),zero(a),sub(a,b))
min = lambda a,b: If(lt(a,b),a,b)
max = lambda a,b: If(lt(a,b),b,a)
absolutevalue = lambda a: mul(sign(a),a)
absolutedifference = lambda a,b: absolutevalue(sub(a,b))
isDivisible = lambda a,b: isZero(rem(a,b))
isEven = lambda a: isDivisible(a,two(a))
isOdd = lambda a: Not(isEven(a))
sum = lambda a,b: div(mul(add(a,b),add(sub(b,a),one(a))),two(a))
cantorPair = lambda a,b: div(add(add(add(add(mul(a,a),a),mul(two(a),mul(a,b))),mul(three(a),b)),mul(b,b)),two(a))
d = lambda a: inc(u(a,a))
test = lambda a,b: [(print(f'\x1b[31m{a}\x1b[0m', "==" ,x, f'\x1b[31m{b}\x1b[0m') if str(x)!=str(b) else 0) if b!=None else print(a,"==",x) for x in [eval(a)]]
TRUE = 0
FALSE = 1
test("inc(0)",1)
test("inc(1)",2)
test("inc(-1)",0)
test("mul(21,2)",42)
test("div(22,2)",11)
test("inc(negate(1))",0)
test("negate(1)",-1)
test("add(2,2)",4)
test("add(10,10)",20)
test("add(33,11)",44)
test("mul(2,4)",8)
test("mul(10,10)",100)
test("mul(negate(10),0)",0)
test("div(10,0)",10)
test("divz(10,0)",10)
test("rem(10,0)",10)
test("negate(42)",-42)
test("isZero(42)",FALSE)
test("isZero(0)",TRUE)
test("isZero(negate(42))",FALSE)
test("eq(0,0)",TRUE)
test("eq(0,4)",FALSE)
test("isDivisible(42,2)",TRUE)
test("isDivisible(42,4)",FALSE)
test("isEven(42)",TRUE)
test("isEven(43)",FALSE)
test("isOdd(43)",TRUE)
test("isEven(-42)",TRUE)
test("isEven(-43)",FALSE)
test("isOdd(-43)",TRUE)
test("absolutevalue(negate(43))",43)
test("absolutedifference(negate(43),43)",86)
test("sign(42)",1)
test("sign(0)","0")
test("sign(negate(42))",-1)
test("min(42,negate(42))",-42)
test("max(42,negate(42))",42)
test("sum(0,100)",int(100*101/2))
test("sum(negate(100),100)","0")
test("sum(0,2)",int(2*3/2))
test("div(3,2)",int(3/2))
test("cantorPair(2,3)",18)
test("add(42,42)",84)
test("isZero(42)",FALSE)
test("isZero(negate(42))",FALSE)
test("isZero(0)",TRUE)
test("eq(0,0)",TRUE)
test("eq(0,4)",FALSE)
test("notEq(0,0)",FALSE)
test("notEq(0,4)",TRUE)
test("If(TRUE,42,20)",42)
test("If(FALSE,42,20)",20)
test("isNegative(42)",1)
test("isNegative(0)",FALSE)
test("isPositive(0)",TRUE)
test("add(div(2,0),div(1,0))",2)
test("add(div(1,0),div(2,0))",1)