Skip to content

Instantly share code, notes, and snippets.

View svalleru's full-sized avatar

Shan Valleru svalleru

View GitHub Profile
@svalleru
svalleru / algorithmia.json
Created July 6, 2017 22:49
Algorithmai's Analyze Twitter User Algorithm (mostly rubbish ;P)
#https://algorithmia.com/algorithms/diego/AnalyzeTwitterUser
{
"followers": 340,
"following": 415,
"is negative about": [
{
"country": 1,
"evil": 1,
"kumailn": 2,
@svalleru
svalleru / Denominator.py
Created June 15, 2016 23:53
Calculate lowest possible denominations for given change
denominations = [25, 10, 1, 100]
change = 66
for d in sorted(denominations, reverse=True):
q, r = divmod(change, d)
print d, '*', q
change = r
# 100 * 0
# 25 * 2
@svalleru
svalleru / Graphy.py
Created June 15, 2016 18:30
Graph Traversal
# A -> B
# A -> C
# B -> C
# B -> D
# C -> D
# D -> C
# E -> F
# F -> C
@svalleru
svalleru / TwoSum.py
Last active June 20, 2016 22:16
find if the sum of any two numbers in a list equates to a given number
class TwoSum(object):
"""
find if the sum of any two numbers in a list equates
to a given number
"""
def __init__(self):
self.nums = []
self.sum = 0
def add_num(self, num):
@svalleru
svalleru / Ramifier.py
Last active February 18, 2017 17:19
Python Pool Multiprocessing
from multiprocessing import Pool
import functools
def _myfunc_with_one_arg(n):
return n ** 2
def _myfunc_with_two_args(arg1, arg2, s):
return arg1 + arg2 + s
def ramifier():
@svalleru
svalleru / DictionaryInPython.py
Last active March 15, 2016 03:33
Pure python implementation of a dictionary of fixed length
__author__ = 'svalleru'
"""If 'YOU' can't solve it, you can't ask the interviewee to solve it ;)"""
class Dictionary(object):
"""Pure python implementation of a dictionary of fixed length"""
def __init__(self, size):
self.size = size
self.data = [[] for _ in xrange(size)]
#hash method
@svalleru
svalleru / ObserverPattern.py
Last active April 6, 2016 06:12
Observer Design Pattern in Python
__author__ = 'svalleru'
class Observable(object):
def __init__(self, name):
self._name = name
self._observers = []
def register_observer(self, observer):
self._observers.append(observer)
def notify_observers(self, msg):
for observer in self._observers:
__author__ = 'svalleru'
# simple script for network latency debugging
import time
from optparse import OptionParser
from urllib2 import Request, urlopen
def url_fetcher(url, count):
print "trying url:", url, "with retry count:", count
for attempt in range(1, count):
if attempt > 1:
@svalleru
svalleru / PythonIdioms.py
Last active April 6, 2016 06:13
Python Idioms
__author__ = 'svalleru'
# Monkey patching
class Spam(object):
def __init__(self, name):
self._name = name
def beans(self):
print "Good morning, %s. Here are your delicious fried beans." % (self._name,)
# sorts a dictionary based on value
from collections import OrderedDict
from random import choice, randint
import string
# generate random upper case string of given length
def generateRandomString(len):
return ''.join(choice(string.ascii_uppercase + string.digits) for _ in range(len))
#create a dict with arbitrary K-Vs