This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
def binary_search(array, left, right, key): | |
mid = left + ((right - left) // 2) # finds the midpoint between left and right | |
if array[mid] == key: | |
return mid # returns the index if the key is found | |
elif key < array[mid]: | |
return binary_search(array, left, mid-1, key) # binary searches the left portion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
def gcd(a, b): | |
if b == 0: | |
return a | |
else: | |
return gcd(b, a % b) | |
a = int(input("a = ")) | |
b = int(input("b = ")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
def square_expo(x, n): | |
if n == 0: | |
return 1; | |
if n % 2 == 0: | |
return square_expo(x * x, n // 2) | |
return x * square_expo(x * x, n // 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
#Insertion Sort | |
def insertion_sort(array): | |
# Traverse through 1 to len(arr) | |
for x in range(1, len(array)): | |
y = x-1 | |
key = array[x] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
def multiply(x, y): | |
""" | |
x is a 2x2 matrix | |
y is either a 2x1 matrix or 2x2 matrix | |
x -> [0, 1, | |
2, 3] | |
y -> [0, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
def partition(array, left, right): | |
i = left - 1 # set i one less than left | |
pivot = array[right] # set as rightmost element | |
for j in range(left, right): # j keeps on going till the pivot | |
if array[j] <= pivot: | |
i += 1 # increment i and swap arr[i] and arr[j] | |
array[j], array[i] = array[i], array[j] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
def n_queens(array, n, row): | |
if row >= n: | |
for i in range(n): | |
for j in range(n): | |
if array[i][j] == 1: | |
print("x", end = ' ') | |
else: | |
print("o", end = ' ') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from itertools import permutations | |
def main(): | |
t = int(input()) | |
for cases in range(t): | |
string = str(input()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
def tower_of_hanoi(number, beginning, auxiliary, ending): | |
if number == 1: | |
print("{} -> {}".format(beginning, ending)) | |
return | |
else: | |
tower_of_hanoi(number-1, beginning, ending, auxiliary) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
class Point(object): | |
'''Creates a point on a coordinate plane with values x and y''' | |
def __init__(self, x, y): | |
'''Defines the x and y coordinates in the variables''' | |
self.X = x | |
self.Y = y |
OlderNewer