Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer 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!

Taak

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.

Oplossing

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))

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 5
single

single

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(_ _ _)

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

some-alt