This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| """ | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| %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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| solutions = [] | |
| def place(positions,col): | |
| if positions is None: | |
| return True | |
| current_row = len(positions) | |
| if col in positions: | |
| return False |
NewerOlder