repl.it
@webbdevvue/

importing_modules_exer

Python

No description

fork
loading
Files
  • main.py
  • addition.py
main.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
from addition import Addition
 
class Calculator:
    @classmethod
    def add(cls, num1, num2):
        return Addition.add(num1, num2)  # make use of add() from Addition module
 
    @classmethod
    def subtract(cls, num1, num2):
        return cls.add(num1, -num2)     # turn subtraction to adding a negative num2
 
    @classmethod
    def multiply(cls, num1, num2):
        res = 0
        for x in range(0, num2):
            res = cls.add(res, num1)    # add num1 for num2 times
        return res
 
    @classmethod
    def divide(cls, num1, num2):
        res = 0
        while num1 >= num2:
            num1 = cls.subtract(num1, num2)  # subtract num2 from num1 until its remainder is smaller than num2
            res = cls.add(res, 1)   # count the times of subtraction as the result
        return res

'''
=====>> 
MY ATTEMPT THAT WORKS BUT NOT BY THE RULES
<<=====
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
    @classmethod
    def subtract(cls, 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
    @classmethod
    def multiply(cls, num1, num2):
      res = 0
      for i in range(0, num2):
        res = res + num1
      return res

    # 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
    @classmethod
    def divide(cls, num1, num2):
      quot = 0
      while num1 >= num2:
        num1 = num1 - num2
        quot+=1
      return quot

my_add = Calculator.add(5, 10)
print(f'Result from ADD classmethod "{my_add}".')
my_sub = Calculator.subtract(10, 5)
print(f'Result from SUBTRACT classmethod "{my_sub}".')
my_mult = Calculator.multiply(10, 5)
print(f'Result from MULTIPLY classmethod "{my_mult}".')
my_div = Calculator.divide(25, 5)
print(f'Result from DIVIDE classmethod "{my_div}".')
'''