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 |
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
#!/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
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
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 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 |
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 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': |
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 | |
''' | |
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. |
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 | |
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] |
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 | |
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. | |
''' |
OlderNewer