Skip to content

Instantly share code, notes, and snippets.

@ashish0x90
ashish0x90 / IslandFinder.java
Created April 27, 2018 18:36
Island finder, contains iterative as well as recursive method with sanity test input.
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) {
@ashish0x90
ashish0x90 / Greplin_problem_1.py
Created March 1, 2011 20:20
My solution for Greplin_problem_1 as a reply to this comment at HN - http://news.ycombinator.com/item?id=2276473
#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
@ashish0x90
ashish0x90 / getBackTrace.py
Created January 21, 2011 18:41
This decorator method can be used to get GDB style backtraces (Stack trace) of currently active stack frames during the execution of a python program, optionally printing positional/variable/keyword arguments for the functions in the outer frames.
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.
@ashish0x90
ashish0x90 / combinations.py
Created November 22, 2010 19:33
Code to generate nCk combinations for a n-length array (Won't work with sequences having duplicate values)
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. :)
@ashish0x90
ashish0x90 / sequence_split.py
Created October 22, 2010 11:08
Splits a sequence and returns a generator having split subsequences - See doc string for further info.
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.
@ashish0x90
ashish0x90 / get_twitter_oauth_userkeys.py
Created October 19, 2010 07:28
A quick python script which for Twitter Oauth system Given application key, application secret, asks user to authorize the application to enable Ouath access to his account by the application and prints those settings after process is over to be used lat
#!/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/)
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)
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
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
*/