Skip to content

Instantly share code, notes, and snippets.

Last active January 15, 2020 15:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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
if item < alist[midpoint]:
last = midpoint-1
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]
def factorial(n):
for i in range(n):
print(f'iteration [{i}]: {n}')
def fibonacci(num):
if (num <= 1):
return num
return fibonacci(num - 2) + fibonacci(num - 1)
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
return False
return True
defdef search(x, input):
for i in input:
if i == x:
print('found element')
search(5, range(10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment