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 / paren_matching.py
Last active December 27, 2015 05:39
Usage: paren(0,4,'')
#Usage: paren(0,4,'')
def paren(sum,remaining,str):
if sum < 0:
return
if remaining == 0:
print str+')'*sum
return
paren(sum+1,remaining-1,str+'(')
paren(sum-1,remaining,str+')')
def findOdd(arr):
index = 0
count = 1
last_char = arr[index]
while(index < len(arr)):
if(arr[index] != last_char):
break
count+=1
index+=1
count2 = 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:
@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:
@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 / 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 / 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 / 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 / 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 / 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')