forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation.py
62 lines (49 loc) · 1.54 KB
/
operation.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
def negate(a):
if a == 0:
return a
result = 0
counter = 0
while counter != a:
result = result + (-1 if a > 0 else 1)
counter = counter + (-1 if a < 0 else 1)
return result
def subtraction(a, b):
return a + negate(b)
def multiply(a, b):
counter = 1
result = 0
multiplier = a if a <= b else b
multiplee = a if a >= b else b
# the final result needs to be negated only if either a or b is -ve
needs_to_negate = False
# remove -ve value
# if both are negative, then result will be +ve
if multiplee < 0 and multiplier < 0:
multiplee = negate(multiplee)
multiplier = negate(multiplier)
needs_to_negate = False
elif multiplier < 0 or multiplee < 0:
needs_to_negate = True
if multiplier < 0:
multiplier = negate(multiplier)
else:
multiplee = negate(multiplee)
# add multiplee to itself multiplier times
while counter <= multiplier:
result += multiplee
counter += 1
# negate result if needed
return negate(result) if needs_to_negate else result
if __name__ == '__main__':
assert negate(10) == -10
assert negate(-10) == 10
assert negate(0) == 0
assert subtraction(10, 20) == -10
assert subtraction(1, 0) == 1
assert subtraction(0, 1) == -1
assert subtraction(100, 50) == 50
assert multiply(1, 100) == 100
assert multiply(10, 10) == 100
assert multiply(-1, -20) == 20
assert multiply(-10, 20) == -200
assert multiply(10, -20) == -200