Skip to content

Instantly share code, notes, and snippets.

View anirudhjayaraman's full-sized avatar
🏠
Working from home

Anirudh Jayaraman anirudhjayaraman

🏠
Working from home
View GitHub Profile
@anirudhjayaraman
anirudhjayaraman / karatsuba.py
Last active September 11, 2023 10:18
Karatsuba Multiplication
def karatsuba(x,y):
"""Function to multiply 2 numbers in a more efficient manner than the grade school algorithm"""
if len(str(x)) == 1 or len(str(y)) == 1:
return x*y
else:
n = max(len(str(x)),len(str(y)))
nby2 = n / 2
a = x / 10**(nby2)
b = x % 10**(nby2)
@anirudhjayaraman
anirudhjayaraman / HW01.R
Created October 12, 2015 16:26
14.74x Foundation of Development Policy (Homework Assignment 01)
# set working directory to local directory where the data is kept
setwd("~/IGIDR/Development Economics - MIT/Homework Assignment 01")
# read the data
wb_dev_ind = read.csv("wb_dev_ind.csv")
# summarize data
summary(wb_dev_ind)
# Question 1
# What is the Mean of GDP per capita? What is the standard deviation of GDP per capita?
@anirudhjayaraman
anirudhjayaraman / karatsuba_pseudocode.txt
Created October 13, 2015 17:02
Pseudocode for Karatsuba Multiplication Algorithm
procedure karatsuba(num1, num2)
if (num1 < 10) or (num2 < 10)
return num1*num2
/* calculates the size of the numbers */
m = max(size_base10(num1), size_base10(num2))
m2 = m/2
/* split the digit sequences about the middle */
high1, low1 = split_at(num1, m2)
high2, low2 = split_at(num2, m2)
/* 3 calls made to numbers approximately half the size */
@anirudhjayaraman
anirudhjayaraman / sherlockandthebeast.py
Created December 12, 2015 01:49
Solution to HackerRank Problem - Sherlock and the Beast
#!/bin/python
import sys
def combinations_sum(n):
k = n/2
combs = []
answers = []
for i in range(0,k+1):
a,b = i, n-i
@anirudhjayaraman
anirudhjayaraman / countInversions.py
Created October 23, 2015 13:29
Counting the Number of Inversions In An Array
# load contents of text file into a list
# numList
NUMLIST_FILENAME = "IntegerArray.txt"
inFile = open(NUMLIST_FILENAME, 'r')
with inFile as f:
numList = [int(integers.strip()) for integers in f.readlines()]
count = 0
@anirudhjayaraman
anirudhjayaraman / DSelect.py
Created July 22, 2016 12:34
Deterministic Selection (Median of Medians) Algorithm
def merge_tuple(a,b):
""" Function to merge two arrays of tuples """
c = []
while len(a) != 0 and len(b) != 0:
if a[0][0] < b[0][0]:
c.append(a[0])
a.remove(a[0])
else:
c.append(b[0])
b.remove(b[0])
@anirudhjayaraman
anirudhjayaraman / HW02.R
Last active June 14, 2021 09:25
14.74x Foundation of Development Policy (Homework Assignment 02)
# Set working directory to local directory where the data is kept
setwd("~/IGIDR/Development Economics - MIT/Homework Assignment 02")
# read data
migueldata = read.csv("ted_miguel_worms.csv", header = TRUE)
attach(migueldata)
# Question 6
# How many observations are there per pupil? (Enter a whole number of 0 or higher)?
length(migueldata$pupid)
length(unique(migueldata$pupid))
# Question 7
@anirudhjayaraman
anirudhjayaraman / lm_linear_algebra.R
Created November 12, 2019 14:57
Linear regression implementation using linear algebra in R
### Linear Regression Using lm() ----------------------------------------
data("swiss")
dat <- swiss
linear_model <- lm(Fertility ~ ., data = dat)
summary(linear_model)
# Call:
# lm(formula = Fertility ~ ., data = dat)
#
@anirudhjayaraman
anirudhjayaraman / graphUndirected.py
Last active February 16, 2021 01:50
Implementing Undirected Graphs in Python
class Vertex:
def __init__(self, vertex):
self.name = vertex
self.neighbors = []
def add_neighbor(self, neighbor):
if isinstance(neighbor, Vertex):
if neighbor.name not in self.neighbors:
self.neighbors.append(neighbor.name)
neighbor.neighbors.append(self.name)
@anirudhjayaraman
anirudhjayaraman / quicksort.py
Last active February 3, 2021 20:01
Python code for the Quick Sort Algorithm
def quicksort(x):
if len(x) == 1 or len(x) == 0:
return x
else:
pivot = x[0]
i = 0
for j in range(len(x)-1):
if x[j+1] < pivot:
x[j+1],x[i+1] = x[i+1], x[j+1]
i += 1