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 / 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 / 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 / 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'
#!/usr/bin/python
class Stack:
def __init__(self):
self.__data=[]
def push(self,x):
self.__data.append(x)
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