Skip to content

Instantly share code, notes, and snippets.

View rajatdiptabiswas's full-sized avatar
:octocat:
Octocat says Git Gud

rajat rajatdiptabiswas

:octocat:
Octocat says Git Gud
View GitHub Profile
@rajatdiptabiswas
rajatdiptabiswas / Binary Search.py
Created October 22, 2017 10:23
Binary Search Algorithm
#!/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
@rajatdiptabiswas
rajatdiptabiswas / Euclid GCD.py
Created October 22, 2017 10:25
Euclid's Algorithm to find GCD of two numbers
#!/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 = "))
@rajatdiptabiswas
rajatdiptabiswas / Exponentiation by Squaring.py
Created October 22, 2017 10:26
Exponentiation by Squaring Algorithm
#!/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)
@rajatdiptabiswas
rajatdiptabiswas / Insertion Sort.py
Created October 22, 2017 10:27
Sorting Algorithm: Insertion Sort
#!/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]
@rajatdiptabiswas
rajatdiptabiswas / Matrix Exponentiation.py
Last active October 22, 2017 10:29
Matrix Exponentiation Algorithm
#!/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,
@rajatdiptabiswas
rajatdiptabiswas / Quick Sort.py
Created October 22, 2017 10:31
Sorting Algorithm: Quick Sort
#!/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]
@rajatdiptabiswas
rajatdiptabiswas / N Queens.py
Last active December 12, 2017 06:22
Place N chess queens on an N×N chess board so that no two queens attack each other
#!/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 = ' ')
@rajatdiptabiswas
rajatdiptabiswas / Palindromic Permutations.py
Created February 5, 2018 17:23
Lists all the palindromes that can be formed from a given string
#!/usr/bin/env python3
from itertools import permutations
def main():
t = int(input())
for cases in range(t):
string = str(input())
@rajatdiptabiswas
rajatdiptabiswas / Tower of Hanoi.py
Last active February 14, 2018 08:59
Python program to solve the Tower of Hanoi problem recursively
#!/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)
@rajatdiptabiswas
rajatdiptabiswas / 2D Point Class.py
Created March 1, 2018 12:39
A point class with x and y coordinates for 2D planes
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