Repository navigation
Expand file tree
/
Copy pathtest2.py
More file actions
129 lines (92 loc) · 2.8 KB
/
Copy pathtest2.py
File metadata and controls
129 lines (92 loc) · 2.8 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
print("=== EDGE CASE TESTS ===")
# --------------------------------------------------
# Operator Precedence
# --------------------------------------------------
print("2 + 3 * 4 =", 2 + 3 * 4) # 14
print("(2 + 3) * 4 =", (2 + 3) * 4) # 20
print("10 - 2 - 3 =", 10 - 2 - 3) # 5 (left associative)
print("100 / 10 / 2 =", 100 / 10 / 2) # 5.0
print("2 * 3 + 4 * 5 =", 2 * 3 + 4 * 5) # 26
print("1 + 2 * 3 - 4 =", 1 + 2 * 3 - 4) # 3
# Unary operators
print("-5 + 10 =", -5 + 10) # 5
print("-(2 + 3) =", -(2 + 3)) # -5
# Comparison precedence
print("3 > 2 and 2 > 1 =", 3 > 2 and 2 > 1)
# --------------------------------------------------
# Boolean Logic
# --------------------------------------------------
print(True and False) # False
print(True or False) # True
print(not False) # True
print(not True == False) # True
# --------------------------------------------------
# Nested Expressions
# --------------------------------------------------
x = (2 + 3) * (4 + 5)
print("x =", x) # 45
y = ((1 + 2) * (3 + 4)) - ((5 + 6) / 11)
print("y =", y)
# --------------------------------------------------
# Variable Reassignment
# --------------------------------------------------
a = 10
print(a)
a = a + 5
print(a)
a = a * 2
print(a)
# --------------------------------------------------
# Function Calls in Expressions
# --------------------------------------------------
def add(a, b):
return a + b
print(add(1, 2))
print(add(1, 2) * 10)
print(add(add(1, 2), add(3, 4)))
# --------------------------------------------------
# Deep Recursion Test
# --------------------------------------------------
def fact(n):
if n <= 1:
return 1
return n * fact(n - 1)
print(fact(6)) # 720
# --------------------------------------------------
# List Access
# --------------------------------------------------
numbers = [10, 20, 30]
print(numbers[0])
print(numbers[1])
print(numbers[2])
# --------------------------------------------------
# Runtime Error Tests
# --------------------------------------------------
print("--- EXPECTED ERRORS BELOW ---")
# Division by zero
print(10 / 0)
# Undefined variable
print(unknown_variable)
# Invalid list index
print(numbers[10])
# Negative list index
print(numbers[-1])
# Type mismatch
print("abc" - "def")
# Wrong function arguments
print(add(1))
# Infinite recursion
def loop():
return loop()
print(loop())
def check(value, expected):
if value == expected:
print("PASS")
else:
print("FAIL:", value, "expected", expected)
check(2 + 3 * 4, 14)
check((2 + 3) * 4, 20)
check(10 - 2 - 3, 5)
check(-(-5), 5)
check(add(10, 20), 30)
check(fact(5), 120)