#Two algos for solving problem-1 at
def getPalindrome1(text):
A more effecient approach for finding palindromes :
Iterate through the text and,
For each character, check if it forms MIDDLE character of a (ODD-LENGTH) palindrome.
For ex. Palindrome like rac[e]car, abc[d]cba
import sys, inspect
def getBackTrace(printArgs=None , logger=sys.stdout.write):
Related blog post -
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.
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. :)
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.
#!/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 (
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
Algorithm to effeciently generate permutations of a sequence
until all possiblities are exhausted