Skip to content

Instantly share code, notes, and snippets.

View praveenvvstgy's full-sized avatar

Praveen Gowda I V praveenvvstgy

View GitHub Profile
@praveenvvstgy
praveenvvstgy / queue-with-stack.py
Created November 10, 2014 11:35
Implement a queue with 2 stacks . Your queue should have an enqueue and a dequeue function and it should be "first in first out" (FIFO). Optimize for the time cost of m function calls on your queue. These can be any mix of enqueue and dequeue calls. Assume you already have a stack implementation and it gives O(1) time push and pop.
stack1 = []
stack2 = []
def enqueuue(a):
stack1.append(a)
def dequeue():
if len(stack2) > 0:
return stack2.pop()
elif len(stack2) == 0 and len(stack1) > 0:
@praveenvvstgy
praveenvvstgy / balanced_paranthesis.py
Created November 10, 2014 12:16
Check for balanced parentheses in an expression
def braces_validator(string):
openers = "({["
closers = ")}]"
stack = []
for char in string:
if char in openers:
stack.append(openers.index(char))
elif char in closers:
if len(stack) == 0 or stack.pop() != closers.index(char):
return False
@praveenvvstgy
praveenvvstgy / matching-parens.py
Created November 10, 2014 15:46
Write a function that, given a sentence, along with the position of an opening parenthesis, finds the corresponding closing parenthesis.
openers = "({["
closers = ")}]"
def paranthesis_match(string, pos):
stack = [string[pos]]
for i in range(pos + 1, len(string)):
if string[i] in openers:
stack.append(string[i])
if string[i] in closers:
stack.pop()
@praveenvvstgy
praveenvvstgy / thumb.sh
Created April 5, 2015 16:55
Create a thumbnail using ImageMagick
for file in `find . -type f -name "*.jpg"`
do
thumbnail_file="./thumbnail/${file##*/}"
if [ ! -e "${file%/*}/thumbnail" ]; then mkdir -p "${file%/*}/${thumbnail_dir}"; fi
echo "Create thumbnail for image $file"
`convert -thumbnail x300 $file $thumbnail_file`
done