Skip to content

Instantly share code, notes, and snippets.

@puneetlakhina
puneetlakhina / reversestr.py
Created October 20, 2011 07:31
Python reverse a string
#!/usr/bin/python
import sys
s = sys.argv[1]
n=len(s)
print ''.join([s[i] for i in range(n-1,-1,-1)])
@puneetlakhina
puneetlakhina / reversestraltupper.py
Created October 20, 2011 07:33
Python reverse a string with alternate letter capitalization
#!/usr/bin/python
import sys
s = sys.argv[1]
n=len(s)
print ''.join([s[i] if i%2 == 0 else s[i].upper() for i in range(n-1,-1,-1)])
@puneetlakhina
puneetlakhina / toutc.pl
Created November 7, 2011 22:32
Convert localtime to unix timestamp
#!/usr/bin/perl
#Format of ip Month/Year/DateThour:minute:seconds
use Time::Local;
my ($date,$time) = split 'T' , $ARGV[$1];
#print $date."\n".$time."\n";
my ($month, $day, $year) = split '/', $date;
$month = $month - 1; #this is because timelocal wants the month to be zero indexed
#print $month."\n";
my ($hr, $
@puneetlakhina
puneetlakhina / useful.sh
Created November 8, 2011 17:53
Useful command lines
netstat -tuanp | grep LISTEN | grep (port number) #listening ports using netstat #use -p to list process id also
@puneetlakhina
puneetlakhina / makengram.py
Created November 30, 2011 10:09
Make n grams from a list of words
def ngram(words,n):
return ["_".join(words[i:i+n]) for i in range(0,len(words)-n+1)]
@puneetlakhina
puneetlakhina / CollectionUtils.java
Created December 2, 2011 20:01
A utility of collection functions. 1. Denormalize a java Map<K,List<T>> into a list of entries 2. firstOrNull in a list
private <K,T> List<Map.Entry<K, T>> denormalizeMap(Map<K,List<T>> nestedListMap) {
List<Map.Entry<K, T>> denormalizedEntryList = new ArrayList<Map.Entry<K,T>>();
for(Map.Entry<K, List<T>> entry:nestedListMap.entrySet()) {
for(T t:entry.getValue()) {
denormalizedEntryList.add(new AbstractMap.SimpleEntry<K,T>(entry.getKey(), t));
}
}
return denormalizedEntryList;
}
@puneetlakhina
puneetlakhina / git_set_upstream.sh
Created December 6, 2011 02:42
set upstream git branch
git branch --set-upstream branch_name upstream/branch_name
Test Gist
@puneetlakhina
puneetlakhina / lpr.py
Created April 10, 2012 06:44
Multiply probabilities by doing log sum
import math
def lpm(*args):
return sum([math.log(x) for x in args])
lpm(0.1,0.2)
public void open() {
new Thread() {
public void run() {
loadData((Schema)null, _file);
}
}.start();
}
@Override
protected boolean processRecord(GenericRecord data) {