Skip to content

Instantly share code, notes, and snippets.

@donkirkby
Created February 26, 2023 19:54
Show Gist options
  • Save donkirkby/2510b38171880a89e4ee892d4e70089d to your computer and use it in GitHub Desktop.
Save donkirkby/2510b38171880a89e4ee892d4e70089d to your computer and use it in GitHub Desktop.
Live Coding Demo
def search(a, n):
low = 0
high = len(a) - 1
while True:
mid = low + high // 2
v = a[mid]
if v == n:
return mid
if n < v:
high = mid - 1
return -1
i = search([1, 2, 4], 2)
def search(a, n):
low = 0
high = len(a) - 1
while True:
mid = (low + high) // 2
v = a[mid]
if v == n:
return mid
if n < v:
high = mid - 1
else:
low = mid + 1
return -1
# i = search([1, 2, 4], 2)
from search import search
def test_middle():
i = search([1, 2, 4], 2)
assert i == 1
def test():
i = search([1, 2, 4], 1)
assert i == 0
from search import search
def test_middle():
i = search([1, 2, 4], 2)
assert i == 1
def test_left():
i = search([1, 2, 4], 1)
assert i == 0
def test():
i = search([1, 2, 4], 4)
assert i == 2
import turtle as t
t.fillcolor('green')
t.pensize(3)
t.begin_fill()
sides = 4
for i in range(sides):
t.forward(100)
t.right(360/sides)
t.end_fill()
t.hideturtle()
t.done()
import turtle as t
t.fillcolor('green')
t.pensize(3)
t.begin_fill()
sides = 5
for i in range(300):
t.forward(50 + i*10)
t.right(360/sides*2 + 1)
t.end_fill()
t.hideturtle()
t.done()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment