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 / Sieve of Eratosthenes.py
Last active June 8, 2018 16:12
Finding primes using the Sieve of Eratosthenes
#!/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
@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 / Snake Game.py
Last active February 2, 2023 19:15
A simple snake game written in Python using the PyGame library (https://github.com/rajatdiptabiswas/snake-pygame)
"""
Snake Eater
Made with PyGame
"""
import pygame, sys, time, random
# Difficulty settings
# Easy -> 10
@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())