This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 sieve_of_eratosthenes(n): | |
''' | |
Complexity : O(n log log n) + O(n) ~ O(n log log n) | |
''' | |
prime = [True for i in range(n+1)] | |
p = 2 | |
# if there are no factors of n till sqrt(n) then the number is prime |
This file contains hidden or 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 hidden or 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
""" | |
Snake Eater | |
Made with PyGame | |
""" | |
import pygame, sys, time, random | |
# Difficulty settings | |
# Easy -> 10 |
This file contains hidden or 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()) |
OlderNewer