Division
Look at the explanation of the task! How does it work?
Use hints if needed! Be careful with the tabulation!
Tarea
Swipe to start coding
- Set condition if the
y
equals the0
. - Set the
middle
variable. Themiddle
equals theleft + (right - left) // 2
. - Return the product of the
middle
and thesign
. - Update the
left
. Theleft
equals themiddle
. - Update the
right
. Theright
equals themiddle
. - Test the function with the
x = 9
andy = 3
.
Solución
99
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
def divide_binarySearch(x, y):
# Set condition if the 'y' equals the 0
if y == 0:
return 'Infinity'
left = 0
right = 100000000000
sign = 1
if x * y < 0:
sign = -1
while True:
# Set the 'middle' variable
middle = left + (right - left) // 2
if abs(abs(y) * middle - abs(x)) <= 0:
# Return the product of the 'middle' and the 'sign'
return middle * sign
if abs(y) * middle < abs(x):
# Update the 'left'
left = middle
else:
# Update the 'right'
right = middle
# Testing
# Test the function with the x = 9 and y = 3
print(divide_binarySearch(9, 3))
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 2. Capítulo 5
99
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
def divide_binarySearch(x, y):
# Set condition if the 'y' equals the 0
_ _ _:
return 'Infinity'
left = 0
right = 100000000000
sign = 1
if x * y < 0:
sign = -1
while True:
# Set the 'middle' variable
_ _ _ = _ _ _
if abs(abs(y) * middle - abs(x)) <= 0:
# Return the product of the 'middle' and the 'sign'
_ _ _
if abs(y) * middle < abs(x):
# Update the 'left'
_ _ _
else:
# Update the 'right'
_ _ _
# Testing
# Test the function with the x = 9 and y = 3
print(_ _ _)
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla