This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayDeque; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Deque; | |
import java.util.List; | |
public final class IslandFinder { | |
// Entry point | |
public List<Island> findIslands(boolean[][] map) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Two algos for solving problem-1 at http://challenge.greplin.com/ | |
def getPalindrome1(text): | |
''' | |
A more effecient approach for finding palindromes : | |
Iterate through the text and, | |
Case1: | |
For each character, check if it forms MIDDLE character of a (ODD-LENGTH) palindrome. | |
For ex. Palindrome like rac[e]car, abc[d]cba |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, inspect | |
def getBackTrace(printArgs=None , logger=sys.stdout.write): | |
''' | |
Related blog post - http://42bits.wordpress.com/2011/01/23/pdb-style-function-backtrace-for-python-using-inspect-module/ | |
This decorator method can be used to get GDB style backtraces (Stack trace) of currently | |
active stack frames during the execution of a program, | |
optionally printing positional/variable/keyword arguments for the | |
functions in the outer frames. | |
It can be used to easily trace the function call flow in a running application, | |
without having to use a external debugger like pdb. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prepArray(idxs,array): | |
return [array[x] for x in idxs] #This step is O(n) too | |
def combinations(array,k): | |
""" | |
Code to generate nCk combinations for a n-length array (Won't work with sequences having duplicate values) | |
Maybe not so effecient but my implementation, Don't know about algo implemented in standard library | |
Standard lib - itertools.combinations | |
Just Wrote code to pass the time. :) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import ceil | |
def split(input_list,num_fractions=None,subset_length=None): | |
''' | |
Given a list/tuple split original list based on either one of two parameters given but NOT both, | |
Returns generator | |
num_fractions : Number of subsets original list has to be divided into, of same size to the extent possible. | |
It's possible that number of fractions returned is less than requested (num_fractions value) | |
In case, when you want to split a 6-member list into 4 subsets. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from webbrowser import open | |
import tweepy | |
""" | |
A small python script which given application key, and application secret, asks user to authorize application for Ouath access to his account and prints those settings to be used later. | |
Library Dependencies - twitter's python API interface - tweepy (http://joshthecoder.github.com/tweepy/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bsearch(arr,key,start=0,end=None): | |
if end == None: end = len(arr) - 1 | |
if start > end: return None | |
if start == end and arr[start] != key: return None | |
mid = (start+end)/2 | |
if arr[mid] == key: | |
return mid | |
if arr[mid] > key: | |
return bsearch(arr,key,start,mid-1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randrange | |
def findKMin(arr,k,start=0,end=None): | |
''' | |
Find kth minimum element in a array (in-place randomized algorithm, similar to quicksort) | |
assumption: Input will only contain unique elements''' | |
if k > len(arr): | |
raise Exception("k should be less than length of the input array") | |
if not end: end = len(arr) -1 #Get last index value | |
pivot_ridx = randrange(start,end) #Get a random array element as pivot value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Permute<E> | |
{ | |
/** | |
* Implementation of http://en.wikipedia.org/wiki/Permutation#Systematic_generation_of_all_permutations | |
Algorithm to effeciently generate permutations of a sequence | |
until all possiblities are exhausted | |
*/ |