-
Notifications
You must be signed in to change notification settings - Fork 0
/
divide.py
109 lines (70 loc) · 2.51 KB
/
divide.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
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 25 13:27:14 2023
@author: jamesboot
"""
# Given two integers dividend and divisor,
# divide two integers without using multiplication, division, and mod operator.
# The integer division should truncate toward zero, which means losing its fractional part.
# For example, 8.345 would be truncated to 8, and -2.7335 would be truncated to -2.
# Return the quotient after dividing dividend by divisor.
# Note: Assume we are dealing with an environment that could only store integers within
# the 32-bit signed integer range: [−2^31, 2^31 − 1].
# For this problem, if the quotient is strictly greater than 2^31 - 1, then return 2^31 - 1,
# and if the quotient is strictly less than -2^31, then return -2^31.
def divide(dividend, divisor):
if divisor < 0 and dividend > 0:
divisorNew = abs(divisor)
quotient = 0
while divisorNew <= dividend:
quotient += 1
divisorNew += abs(divisor)
if quotient > 2**31:
return -2**31
else:
return -abs(quotient)
if divisor > 0 and dividend < 0:
divisorNew = abs(divisor)
dividendNew = abs(dividend)
quotient = 0
while divisorNew <= dividendNew:
quotient += 1
divisorNew += abs(divisor)
if quotient > 2**31:
return -2**31
else:
return -abs(quotient)
if divisor > 0 and dividend > 0:
divisorNew = abs(divisor)
quotient = 0
while divisorNew <= dividend:
quotient += 1
divisorNew += abs(divisor)
if quotient > (2**31)-1:
return (2**31)-1
else:
return quotient
if divisor < 0 and dividend < 0:
divisorNew = abs(divisor)
quotient = 0
while divisorNew <= dividend:
quotient += 1
divisorNew += abs(divisor)
if quotient > (2**31)-1:
return (2**31)-1
else:
return quotient
if abs(dividend) < abs(divisor):
return 0
print(divide(10, 3))
print(divide(7, -3))
print(divide(-7, 3))
print(divide(3, 7))
print(divide(-3, -7))
print(divide(3, -7))
print(divide(-3, 7))
print(divide(27, 9))
print(divide(7868, 9))
print(divide(2**33, 1))
print(divide(982374, 73))