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 / 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 / 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 / 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 / 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 / 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 / set_combination.py
Created July 31, 2014 05:50
Prints all possible combinations of words from input lists; maintains order
#!/usr/bin/python
from itertools import product
def set_combination(list_of_lists):
'''
Prints out all possible combinations of words (each combination only once),
where one word is taken from each list, in the same order the lists are
given.
'''
@rohit-jamuar
rohit-jamuar / rot13.py
Created July 31, 2014 05:48
ROT13 encoder/decoder
#!/usr/bin/python
from string import lowercase, uppercase
ROTATED_LOWERCASE = lowercase[13 : ] + lowercase[ : 13]
ROTATED_UPPERCASE = uppercase[13 : ] + uppercase[ : 13]
REF = {}
for index1 in range(len(lowercase)):
REF[lowercase[index1]] = ROTATED_LOWERCASE[index1]
@rohit-jamuar
rohit-jamuar / anagram_count.py
Last active August 29, 2015 14:04
Number of anagrams in a file
#!/usr/bin/python
'''
Anagram count
Assumption - One word per line
Time complexity - O(n*k*log(k)); where n is the total number of
lines and k is the maximum length of word.
@rohit-jamuar
rohit-jamuar / tick_tack_toe_verifier.py
Created July 22, 2014 03:04
Tick-Tack-Toe Verifier
def tick_tack_toe_verifier(game_result):
'''
Input : A nXn grid ('game_result')
Output : Checks the status of game in 'game_result'. If there is a winner
it return 'X' or 'O', else in case of a draw, it returns 'D'.
'''
for r in range(len(game_result)):
x, o = 0, 0
for c in range(len(game_result)):
if game_result[r][c] == 'X':
@rohit-jamuar
rohit-jamuar / fibonacci_sequence_generator.py
Last active August 29, 2015 14:03
Fibonacci sequence generator
def fibo_num_generator():
'''
Create an instance of 'fibo_num_generator' to use it as a generator -
e.g. g = fibo_num_generator()
In order to get the next element in the sequence --> g.next()
'''
yield 0
yield 1
yield 1
a, b = 1, 1