View Greplin_problem_1.py
#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
View getBackTrace.py
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.
View combinations.py
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. :)
View sequence_split.py
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.
View get_twitter_oauth_userkeys.py
#!/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/)
View bsearch.py
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)
View find_kmin.py
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
View Permute.java
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
*/