Skip to content

Instantly share code, notes, and snippets.

View vaishaks's full-sized avatar

Vaishak Salin vaishaks

View GitHub Profile
@vaishaks
vaishaks / remember_me.php
Created July 2, 2012 15:18
Trying out sessions in PHP
<?php
session_start();
?>
<html>
<head></head>
<body>
<?php
if (!isset($_SESSION['name']) && !isset($_POST['name'])) {
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
@vaishaks
vaishaks / postfixtoprefix.cpp
Created August 31, 2012 13:38
C++ code to convert postfix expression to prefix
#include <iostream>
#include <cstring>
#include <stack>
#include <algorithm>
#define flag '#'
using namespace std;
bool isOperator(char c)
{
@vaishaks
vaishaks / tutorial.markdown
Created October 20, 2012 16:08
Open Source Plugin Tutorial

DuckDuckGo plugins react to search queries and provide useful instant answers above traditional links.

A plugin line-by-line

In this tutorial, we'll be making a plugin that checks the number of characters in a given search query. Then end result will look like this and works like this. It's in Perl though the meat of some plugin types can be written in other languages (see Plugin types).

Let's begin. Open a text editor like gedit, notepad or emacs and type the following.

package DDG::Goodie::Chars;
#!/usr/bin/env python
__author__ = "vaishaks <[email protected]>"
__date__ = "Mar 25, 2013"
import sys
from collections import defaultdict
emission_counts = defaultdict(float)
unigram_counts = defaultdict(float)
import fractions
T = input()
dim = []
for i in xrange(0, T):
dim.append(raw_input().split())
for i in dim:
sq = fractions.gcd(int(i[0]), int(i[1]))
print (int(i[0])*int(i[1]))/(sq*sq)
#!/usr/bin/env python
def sort_and_count(A, n):
if n == 1:
return (A, 0)
else:
(B, x) = sort_and_count(A[:n/2], len(A[:n/2]))
(C, y) = sort_and_count(A[n/2:], len(A[n/2:]))
(D, z) = merge_and_count_splitInv(B, C, n)
return (D, x+y+z)
@vaishaks
vaishaks / inv.py
Last active December 19, 2015 18:08
def inv(arr,n):
if n==1:
return 0
else:
x=inv(arr[0:n/2],n/2)
y=inv(arr[n/2:n],n/2)
z=split(arr[0:n/2],arr[n/2:n])
return x+y+z
def split(a,b):
#!/usr/bin/env python
from random import randint
from copy import deepcopy
def find_mincut(n, e):
mincut = len(e)
N = 2000
for x in range(N):
nodes = deepcopy(n)
def dfs(graph, node):
"""Run DFS through the graph from the starting
node and return the nodes in order of finishing time.
"""
stack = [[node, True]]
while True in [x[1] for x in stack]:
i = 0
for x in xrange(len(stack)):
if stack[x][1] == True:
i = x
@vaishaks
vaishaks / quicksort.scm
Created November 22, 2013 19:47
Quicksort in scheme. It's beautiful! :'(
(define less
(lambda (L x)
(cond ((null? L) '())
((< (car L) x) (cons (car L) (less (cdr L) x)))
(else (less (cdr L) x)))))
(define great
(lambda (L x)
(cond ((null? L) '())
((> (car L) x) (cons (car L) (great (cdr L) x)))