Skip to content

Instantly share code, notes, and snippets.

View prat0318's full-sized avatar
💭
Ubering on.

Prateek Agarwal prat0318

💭
Ubering on.
View GitHub Profile
@prat0318
prat0318 / next.py
Last active December 28, 2015 13:39
next greater in array
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]
@prat0318
prat0318 / lcs.py
Created November 17, 2013 04:12
sub optimal longest common substring
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):
@prat0318
prat0318 / encode.py
Created November 12, 2013 03:49
encode strings A->1...Z->26 ...total combinations possible by reading input string
def encoding_ways(str):
if((len(str) == 1) or (len(str) == 0)): return 1
total1 = encoding_ways(str[1:])
if((str[0] == '1') or ((str[0] == '2') and (1 <= int(str[1])) and (int(str[1]) <= 6))):
total1 = total1 + encoding_ways(str[2:])
return total1
print encoding_ways('12')
@prat0318
prat0318 / pivot.py
Created November 11, 2013 07:02
pivot position correction
def swap(arr, a, b):
temp = arr[a]; arr[a] = arr[b]; arr[b] = temp
def correct_pivot_pos(arr):
pivot = 0
if len(arr) < 2 : return arr
start = 1; end = len(arr) - 1
while(start < end):
while((start < len(arr)) and (arr[start] < arr[pivot])): start+= 1
while((end > 0) and (arr[end] > arr[pivot])): end -= 1
@prat0318
prat0318 / reverse.py
Created November 11, 2013 03:29
reverse words in sentence
def reverse(string, start, end):
rev = ""
for i in range(0, end - start +1):
rev += string[end - i]
return rev
def reverse_words(sent):
rev_sent = reverse(sent, 0, len(sent)-1)
rev_words = ""; i = 0; j = i
while(True):
@prat0318
prat0318 / linked_list.py
Created November 10, 2013 19:16
single linked list with insert, pop and print operations
class node:
def __init__(self, val):
self.value = val
self.next = None
class linked_list:
def __init__(self):
self.head = None
def insert(self, value):
@prat0318
prat0318 / find_min_of_rotated.py
Created November 10, 2013 18:05
rotate and find_min
def swap(a, b):
temp = a
a = b
b = temp
return (a,b)
def rotate(arr, by):
if len(arr)%by == 0:
for i in range(by):
start, pos = arr[i], i+by
@prat0318
prat0318 / sublength.py
Created November 10, 2013 03:26
lowest substring of a containing all characters of string b
def sublength(a, b):
hash_b = {}
for char in set(b):
hash_b[char] = -1
min_length = len(a)+1
for i, char in enumerate(a):
if char in hash_b:
hash_b[char] = i
if (min(hash_b.values()) != -1) & (i - min(hash_b.values()) + 1 < min_length) :
print hash_b
@prat0318
prat0318 / justify2.py
Last active December 27, 2015 19:19
Beautification EDIT: small fix: distributing last buffer among all
string = "Hellooooooooooooooooooooooooo thiss is an example of the project to justify the code and we want a biggggggg word also."
l=25
start=0
length = len(string)
while(len(string[start:]) > l):
line = string[start:start+l]
if line.find(' ') == -1:
def isInt(str):
if len(str) < 1:
return False
if str[0] == '-':
str = str[1:]
dot = False
for c in str:
if c == '.' and dot == True:
return False
if c == '.' and len(str) > 1: