This file contains hidden or 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 longest_common_prefix(str1, str2): | |
result = '' | |
for i in range(len(str1)): | |
if(len(str2) > i and str1[i] == str2[i]): | |
result = result + str1[i] | |
else: | |
break | |
return result | |
def longest_common_substring(str1, str2): |
This file contains hidden or 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 next_greater(arr): | |
while(not is_rev_sorted(arr)): | |
for i in reversed(range(len(arr) - 1)): | |
if arr[i] < arr[i+1]: break | |
for j in reversed(range(len(arr))): | |
if arr[i] < arr[j]: break | |
swap(arr, i, j) | |
yield reverse(arr, i+1) | |
def swap(arr, i, j): arr[i], arr[j] = arr[j], arr[i] |
This file contains hidden or 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
import fileinput | |
def find_min(a, x, length): | |
if length == 0: return float('inf'), -1 | |
if len(a) == (x+1): return 1, 'out' | |
h = reduce(lambda x, y: (a[y], y) if x[0] > a[y] else x, range(x+1, min(len(a), x+1+length)), (float('inf'), -1)) | |
return h[0]+1, h[1] | |
def find_min_hops(a): | |
length = len(a) |
This file contains hidden or 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 print_permutations(str): | |
if(len(str) == 1): return [str] | |
perms = print_permutations(str[1:]) | |
return [x[0:i]+str[0]+x[i:] for x in perms for i in range(len(x)+1)] | |
print print_permutations("abc") |
This file contains hidden or 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
class MyArray { | |
// returns null, if out-of-bounds index, returns value otherwise | |
public Integer get(Integer index); | |
public Integer search(Integer value) { | |
// if(get(0) == value)return 0; | |
int base = 0; | |
while(true) { | |
int index_jump = 1; | |
while(get(base+index_jump) != null && get(base+index_jump) <= value) { |
This file contains hidden or 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
class Node: | |
def __init__(self, nbrs): | |
self.nbrs = nbrs | |
def add_nbr(self, node_nbr): | |
self.nbrs.append(node_nbr) | |
def copy(self): | |
return self.copy_recurse({}) | |
This file contains hidden or 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 strstr(haystack, needle): | |
for i in range(len(haystack) - len(needle) + 1): | |
match = True | |
for j in range(len(needle)): | |
if haystack[i+j] != needle[j]: | |
match = False | |
break | |
if match == True: return i | |
return None | |
This file contains hidden or 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
import sys | |
cache = {} | |
while True: | |
c = sys.stdin.readline() | |
if c == '': break | |
a1, b1 = c.split() | |
a = int(a1); b = int(b1) | |
max_length = 0 | |
a, b = min(a,b), max(a,b) | |
if a < b/2: a = b/2 |
This file contains hidden or 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
#include <iostream> | |
#include <cstdint> | |
using namespace std; | |
int main() { | |
cout<<"Hello World! \n"; | |
//For invalid pointer, uncomment the below line... | |
//string* s = new string[10]; | |
//And comment the following two lines... | |
string* s = (string *) operator new(10 * sizeof(string)); |
This file contains hidden or 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
f = lambda i,j: int("{0:b}".format(i+1).zfill(3)[j]) | |
def func(x, p, a, g): | |
if(p == 0 and a == 0 and g == 0 and x == 0): return 1 | |
if(p < 0 or a < 0 or g < 0 or p+a+g < x or x==0): return 0 | |
l = lambda count, pos: count + func(x-1, p-f(pos,0), a - f(pos,1), g - f(pos,2)) | |
return reduce(l, range(7), 0) | |
print func(3,1,1,3) #9 | |
print func(50,10,10,10) #0 |