Skip to content

Instantly share code, notes, and snippets.

View rohit-jamuar's full-sized avatar

Rohit rohit-jamuar

  • Intel
  • Austin, TX
View GitHub Profile
@rohit-jamuar
rohit-jamuar / numeric_converter.py
Last active August 29, 2015 14:04
Converts an integer (between 0 and 10,000) into its alphabetical form.
#!/usr/bin/python
ONES = {1 : 'one', 2 : 'two', 3 : 'three', 4 : 'four', 5 : 'five',
6 : 'six', 7 : 'seven', 8 : 'eight', 9 : 'nine'}
ODDS = {10 : 'ten', 11 : 'eleven', 12 : 'twelve', 13 : 'thirteen',
14 : 'fourteen', 15 : 'fifteen', 16 : 'sixteen', 17 : 'seventeen',
18 : 'eighteen', 19 : 'nineteen'}
TENS = {2 : 'twenty', 3 : 'thirty', 4 : 'forty', 5 : 'fifty',
6 : 'sixty', 7 : 'seventy', 8 : 'eighty', 9 : 'ninety'}
@rohit-jamuar
rohit-jamuar / number_counting.py
Created July 31, 2014 05:56
Count of numbers (in range [1,input_num]) which don't have 'exclude'
#!/usr/bin/python
def number_counting(input_num, exclude):
'''
Returns the count of numbers (in range [1,input_num]) which don't
have 'exclude' among their constituent digits.
'''
if type(input_num) == int and type(exclude) == int:
exclude = str(exclude)
if len(exclude) == 1:
@rohit-jamuar
rohit-jamuar / prime_number_generator.py
Last active August 29, 2015 14:04
Prime number generator
#!/usr/bin/python
from math import ceil
def prime_number_generator():
'''
Prime number generator.
Create an object, keep calling next() to fetch next prime.
'''
yield 2
@rohit-jamuar
rohit-jamuar / anagram_grouper.py
Last active August 29, 2015 14:04
Groups anagrams in lists (without sorting)
#!/usr/bin/python
from prime_number_generator import prime_number_generator
from operator import mul
from string import lowercase
P_GEN = prime_number_generator()
REF = { elem : P_GEN.next() for elem in lowercase }
def anagram_grouper(input_list):
@rohit-jamuar
rohit-jamuar / palindrome_partition.py
Last active August 29, 2015 14:05
Find all palindromic substrings
#!/usr/bin/python
def get_substrings(input_str):
'''
Yields all substrings of size in range [1, len(input_str)]
'''
for step in range(1, len(input_str)+1):
temp1 = []
for i in range(len(input_str)):
temp2 = input_str[i:i+step]
@rohit-jamuar
rohit-jamuar / get_subsequences.py
Last active August 29, 2015 14:05
Yields all subsequences of input string
#!/usr/bin/python
from itertools import combinations
def get_subsequences(input_str):
'''
Yields all subsequences of size in range [1, len(input_str)]
'''
if type(input_str) == str:
for length in range(1, len(input_str)+1):
@rohit-jamuar
rohit-jamuar / generate_alpha_combinations.py
Last active August 29, 2015 14:16
Generates all combinations of case-alternating alphabets from string argument
def generate_combinations(s):
'''
Generates all combinations of case-alternating alphabets from string 's'
for e.g. if 's' was '0abc', the function would output:
0abc
0abC
0aBc
0aBC
0Abc
0AbC
@rohit-jamuar
rohit-jamuar / binary_tree.py
Last active August 29, 2015 14:18
Binary Tree with a print generator
class BiNode(object):
'''
A binary tree node
'''
def __init__(self, val):
self.value = val
self.left, self.right = None, None
@rohit-jamuar
rohit-jamuar / combine_permute.py
Created March 30, 2015 21:42
Find all combinations / permutations of a string
def combine(s):
if len(s) <= 1:
return {'', s}
last_char = s[-1]
result = combine(s[:-1])
intermediate = {i for i in result}
for item in result:
intermediate.add(item + last_char)
return list(intermediate)
@rohit-jamuar
rohit-jamuar / radix_sort.py
Created April 8, 2015 20:16
O(kn) integer sorting
def max_num_len(arr):
x = max(arr)
count = 0
while x > 0:
x /= 10
count += 1
return count
def radix_sort(arr):