Python Snippets

Count all uppercase letters in a file.

with open('./test_text.txt') as fh:
	count = sum(1 for c in fh.read() if c.isupper())
	print("The # of capital letters in file", str(count))

Implement a stack with 2 queues

two_stacks_queue.py
#  Implement A Queue using Two Stacks Python

class Queue(object):
    def __init__(self):
        self.instack=[]
        self.outstack=[]
    def enqueue(self,element):
        self.instack.append(element)
    def dequeue(self):
        if not self.outstack:
            while self.instack:
                self.outstack.append(self.instack.pop())
        return self.outstack.pop()
q=Queue()
for i in range(10):
    q.enqueue(i)
for i in xrange(10):
    print q.dequeue(),

Check for duplicate files

Read JSON file

Return nth Fibonacci sequence

Minimum Window Substring

Rainwater Problem

House Robber Problem

URL shortener example

Multiple Hash Function

Last updated

Was this helpful?