Skip to content

Instantly share code, notes, and snippets.

View abhin4v's full-sized avatar

Abhinav Sarkar abhin4v

View GitHub Profile
@abhin4v
abhin4v / random_cow_fortune.sh
Created April 8, 2011 11:15
Generates a random fortune spoken by a random cow
fortune -s -n 100 | cowsay -f `ls -1 /usr/share/cowsay/cows/ | replace ".cow" "" | tail -n +\`echo $(( 1 + (\\\`od -An -N2 -i /dev/random\\\`) % (\\\`ls -1 /usr/share/cowsay/cows/ | wc -l\\\`) ))\` | head -1`
@abhin4v
abhin4v / echo-server.py
Created May 24, 2011 08:49
A simple echoing HTTP server with HTTP-Basic-Auth in Python Flask
from functools import wraps
from flask import Flask, request, Response
app = Flask(__name__)
def check_auth(username, password):
"""This function is called to check if a username /
password combination is valid.
"""
return username == 'username' and password == 'pass'
@abhin4v
abhin4v / reconstruct.hs
Created December 19, 2011 17:44
Reconstruct a binary tree from its pre and post order traversals
data Tree a = NullNode | Node a (Tree a) (Tree a)
instance (Show a) => Show (Tree a) where
show NullNode = ""
show (Node x NullNode NullNode) = show x
show (Node x l r) = show x ++ "(" ++ show l ++ " " ++ show r ++ ")"
preOrder :: Tree a -> [a]
preOrder NullNode = []
preOrder (Node x l r) = x : (preOrder l ++ preOrder r)
@abhin4v
abhin4v / shreds.txt
Created December 21, 2011 08:33
An attempt to solve the Unshredding problem from the AI class NLP quiz at http://www.youtube.com/watch?v=KuSg1wcty3s using Genetic Algorithm
|de| | f|Cl|nf|ed|au| i|ti| |ma|ha|or|nn|ou| S|on|nd|on|
|ry| |is|th|is| b|eo|as| | |f |wh| o|ic| t|, | |he|h |
|ab| |la|pr|od|ge|ob| m|an| |s |is|el|ti|ng|il|d |ua|c |
|he| |ea|of|ho| m| t|et|ha| | t|od|ds|e |ki| c|t |ng|br|
|wo|m,|to|yo|hi|ve|u | t|ob| |pr|d |s |us| s|ul|le|ol|e |
| t|ca| t|wi| M|d |th|"A|ma|l |he| p|at|ap|it|he|ti|le|er|
|ry|d |un|Th|" |io|eo|n,|is| |bl|f |pu|Co|ic| o|he|at|mm|
|hi| | |in| | | t| | | | |ye| |ar| |s | | |. |
@abhin4v
abhin4v / sumtree.hs
Created December 28, 2011 11:28
Create a sumtree given the value of root node and depth of tree
data Tree a = NullNode | Node a (Tree a) (Tree a)
instance (Show a) => Show (Tree a) where
show NullNode = ""
show (Node x NullNode NullNode) = show x
show (Node x l r) = show x ++ "(" ++ show l ++ " " ++ show r ++ ")"
sumtree :: Int -> Int -> Tree Int
sumtree rootVal depth
@abhin4v
abhin4v / KthSmallest.hs
Created January 10, 2012 18:07
find kth smallest unique substring of a list of strings
-- You are given n strings w1, w2, ..., wn. Let Si denote the set of strings formed by
-- considering all unique substrings of the string wi. Let S = {S1 U S2 U .... Sn} i.e.
-- S is a set of strings formed by considering all the unique strings in all sets
-- S1, S2, ..., Sn. You will be given many queries and for each query, you will be given
-- an integer k. Your task is to output the lexicographically kth smallest string from
-- the set S.
import Data.List
import Control.Monad
import Control.Exception
@abhin4v
abhin4v / CoderWeeklyToInstaper.py
Created July 3, 2012 07:53
Python script to push latest Coder Weekly article links to Instapaper
from bs4 import BeautifulSoup
import requests
from urlparse import urlparse
import sys
if len(sys.argv) != 3:
print "Usage: python CoderWeeklyToInstapaper.py <instapaper_username> <instapaper_password>"
sys.exit()
else:
(_, username, password) = sys.argv
@abhin4v
abhin4v / HackerMonthlyToInstapaper.py
Created July 3, 2012 13:53
Python script to push latest Hacker Monthly article links to Instapaper
from bs4 import BeautifulSoup
import requests
import sys
if len(sys.argv) != 3:
print "Usage: python HackerMonthlyToInstapaper.py <instapaper_username> <instapaper_password>"
sys.exit()
else:
(_, username, password) = sys.argv
@abhin4v
abhin4v / non_dominated_points.py
Created July 5, 2012 21:19
Given a set of points, find all the points which are not dominated by any other point
# A point A is said to be dominating another point B
# iff A.x > B.x and A.y > B.y.
# Given a set of points, find all the points which are
# not dominated by any other point.
#
# This program used a Quad Tree to solve this problem
# in O(kn) time where n is number of input points and
# k = max(log2(x_range/min_x_dist), log2(y_range/min_y_dist)) where
# x_range = range of x coordinates of the points,
# y_range = range of y coordinates of the points,
@abhin4v
abhin4v / HaskellCC.py
Created August 22, 2012 10:44
Watches current directory and compiles the haskell source files when they are modified
# Usage: python HaskellCC.py (Ctrl-C for exit)
import time
from datetime import datetime
import subprocess
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
class HaskellContinuousCompiler(PatternMatchingEventHandler):