Skip to content

Instantly share code, notes, and snippets.

@GoWind
GoWind / nqueens.py
Created December 31, 2013 18:30
a simple program for solving the nqueens problem using Python.
import sys
solutions = []
def place(positions,col):
if positions is None:
return True
current_row = len(positions)
if col in positions:
return False
@GoWind
GoWind / createmarkdown
Last active January 2, 2016 04:58
A simple way to create HTML from Markdown
Markdown is a simple language to write create web pages . Markdown is much easier than HTML with the barest of markup ( as opposed to HTML) and lets you focus on the content .
The best way to create your own static website is to create a user.github.io page ( its pretty simple ) and then write your pages in markdown ( *.md files) and let some program generate HTML for you .
Unfortunately , while people on the internet obsess over Markdown , I found it hard to find a simple and easy-to-use tool to generate my static website . The most often cited tool Jekyll and Hugo were overkill for my needs ( and Hugo didn't work in my case for I was unable to generate any html files )
Anyway, back to the topic , i found a nice tool called markdown2 a python based html generator .Its easy to install with
sudo pip install markdown2
(or)
sudo easy_install markdown2
you will need python (2.7 in my case) and python pip or python setup_tools
@GoWind
GoWind / buublesort.erl
Last active January 2, 2016 09:29
Bubble Sort in Erlang
%Bubblesort is pretty non-intuitive in Erlang with its lists and recursive style of programming . Overnight I wrote a bubblesort function in erlang based on the example provided here http://tinyurl.com/bng9ku2 ( link to a pdf with exercises in erlang)
%The code is given as below
%the func prototype exposed
bubblesort(Alist,N) -> bubblesort([],Alist,0,N).
%all items are sorted
bubblesort(Curlist,Alist,X,N) when X == N -> Alist;
%only largest item is in the temp list. In that case move it to the
%mainlist and proceed with the next item
@GoWind
GoWind / coinchange.py
Created March 2, 2014 07:04
Coin change problem - change a given amount into coins of given denominations
d = {}
def coinchange(change , Coinlist):
""" input
change - the amount that is to be changed into coins
Coinlist - list of coins sorted in descending order
output
list of possible combinations for given change
"""
@GoWind
GoWind / huffman.py
Created March 6, 2014 07:33
Huffman encoding in python
# Huffman Encoding is an encoding scheme to compress the size of data .
# http://www.cs.cf.ac.uk/Dave/Multimedia/node210.html
#
# I designed a simple Huffman Prefix tree maker and a Huffman encoder to encode text strings
import sys
from collections import defaultdict
class HufNode:
def __init__(self,char,value):
self.char = char
The edit distance is the minimum number of changes need to transform one sentence into another. For example the edit distance between 'cat' and 'cats' is 1 and between 'rat' and 'hammer' is 6 (change 'r' to 'h', retain the 'a' and then replace 't' with 'mmer').
Calculating the edit distance between 2 words is done by using a dynamic programming approach.
Consider transforming sentence 'arts' to 'artu'.
There are three possible ways to do this
adding a 'u' after 'art' in 'arts'.
deleting the 'u' in 'artu' to get 'art' in 'arts'.
substituting 's' with 'u'
assuming the cost to delete, add or substitute is 1, this can be expressed as
@GoWind
GoWind / number_digits.py
Created July 26, 2014 21:09
get the number of digits in a number
#The normal way to find the digits in a number is to divide it by 10 until it becomes zero and return the count of times this is done
# There is however an easier solution. for any number n, we can find the log of n to the base 10. this will return a decimal value if n #is not a power of 10 or an integer if n = 10 ** k for some k. So the solution to find the number of digits in a number is to find the
# largest power of 10 greater than or equal to n
import math
def digits(n):
n = abs(n)
return int(math.ceil(math.log(n, 10)))
(defmacro reverser
[f]
`(fn [& more#] ;use gensym to prevent variable capture
(apply ~f (reverse more#))))

Keybase proof

I hereby claim:

  • I am GoWind on github.
  • I am govind (https://keybase.io/govind) on keybase.
  • I have a public key whose fingerprint is 18B6 161F 3761 D82E BD15 5AA5 A4E2 7754 FD37 948F

To claim this, I am signing this object:

@GoWind
GoWind / rungkutta.clj
Created December 1, 2016 12:13
A Rung-Kutta 2nd order solver for ordinary differential equations
(ns rungkutta)
;; I used materials from the following website
;; as I found them very easy to understand
;; http://nm.mathforcollege.com/
;; this is a constant, compute this only once so that we save some time
(def g (double (* -2.2067 (Math/pow 10 -12))))
(def k (double (* -81 (Math/pow 10 8))))
(defn rk2