Skip to content

Instantly share code, notes, and snippets.

@samrat
samrat / bookmarklet.js
Created October 21, 2011 04:01
Videodropper bookmarklet- under progress
if (!($ = window.jQuery)) { // typeof jQuery=='undefined' works too
script = document.createElement('script');
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js';
script.onload = videodropper_bookmarklet;
document.body.appendChild(script);
}
else {
videodropper_bookmarklet();
}
d = document; b = d.body;
@samrat
samrat / csv2json.py
Created September 17, 2011 08:52
Converting CSV file(from Google Spreadsheet) to JSON
# script for converting Google Spreadsheets .CSV file to JSON
import csv
import simplejson as json
f = open('path/to/filename.csv', 'r')
reader = csv.reader(f)
keys = ('Name', 'Address') #replace/add keys here
out = []
out = [dict(zip(keys, property)) for property in reader]
out.remove(out[0]) # removing the first row- keys
@samrat
samrat / guessthenumber.html
Created September 17, 2011 04:18
guess the number game
<html>
<body>
<form name="guess">
<input type="text" name="num">
<input type="button" onclick="checkGuess()" value="Submit">
</form>
<script type="text/javascript">
var answer = Math.floor(Math.random()*11);
@samrat
samrat / euclid_algo.py
Created September 12, 2011 14:56
Euclid's algorithm for finding greatest common divisor implemented in Python
def HCF(a,b):
''' Returns HCF(highest common factor) of a and b
http://en.wikipedia.org/wiki/Euclidean_algorithm
'''
great = max(a,b)
less = min(a,b)
great = great % less
if great == 0:
return less
return HCF(great, less)
@samrat
samrat / loadsave.py
Created September 1, 2011 18:53
Suspends laptop to save power during unexpected power outages(they're quite common in Nepal)
'''Checks the state of AC adapter(online or offline) and suspends the computer if offline'''
#while true ; do python2.7 loadsave.py ; sleep 5m; done
import os
comm = '''dbus-send --system --print-reply --dest="org.freedesktop.UPower" \
/org/freedesktop/UPower org.freedesktop.UPower.Suspend'''
def loadsave():
os.system("acpi -a > powerinfo.txt")
f = open('powerinfo.txt', 'r')
tf = 0
t = 1
(1..100).each do |num|
if num % 3 == 0 and num % 5 == 0
tf += 1
else if num % 3 == 0 and num % 5 != 0
t += 1
end
end
@samrat
samrat / central_limit.py
Created August 31, 2011 07:26
Verifies the Central Limit Theorem for a given Probability Mass Function(PMF)
'''Verifies the Central Limit Theorem for a given Probability Mass Function(PMF)
'''
import Cdf #http://greenteapress.com/thinkstats/Pmf.html
import Pmf #http://greenteapress.com/thinkstats/Cdf.html
from thinkstats import Mean #http://greenteapress.com/thinkstats/thinkstats.html
from matplotlib import pyplot
pmf = Pmf.MakePmfFromDict( {1:3, 3:7, 6:9, 10:4, 5:9, 10:4, 11:5, 12:8} )
cdf = Cdf.MakeCdfFromPmf(pmf)
@samrat
samrat / central_limit_theorem.py
Created August 28, 2011 11:38
Verifying Central Limit Theorem computationally
from random import randint
from matplotlib import pyplot
def gen_mean(sample_size):
sum = 0
for i in range(sample_size):
sum += randint(1, 100)
mean = ( float(sum) / sample_size )
return mean
@samrat
samrat / trapezium.py
Created August 27, 2011 07:40
Definite integration using the trapezium rule
from __future__ import division
def trapezium(f, n, a, b):
'''
f- function [f(x)]
n- number of trapeziums
a- lower limit
b- upper limit
Returns definite integral of f(x) from range a to b
'''
function quicksort(a) {
if (a.length == 0) return [];
var great = [], less = [], pivot = a[0];
for (i=1; i<a.length; i++){
if (a[i]>pivot)
great.push(a[i]);
else {