@@ -22,21 +22,19 @@ def divide(self, dividend: int, divisor: int) -> int:
2222 -3
2323 >>> sol.divide(1, 1)
2424 1
25- >>> sol.divide(-2147483648, -1) # overflow case
25+ >>> sol.divide(-2147483648, -1)
2626 2147483647
2727 >>> sol.divide(24, 8)
2828 3
2929 >>> sol.divide(43, -8)
3030 -5
3131 """
32- # Edge case: if both are equal , result is 1
32+ # Edge case: if dividend equals divisor , result is 1
3333 if dividend == divisor :
3434 return 1
3535
3636 # Determine the sign of the result
37- sign = not (
38- (dividend < 0 ) ^ (divisor < 0 )
39- ) # True if same sign, False if different
37+ sign = not ((dividend < 0 ) ^ (divisor < 0 )) # True if same sign,
4038
4139 # Convert both numbers to positive
4240 d = abs (dividend ) # remaining dividend
@@ -46,8 +44,7 @@ def divide(self, dividend: int, divisor: int) -> int:
4644 # Outer loop: subtract divisor multiples from dividend
4745 while d >= n :
4846 cnt = 0
49- """Inner loop: find largest power-of-two multiple of divisor
50- that fits in dividend """
47+ # find largest power-of-two multiple of divisor that fits in dividend
5148 while d >= (n << (cnt + 1 )):
5249 cnt += 1
5350
@@ -67,10 +64,4 @@ def divide(self, dividend: int, divisor: int) -> int:
6764if __name__ == "__main__" :
6865 import doctest
6966
70- doctest .testmod () # Run the doctest examples
71-
72- # Optional: interactive test
73- sol = Solution ()
74- dividend = int (input ("Enter dividend: " ))
75- divisor = int (input ("Enter divisor: " ))
76- print (f"Quotient: { sol .divide (dividend , divisor )} " )
67+ doctest .testmod ()
0 commit comments