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

Aufgabe

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.

Lösung

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 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(_ _ _)

Fragen Sie AI

expand
ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt