Skip to content

Commit d6ebf17

Browse files
Merge pull request #1 from code4policy/new-features
cube and square changes coming thru
2 parents cede375 + dcc2e28 commit d6ebf17

2 files changed

Lines changed: 53 additions & 37 deletions

File tree

calculator.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
1-
def multiply(a,b):
1+
def multiply(a, b):
22
return a * b
33

4-
def add(a,b):
5-
return a+b
64

7-
def subtract(a,b):
8-
return a-b
5+
def add(a, b):
6+
return a + b
97

10-
def divide(a,b):
11-
return a/b
128

9+
def subtract(a, b):
10+
return a - b
1311

14-
print("I'm going use the calculator functions to multiply 5 and 6")
15-
x = multiply(5,6)
16-
print(x)
12+
13+
def divide(a, b):
14+
return a / b
15+
16+
17+
def square(x):
18+
return x ** 2
19+
20+
21+
def cube(x):
22+
return x ** 3
23+
24+
25+
def square_n_times(number, n):
26+
"""
27+
Squares `number` n times, summing each intermediate squared value.
28+
29+
Example: number=2, n=3
30+
2^2 = 4
31+
4^2 = 16
32+
16^2 = 256
33+
return 4 + 16 + 256 = 276
34+
"""
35+
total = 0
36+
value = number
37+
for _ in range(n):
38+
value = value ** 2
39+
total += value
40+
return total

test_calculator.py

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
1-
import pytest
1+
from calculator import add, subtract, multiply, divide, square, cube, square_n_times
22

3-
def test_multiply():
4-
from calculator import multiply
5-
assert multiply(3, 4) == 12, "Expected 3 * 4 to be 12"
6-
assert multiply(0, 5) == 0, "Expected 0 * 5 to be 0"
7-
assert multiply(-2, 6) == -12, "Expected -2 * 6 to be -12"
83

94
def test_add():
10-
from calculator import add
11-
assert add(3, 4) == 7, "Expected 3 + 4 to be 7"
12-
assert add(-5, 5) == 0, "Expected -5 + 5 to be 0"
13-
assert add(0, 0) == 0, "Expected 0 + 0 to be 0"
5+
assert add(2, 3) == 5
6+
147

158
def test_subtract():
16-
from calculator import subtract
17-
assert subtract(10, 4) == 6, "Expected 10 - 4 to be 6"
18-
assert subtract(0, 5) == -5, "Expected 0 - 5 to be -5"
19-
assert subtract(-3, -7) == 4, "Expected -3 - (-7) to be 4"
9+
assert subtract(10, 4) == 6
10+
11+
12+
def test_multiply():
13+
assert multiply(3, 7) == 21
14+
2015

2116
def test_divide():
22-
from calculator import divide
23-
assert divide(12, 3) == 4, "Expected 12 / 3 to be 4"
24-
assert divide(-9, 3) == -3, "Expected -9 / 3 to be -3"
25-
with pytest.raises(ZeroDivisionError):
26-
divide(5, 0) # Ensure it raises an exception for divide by zero
17+
assert divide(8, 2) == 4
18+
2719

2820
def test_square():
29-
from calculator import square
30-
assert square(12) == 144, "Expected 12^2 to be 144"
31-
assert square(2) == 4, "Expected 2^2 to be 4"
32-
assert square(-2) == 4, "Expected (-2)^2 to be 4"
21+
assert square(5) == 25
22+
3323

3424
def test_cube():
35-
from calculator import cube
36-
assert cube(12) == 1728, "Expected 12^3 to be 1728"
37-
assert cube(2) == 8, "Expected 2^3 to be 8"
38-
assert cube(-2) == -8, "Expected (-2)^3 to be -8"
25+
assert cube(3) == 27
26+
27+
28+
def test_square_n_times():
29+
assert square_n_times(2, 3) == 276
30+
assert square_n_times(10, 0) == 0

0 commit comments

Comments
 (0)