Skip to content

Instantly share code, notes, and snippets.

@vishesh
vishesh / set.rkt
Last active August 29, 2015 14:13
; Purely functional implementation of Set data structure
(define set-init
(lambda (x) #false))
(define (set-add set x)
(lambda (y)
(if (= x y)
#true
(set y))))
@vishesh
vishesh / lis.ss
Created March 17, 2015 01:39
longest increasing subsequence
(define (seq lst last)
(if (empty? lst)
0
(if (< last (first lst))
(max (add1 (seq (rest lst) (first lst)))
(seq (rest lst) last))
(seq (rest lst) last))))
@vishesh
vishesh / way2sms.py
Created September 13, 2011 01:33
Script to send SMS via way2sms
#!/usr/bin/env python
#
# Script to send SMS via way2sms
#
# @author Vishesh Yadav
# @license BSD License
#
#TODO: Phonebook
@vishesh
vishesh / lcs.py
Created December 12, 2011 21:34
Longest Common Subsequence
#!/usr/bin/python2
#
# Longest Common Subsequence problem
# Just implements simple lcs function. One solution, not all.
def lcs(x, y):
if len(x) == 0 or len(y) == 0:
return ""
elif x[-1:] == y[-1:]:
return lcs(x[:-1], y[:-1]) + x[-1:]
@vishesh
vishesh / thread_decorator.py
Created December 17, 2011 22:55
Decorator to make threads.
import threading
def threadify(func):
def run(*fargs, **fkwargs):
t = threading.Thread(target=func, args=fargs, kwargs=fkwargs)
t.start()
return run
@threadify
def function_a():
@vishesh
vishesh / billboard.py
Created January 24, 2012 10:23
Facebook Billboard Problem
#!/usr/bin/env python
#
# BillBoard problem solution
# Vishesh Yadav
#
# Note: Lot many easy optimizations can be made, but for the sake of
# clarity, let it be.
#
from math import sqrt
@vishesh
vishesh / timDiff.js
Created February 12, 2012 19:55
Time Difference in format 'HOUR:MONTH'
/**
* Time Difference
*
* function : timeDiff(t1, t2)
* timeformat : 'HOUR:MINUTES'
*
*/
function timeInHours(str)
{
@vishesh
vishesh / example-user.js
Created May 4, 2012 12:31 — forked from nijikokun/example-user.js
Beautiful Validation... Why have I never thought of this before?!
var user = {
validateCredentials: function (username, password) {
return (
(!(username += '') || username === '') ? { error: "No Username Given.", field: 'name' }
: (!(username += '') || password === '') ? { error: "No Password Given.", field: 'pass' }
: (username.length < 3) ? { error: "Username is less than 3 Characters.", field: 'name' }
: (password.length < 4) ? { error: "Password is less than 4 Characters.", field: 'pass' }
: (!/^([a-z0-9-_]+)$/i.test(username)) ? { error: "Username contains invalid characters.", field: 'name' }
: false
);
@vishesh
vishesh / pkgsrc-vim-enable-python.diff
Created May 28, 2012 21:12
pkgsrc/editors/vim-share: enable python extension
Index: Makefile.common
===================================================================
RCS file: /cvsroot/pkgsrc/editors/vim-share/Makefile.common,v
retrieving revision 1.143
diff -u -p -r1.143 Makefile.common
--- Makefile.common 31 Oct 2010 23:50:51 -0000 1.143
+++ Makefile.common 13 May 2012 10:52:19 -0000
@@ -76,7 +76,7 @@ ALTERNATIVES_SRC= ${.CURDIR}/../../edito
ALTERNATIVES_SRC=
.endif
@vishesh
vishesh / latency.txt
Created May 31, 2012 13:37 — forked from jboner/latency.txt
Latency numbers every programmer should know
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns
Mutex lock/unlock 25 ns
Main memory reference 100 ns
Compress 1K bytes with Zippy 3,000 ns
Send 2K bytes over 1 Gbps network 20,000 ns
Read 1 MB sequentially from memory 250,000 ns
Round trip within same datacenter 500,000 ns
Disk seek 10,000,000 ns