This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
class Stack: | |
def __init__(self): | |
self.__data=[] | |
def push(self,x): | |
self.__data.append(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
NewerOlder