Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Division | Problems
Binary Search in Python

book
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

  1. Set condition if the y equals the 0.
  2. Set the middle variable. The middle equals the left + (right - left) // 2.
  3. Return the product of the middle and the sign.
  4. Update the left. The left equals the middle.
  5. Update the right. The right equals the middle.
  6. Test the function with the x = 9 and y = 3.

Solución

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?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 5
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

expand
ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

some-alt