-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuiz15.py
More file actions
49 lines (42 loc) · 2.48 KB
/
Copy pathQuiz15.py
File metadata and controls
49 lines (42 loc) · 2.48 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
from addition import Addition
# You don't need to change the import statement
# now you can use Addition.add() function from the addition module like this:
# res = Addition.add(100, 150)
# the Addition.add() function takes in two parameters `num1` and `num2` and return the sum of `num1` and `num2`
# Please create and implement a Calculator class, which makes use of the `addition` module.
# Your Calculator should achieve these goals:
# - It should implement `Addition.add()`, `subtract()`, `multiply()` and `divide()` methods.
# - It cannot use addition, subtraction, multiplication and division operators (`+`, `-`, `*` and `/`) directly.
# Instead, it should be only based on the `Addition.add()` function from the `addition` module.
# - To simplify the problem, you may expect input for the multiply() and divide() methods are all non-integers,
# and will always be valid, i.e. all non-negative integers and no 0 as divisor.
# the class definition and a sample class method `Addition.add()` is provided below
class Calculator:
# a sample add() method in our calculator is shown below
# you may learn from it and implement the other methods
@classmethod
def add(cls, num1, num2):
return Addition.add(num1, num2) # make use of add() from addition module
# implement a class method `subtract()` that takes in num1 and num2 and return num1 - num2
# your `subtract()` method cannot use the + - * / calculation operators, but can use - as a negative sign operator
def subtract(num1, num2):
return Addition.add(num1, -num2)
# implement a class method `multiply()` that takes in num1 and num2 and return num1 * num2
# your `multiply()` method cannot use the + - * / calculation operators, but can use - as a negative sign operator
# you may assume num1 and num2 are always non-negative integers
def multiply(num1, num2):
sum = 0
for i in range(num2):
sum = Addition.add(sum,num1)
return sum
# implement a class method `divide()` that takes in num1 and num2 and return num1 // num2
# your `divide()` method cannot use the + - * / calculation operators, but can use - as a negative sign operator
# you may assume num1 is always a non-negative integer, and num2 is always a positive integer
def divide(num1, num2):
counter = 0
while(num1 >= num2):
counter = Addition.add(1,counter)
num1 = Addition.add(num1, -num2)
return counter
cal = Calculator
print(cal.devide(8,5))