Skip to content

Instantly share code, notes, and snippets.

@eferro

eferro/binary_search.py

Last active Jan 15, 2020
Embed
What would you like to do?
def binarySearch(alist, item):
first = 0
last = len(alist)-1
found = False
while first <= last and not found:
midpoint = (first + last)//2
if alist[midpoint] == item:
found = True
else:
if item < alist[midpoint]:
last = midpoint-1
else:
first = midpoint+1
return found
def bubbleSort(arr):
n = len(arr)
# Traverse through all array elements
for i in range(n):
# Last i elements are already in place
for j in range(0, n-i-1):
# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
# Driver code to test above
arr = [64, 34, 25, 12, 22, 11, 90]
bubbleSort(arr)
def factorial(n):
for i in range(n):
print(f'iteration [{i}]: {n}')
factorial(n-1)
print(factorial(65))
def fibonacci(num):
if (num <= 1):
return num
return fibonacci(num - 2) + fibonacci(num - 1)
print(fibonacci(78))
def is_prime_number(x):
if x >= 2:
for y in range(2,x):
# if x divides with zero remainder (i.e. equal to bool False)
if not (x % y):
return False
else:
return False
return True
defdef search(x, input):
for i in input:
print(i)
if i == x:
print('found element')
return
search(5, range(10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment