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
def foo(arr):
from sys import maxint
currentSum,maxSum=0,-maxint-1
for i in arr:
currentSum+=i
if currentSum<0:
currentSum=0
elif currentSum>maxSum:
maxSum=currentSum
return maxSum
#!/usr/bin/python
class Stack:
def __init__(self):
self.__data=[]
def push(self,x):
self.__data.append(x)
@rohit-jamuar
rohit-jamuar / balanced_delimiters.py
Last active September 7, 2023 19:27
balanced_delimiters
#!/usr/bin/python
def balanced_delimiters(s):
'''
This function determines if the input string ('s') has balanced number of braces.
All the opening braces are appended to 'data'. As soon as a closing brace is
encountered, the last entry in 'data' is matched with it. If the closing brace is
the opposite of the last element in 'data' - opening braces and their corresponding
closing braces are stored in 'braces', we have a match and the the last element
is popped-off 'data'. This process is continued till either all the elements of 's'
@rohit-jamuar
rohit-jamuar / binary_search.py
Last active August 29, 2015 14:03
Binary Search
def binary_search(arr, val, algo='iterative'):
'''
Wrapper function for binary search routine. By default, choses the iterative
variant of the binary search algorithm.
Input : Sorted Array
Output: Index of 'val' being searched is printed on console. If not found, -1
is printed.
'''
@rohit-jamuar
rohit-jamuar / get_n_th_fibo.py
Last active August 29, 2015 14:03
Function to get the nth Fibonacci term in linear time and constant space.
def get_n_th_fibo(n):
'''
Returns the nth term of the fibonacci sequence - {0, 1, 1, 2, ...}
F(n) = F(n-1) + F(n-2) where n>=2
F(0) = 0
F(1) = 1
'''
if n == 0:
return 0
@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
@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 / 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 / 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 / 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.
'''