Monday, September 30, 2013

Simple Paint Application using HTML5 Canvas


         Paint application is used to draw curves,pictures,import images,etc..Here is a simple Paint App i designed for beginners.This done using HTML and javaScript.Canvas(drawing area) created using Html tags.That is Picturising the window is done by Html tags and the functions done using javaScript.


         JavaScript insert elements from documents using:
                  document.getElementById(id)

   This App includes mouse events.For eg:

   My Paint App View:


     Canvas and buttons are designrd using HTML where the functions are done with JavaSsript.

MouseEvents:


 function events(){b.onmousedown=md;b.onmouseup=mu;b.onmousemove=mv;}  
 function md(e){img=c.getImageData(0,0,b.width,b.height);sX=e.x;sY=e.y;pulse="on";}  
 function mu(e1){eX=e1.x;eY=e1.y;pulse="off";}  
 function mv(e2){mX=e2.x;mY=e2.y;if (pulse=="on"){c.putImageData(img,0,0);draw();}}  

LineTool:


 c.beginPath();c.moveTo(sX,sY);c.lineTo(mX,mY);c.stroke();c.closePath();  

RectangleTool:


 c.strokeRect(sX,sY,mX-sX,mY-sY);c.stroke();if(f==1){c.fillRect(sX,sY,mX-sX,mY-sY);  

CircleTool:


 c.beginPath();c.arc(Math.abs(mX+sX)/2,Math.abs(mY+sY)/2,Math.sqrt(Math.pow(mX-sX,2)+Math.pow(mY-sY,2))/2, 0, Math.PI*2);  
 c.stroke();if(f==1){c.fill();}c.closePath();  

Penciltool:

 c.moveTo(sX,sY);c.lineTo(mX,mY);c.stroke();sX=mX;sY=mY;  

In this way i set function fill() ,Stroke() etc..
For full program: Click here

Thursday, September 26, 2013

Lisp Interpreter in Python and JavaScript

    LISP is a family of computer programming languages with a long history, fully parenthesized Polish prefix notation.Lisp is specified in 1958.Lisp is the second-oldest high-level programming language in widespread use today; only Fortran is older (by one year). Like Fortran,The name LISP derives from "LISt Processing". Linked lists are one of Lisp language's major data structures, and Lisp source code is itself made up of lists.   LISP expressions are composed of forms. The most common LISP form is function application. LISP represents a function call f(x) as (f x). For example, cos(0) is written as (cos 0).LISP expressions are case-insensitive. It makes no difference whether we type (cos 0) or (COS 0).Some functions, like "+" and "*", could take an arbitrary number of arguments. In our example, "*" took three arguments. It could as well take 2 arguments, as in "(* 2 3)", or 4 arguments, as in "(* 2 3 4 5)".Numeric values like 4 and 6 are called self-evaluating forms: they evaluate to themselves.

Defining a function in LISP : 


 (define (add a b)
  (+ a b))  
(add 3 4)  #4
  Lisp interpreter in Python : LISP.py .(for more details click here)
  Lisp interpreter in JavaScript : LISP.js

Monday, September 23, 2013

Huffman Compression

         A data compression technique which varies the length of the encoded symbol in proportion to its information content,that is the more often a symbol or token is used, the shorter the binary string used to represent it in the compressed stream.Huffman codes can be properly decoded because they obey the prefix property, which means that no code can be a prefix of another code, and so the complete set of codes can be represented as a binary tree, known as a Huffman tree.
        The least length codes correspond to the most often occurring symbols allowing to perform compression. For example, the sequence “aaaaaaaa” contains only one symbol ‘a' that corresponds to the Huffman code with size 1 bit; after encoding the output sequence will have size 1 byte - thus compression ratio will be 8.Because generally a character uses 8 bits.Huffman encoding is based on the principle that letters that appear more frequently should have a smaller code than the ones that are used less often. So, in the English language, vowels would be used more than the letter 'z', and would get shorter codes.

Huffman program  in python Huffman.py
Huffman program in javaScript Huffman.js


For example: Huffman Tree for String "malayalam"

                                                  

Generating Huffman Tree is also simple.We can just check in Huffman Tree Generation

       So Generally The idea behind Huffman coding is to find a way to compress the storage of data using variable length codes. Our standard model of storing data uses fixed length codes. For example, each character in a text file is stored using 8 bits. There are certain advantages to this system. When reading a file, we know to ALWAYS read 8 bits at a time to read a single character. But as you might imagine, this coding scheme is inefficient. The reason for this is that some characters are more frequently used than other characters. Let's say that the character 'e' is used 10 times more frequently than the character 'q'. It would then be advantageous for us to use a 7 bit code for e and a 9 bit code for q instead because that could shorten our overall message length.
Huffman coding finds the optimal way to take advantage of varying character frequencies in a particular file. On average, using Huffman coding on standard files can shrink them anywhere from 10% to 30% depending to the character distribution.

Wednesday, September 4, 2013

Word count programs in Python



text file a.txt:     
      India is my country and all Indians are my brothers and sisters.I love my country and I am proud of its rich and varied heritage.I shall always strive to be worthy of it.I shall give my parents, teachers and all elders respect and treat everyone with courtesy.To my country and my people, I pledge my devotion. In their well-being and prosperity alone lies my happiness.
 >>>from collections import Counter  
 >>>import re  
 >>>import os  
 >>>list_word=re.findall("\w+",open('a.txt').read().lower())  
 >>>for i in Counter(list_word):
 . . .  print i,'-',Counter(list_word)[ i ]  

#
and - 8
all - 2
everyone - 1
love - 1
pledge - 1
give - 1
being - 1
is - 1
india - 1
it - 1
alone - 1
are - 1
in - 1
respect - 1
its - 1
happiness - 1
prosperity - 1
varied - 1
their - 1
indians - 1
to - 2
parents - 1
treat - 1
rich - 1
heritage - 1
be - 1
brothers - 1
elders - 1
shall - 2
am - 1
of - 2
courtesy - 1
worthy - 1
sisters - 1
with - 1
people - 1
strive - 1
lies - 1
i - 5
country - 3
proud - 1
well - 1
teachers - 1
always - 1
my - 8
devotion - 1

another program for word count:
 >>>d={}  
 >>> list_word=re.findall("\w+",open('a.txt').read().lower())  
 >>>for i in list_word:  
 . . .   d[i]=d.get(i,0)+1  
 >>>d  
# dict of word as above

another program for word count:
 >>> list_word=re.findall("\w+",open('a.txt').read().lower())  
 >>> for i in list(set(list_word)):  
 . . .  print i,list_word.count(i)  


Python programming language

Python programming language is more powerful and is often comparable to Perl,Ruby or Java.
Readability of syntax is a distinguishing factor of Python.Python is available for all major operating systems: Windows, Linux/Unix, OS/2, Mac.Python is friendly and easy to learn.

          Comparing to Java,Python programs are 3-5 times smaller than java prog's.Python is dynamic typed language.Python programmers don't need to waste time in declaring variable types as in java.

          List is one of the Data Structure used in Python.Main feature is LIST COMPREHENSION.
Python supports a concept called "list comprehensions". It can be used to construct lists in a very natural, easy way, like a mathematician is used to do.


for eg:

            >>> a=[1,2,3,4]  
            # want to square each item in list a.  
            >>> b=[]  
            >>> for i in a:  
                 b.append(i*i)  
            >>> b  
                [1,4,9,16]  
using List Comprehension: 
            >>> b=[i*i for i in a]  
            >>> b  
                [1,4,9,16]