Thursday 29 November 2012

Conway's Game of Life



Game of Life is not as our typical computer games. It is a 'cellular automaton'. This consists of a collection of cells which, based on a few mathematical rules, can live, die or multiply.


The Rules: 

1. Any live cell with fewer than two live neighbours dies, as if caused by    
    underpopulation.

2. Any live cell with two or three live neighbours lives on to the next generation.

3. Any live cell with more than three live neighbours dies, as if by overcrowding.

4. Any dead cell with exactly three live neighbours becomes a live cell, as if by 
    reproduction.


Patterns

Different types of patterns occur in the Game of Life, including still lifes, oscillators, and spaceships, which are the patterns that translate themselves across the board.


           Still lifes

Block Game of life block with border.svg
Beehive Game of life beehive.svg
Loaf Game of life loaf.svg
Boat Game of life boat.svg
                Oscillators




Blinker [period2] Game of life blinker.gif



Toad [period2] Game of life toad.gif



Beacon  [period2] Game of life beacon.gif



Pulsar [period3] Game of life pulsar.gif
                Spaceships

Glider Game of life animated glider.gif
Lightweight spaceship [LWSS] Game of life animated LWSS.gif
Algorithm
 

Life patterns are represented as two-dimensional arrays in computer memory. Typically two arrays are used, one to hold the current generation, and one in which to calculate its successor. Often 0 and 1 represent dead and live cells respectively. A nested for-loop considers each element of the current array in turn, counting the live neighbours of each cell to decide whether the corresponding element of the successor array should be 0 or 1. The successor array is displayed. For the next iteration the arrays swap roles so that the successor array in the last iteration becomes the current array in the next iteration.



Play the game:





Click here to download 'Game of Life' code.




[Expecting Your Valuable Comments]
Thank You

Sunday 25 November 2012

Generate SSH Key






To use SSH keys to establish a secure connection between your computer and GitHub. Here is the steps to generate SSH keys and adding the public key to your Github account.

Step 1: Check for SSH keys

 Check for existing ssh keys on your computer by running the command given below:
$ cd ~/.ssh 
# Checks to see if there is a directory named ".ssh" in you user        directory                                                
If it shows "No such file or directory", skip to step 3. Else go to step 2.

Step 2: Backup and remove the existing key

Back the old SSH directory up and remove it:
$ ls
# Lists all the subdirectories in the current directory
config  id_rsa  id_rsa.pub  known_hosts

$ mkdir key_backup
# Makes a subdirectory called "key_backup" in the current directory

$ cp id_rsa* key_backup
# Copies the id_rsa keypair into key_backup

$ rm id_rsa*
# Deletes the id_rsa keypair
Step 3: Generate a new key

To generate a new SSH key, enter the code given below. To make default settings, when asked to enter a file in which to save the key, just press enter.
$ ssh-keygen -t rsa -C "your_email@youremail.com"
# Creates a new ssh key using the provided email
Generating public/private rsa key pair.
Enter file in which to save the key (/home/you/.ssh/id_rsa):
You need to enter passphrase.
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]
When you enter the passphrase, it shows something like this:
Your identification has been saved in /home/you/.ssh/id_rsa.
Your public key has been saved in /home/you/.ssh/id_rsa.pub.
The key fingerprint is:01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db                       your_email@youremail.com
Step 3: Add SSH key to Github

Run the following code to copy the key to your clipboard.
$ sudo apt-get install xclip
# Downloads and installs xclip
$ xclip -sel clip < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard
Now,

1. Go to Github Account settings.
2. Click on SSH keys in the left sidebar.
3. Click on Add SSH key.
4. Paste your key into the Key field.
5. Click Add key
6. Confirm the action by entering your Github password.


[Expecting Your Valuable Comments]
Thank You

GIT REPOSITORY





A Git project using two main approaches. The first takes an existing project or directory and imports it into Git. The second clones an existing Git repository from another server.

Initialize a git repository in a directory


Go to the project’s directory and type
$ git init 
This creates a new subdirectory named .git that contains all of your necessary repository files. If you want to begin version-controlling existing files, then you need to start tracking files and do a initial commit. git add commands specify the files you want to track.git commit commands used to commit.
$ git add *.c
$ git add README
$ git commit -m 'initial project version'
Cloning a repository

If you want to get a copy of an existing Git repository, use git clone command.
git clone [url]if you want to clone the Ruby Git library called Grit
$ git clone git://github.com/schacon/grit.git 
if you want to clone the Ruby Git library called Grit in a new directory new
$ git clone git://github.com/schacon/grit.git new
Checking the status

To get the status of the git, type the command git status.Then you should see something like this.

$ git status
# On branch master
nothing to commit (working directory clean)
Add a new file README to your project. If the file didn’t exist   before, and you run git status, you see your untracked file like this:
$ vim README
$ git status
# On branch master
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#   README
nothing added to commit but untracked files present (use "git add" to track)
Tracking a file

To begin tracking, use git add. Now on running the command git status, you will get  something like so:
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#   new file:   README
#
Staging a modified file

Modify a file which is already tracked and then try the command. You will get something like this:
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#   new file:   README
#
# Changed but not updated:
#   (use "git add ..." to update what will be committed)
#
#   modified:   hunter.rb
#

Now this shows the file 'hunter.rb' is modified but not yet staged. To stage it, run the command git add (Multipurpose command- Used for tracking, staging and to do more) and then git status.
$ git add benchmarks.rb
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#   new file:   README
#   modified:   hunter.rb
#

If you modified the file 'hunter.rb'. To see the difference between the modified and unmodified file, type the command git diff before staging the modified file.
$ git diff
diff --git a/hunter.rb b/hunter.rb
index 3cb747f..da65585 100644
--- a/hunter.rb
+++ b/hunter.rb
@@ -36,6 +36,10 @@ def main
           @commit.parents[0].parents[0].parents[0]
         end

+        run_code(x, 'commits 1') do
+          git.commits.size
+        end
+
         run_code(x, 'commits 2') do
           log = git.commits('master', 15)
           log.size
Committing a file


The simplest way to commit is to type the commit message inline with the command git commit after -m flag.
$ git commit -m "Story 182: First commit"
[master]: created 463dc4f: "First commit"
 2 files changed, 3 insertions(+), 0 deletions(-)
 create mode 100644 README
Removing files

To remove a file from Git, you have to remove it from your tracked files and then commit. The git rm command does that and also removes the file from your working directory.
$ git rm pythonhunter.rb
rm 'pythonhunter.rb'
$ git status
# On branch master
#
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#       deleted:    pythonhunter.rb
#
Renaming files

To rename a file, run a command git mv and run command git status to get something like this:
$ git mv pythonhunter.txt hunter
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#       renamed:    pythonhunter.txt -> hunter
#
Adding remote repository

To add a remote repository, run the below command.
$ git remote add origin git@github.com:rasheedh/Photo-Editor.git
Where 'git@github.com:rasheedh/Photo-Editor.git' is the SSH url(Generating SSH key).

Pushing to remote

If you want to push your master branch to your origin server, then you can run this to push your work back up to the server:
$ git push origin master

[Expecting Your Valuable Comments]
Thank You

GIT INTRODUCTION AND INSTALLATION





Git is a distributed revision control and source code management (SCM) system with an emphasis on speed. The major difference between Git and any other VCS is the way Git thinks about its data. Conceptually, most other systems store information as a list of file-based changes. These systems (CVS, Subversion, Perforce, Bazaar, and so on) think of the information they keep as a set of files and the changes made to each file over time.

 Git thinks of its data more like a set of snapshots of a mini filesystem. Every time you commit, or save the state of your project in Git, it basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot. To be efficient, if files have not changed, Git doesn't store the file again, just a link to the previous identical file it has already stored. Git stores data as snapshots of the project over time as shown in the figure below:




Git has three main states that your files can reside in: committed, modified, and staged. Committed means that the data is safely stored in your local database. Modified means that you have changed the file but have not committed it to your database yet. Staged means that you have marked a modified file in its current version to go into your next commit snapshot.

Three main sections of a Git project:




The working directory is a single checkout of one version of the project. These files are pilled out of the compressed database in the Git directory and placed on disk for you to use or modify.

The Git directory is where Git stores the metadata and object database for our project. This is what is copied when you clone a repository from another computer.

The staging area is a simple file, generally contained in your Git directory, that stores information about what will go into your next commit. 

Git Installation

Windows

First, download msisgit. This download is a single executable which installs the entire git system. While going through the installer, you will want to check the options to add Windows Explorer integration when you right click on a folder.


Because we will be using PuTTY as our SSH client, choose Use PLink and fill in the path to the downloaded plink.exe executable.



Continue clicking Next until the installation is complete.

Linux

On Fedora, you can use yum to install Git:
$ yum install git-core 
On Debian-based like Ubuntu, try apt-get:
$ apt-get install git-core 
Mac

Two easy ways to install Git on a Mac:
(1) Graphical Git Installer:
$ http://code.google.com/p/git-osx-installer 
(2) Install Git via MacPorts: 
Install MacPorts and then install Git via
$ sudo port install git-core +svn +doc +bash_completion +gitweb
Git Setup

Set your user name and e-mail address. Every Git commit uses this information while commits:
$ git config --global user.name "Python Hunter"
$ git config --global user.email pythonhunter@example.com
For special projects, to use different name or e-mail address, you can run the command without the -- global
$ git config user.name "Python Hunter"
$ git config user.email pythonhunter@example.com
Configure Tools

If you want to use different text editor such as Emacs:
$ git config --global core.editor emacs
If you want to configure diff tool to use to resolve merge conflicts such as gvimdiff:
$ $ git config --global merge.tool gvimdiff
Check your Settings

Check your git setting using the command:
$ git config --list
Git Help

To get help on Git, type the command given below along with your verb
$ git help <verb>
Example: $ git help config


[Expecting Your Valuable Comments]
Thank You

Saturday 24 November 2012

PHOTO EDITOR



To download my Photo Editor, Click here



Click upload button and select a photo to upload and edit.


By clicking the '+' and '-' button, change the value corresponding to Brightness, sharpness, Color and Contrast.

Before

After

Enter the hex color code for your selected color and click Classic button.

Before
After

Enter the hex color code and contrast and click Paint button.

Before
After

Enter the Left, Top coordinate(LFT, TOP) values and Right, Bottom coordinate(RGT, BTM) values and click Crop button.
Before
After

In order to crop the border of a photo, click Cropborder button.

Before

After

To get the invert of your image, click Invert button.

Before
After 

Select your photo and click Rgba button.

Before
After
Select your image and enter Cmyk button.

Before
After
Select you image and click Sepia button.

Before
After
Select your image and click Rotate button.

Before

After
Select your image and click Resize button.

Before
After

Select your image and click FBcover button.

Before
After
Invert & Rgba combined effect.

Before
After(Invert & Rgba)
After(Invert & Cmyk)

Click save button and select the folder to save.




[Expecting Your Valuable Comments]
Thank You

LISP INTERPRETER IN JAVASCRIPT






For the construction of an interpreter we are considering two main parts parsing and execution. Parsing is implemented using the function parse and execution is done using the function eval

First we define the function Env():

1.This function is defined to find the right environment for a variable.
2.This process of looking variables first in inner environments and then in outer ones is called 
   Lexical Scoping

Consider this scheme example:
(define make-account(lambda (balance)(lambda (amt)(begin (set! balance (+ balance amt)) balance))))

Their environment structure will be: 

State 1:  (define make-account 
State 2:                      (lambda (balance) 
State 3:                                         (lambda (amt) 
State 4:                                                    (begin (set! balance (+ balance amt)) balance))))

In State 4, the variables amt and balance are used. The variable defined in the inner environment(Stage 3) is amt. And the variable balance is defined in the outer environment(Stage 2).
But '+' sign in the Stage 4 is not defined in both environment. So we need to define a global environment .

So we define the function add_globals() :

1. This function is to define the global environment.
2. Many functions for mathematical operators are  defined.
3. Many built-in procedures are accessed by importing modules.

The methods in the list Method are built-in functions and can be used by importing the module Math.

Method = ['abs', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'exp', 'floor', 'log', 'max', 'min', 'pow', 'random', 'round', 'sin', 'sqrt', 'tan']

Where as other methods like addition, subtraction,multiplication, division, modulus and comparison operators and defined and added to the module.

Then we define the function eval():

1. Function for execution part of an Interpreter.
2. Here we evaluates the expression in an environment.
3. Special forms or syntactic constructor are defined.

For eg :-  In case of scheme (if test conseq alt), if the expression test is true, then returns 
                conseq,  else returns alt.

Now we define the function parse():

1. We defines the four other functions tokenize(), read_from(), read() and atom()

2. Inputs are scanned and made a list of strings in expressions.

tokenize() :- This function takes the input expression and replaces '(' by ' ( ' and ')' by ' ) ' and 

                    then  removes the spaces at the beginning and end of the expression using 
                    built-in function trim() and splits the input based on the single space ' ' using 
                    built-in function split(' ').

read_from() :- This function takes the list returned by the tokenize() function and it removes '(' and ')'.

                       Thus we gets the list of expression elements in its order without parenthesis.

read()  :-      This function returns the read_from(tokenize(input))

   
atom() :-      This function converts string of integers to numbers and returns.

These all are the main functions used in my javascript code to make Lisp Interpreter.


Download my Javascript Lisp Interpreter code here :                



[Expecting Your Valuable Comments]
Thank You