Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

25 January 2014

Sierpinski Triangle - Haskell

Hello folks!
I'll skip the 'It has been a while...' part and jump right into the thing.

So there is this fractal, Sierpinski triangle:
Like any other fractal, you can see the self-similar pattern appearing on every scale. It looks the same from far and near.

Today, I wrote a Haskell program to print the Sierpinski triangle:

Ofcourse, Haskell means it can be more concise. But hey, I am a newbie! ;)

I have been fascinated by the Functional programming paradigm ever since Pramode Sir initiated me into it.
And, Haskell = Mindblowing!

Here are some related links:
Learn you a Haskell for Great Good!

29 February 2012

Conway's Game of Life - life.py

    Read about Conway's Game of life if you still haven't. I came to know about this from Saurabh Santhosh, a senior of mine. And, IT IS AWESOME..!

    Here is a basic 'game of life' program. As it is a zero-player game, my program reads the size of universe, the number of cells and their (x, y) position.
Program
Output

    The given configuration ends up in an oscillator. I had to manually exit. But program stops when all we're left with is still life. And also, an infinite universe(unbounded environment) is not simulated in this code.

Here's the Github repo of my Life :P - https://github.com/jithusunny/Life

12 November 2011

wordify.c - Prints number in words

jisuka@HCL:~/beta$ ./a.out
Enter: 9876543       
ninety eight lakh seventy six thousand five hundred and forty three


You can browse the code here.

It is a really verbose version. If you find a better solution, kindly put the link here.. ;-)

25 September 2011

Pascal's Triangle - C Program

    Pascal's triangle is a triangular array of the binomial coefficients in a triangle. Read more about Pascal's triangle here.
    The gif image below is from Wikipedia that demonstrates the formation of Pascal's triangle clearly.
File:PascalTriangleAnimated2.gif 
    Here is a recursive algorithm to find out the number at any given position.
Algorithm
function element_at(row, position)
        If position = 0 OR position == row
                return 1
        else
                return element_at(row - 1, position - 1) + element_at(row - 1, position)

The C program
The github repo - here
    By the way, did you notice the elegance of the recursion? The code is almost same as the algorithm. Recursion makes code readable, concise and brilliant..! 

19 September 2011

Python script to decipher Caesar ciphers

    Caesar cipher is a simple, widely used encryption technique. It is a kind of substitution cipher in which each letter in the plain-message is replaced by a letter some fixed number positions down the alphabet.

Caesar3.svg
Read more - http://en.wikipedia.org/wiki/Caesar_cipher

Example 

Today, I prepared a simple python program to do this for solving a puzzle from Kryptos - an online treasure hunt conducted as a part of MEC EXCEL 2011.


Then I added some more features to it like handling both uppercase and lowercase & ignoring symbols. Here's it.
Sample Output
Note: This program can be used for both ciphering and deciphering..!

07 September 2011

Mobme Codejam - Solutions

    I hope you have read about Mobme Codejam here. 
Here are my solutions to some of the contest problems.

Mirror
    Write a program which when given links to two images on the Internet, finds out how similar they are.
Bonus points if it ranks images on a similarity scale from 0 - very different to 100 - exactly the same. 
Here is my Solution

Ternary
    Write a number generator that keeps printing out 10 digit numbers, one in each line. Write a second program called filter that takes the output of this number generator and filters it based on its first digit: if the first digit starts with 0, write it to a file named 0.txt, if the first digit is 1, write it to a file named 1.txt, and so on. Write a third program that when run keeps a watch on these files and prints out the count of numbers in these files every second.
Note: Three separate programs are required here.  
Here is my Solution

Tricolour
    Write a program which will generate the Indian national flag. 
Here is my Solution


Spell
    Write a spell checking program which checks an input text with help of an English dictionary. Remember: the spell checker is better if it manages to catch the most likely errors.
[Bonus points if you use cool data structures, like say, a Bloom filter] 
Here is my Solution

Sandbox
    Write a simple TCP server that supports the following commands:
  • list - Lists all the files in the directory the server is running on.
  • pwd - Print the path of the directory the server is running on.
  • cat - Print the contents of the filename given as argument.
  • rm - Delete the filename given as argument.
  • touch - Create a file with name and content given as argument.
  • time - Returns the current time on the server.
Also implement a simple client program that uses this server. The client must create a file every minute on the server with a random filename and with the content being the current server time. 
Here is my Solution

Invariant
    Consider the following process done on 4 digit numbers:
Take any four digit number which has at least 2 different digits. (1111 is not allowed, but 1112 is)
Arrange the digits first in descending order to get another 4 digit number (2111) and then in ascending order to get one more four digit number (1112). Add leading zeroes if necessary.
Subtract smaller number from the larger number (eg: 2111 - 1112 = 0990)
Repeat from step b
Example - Starting with 1112
2111 - 1112 = 0999
9990 - 0999 = 8991
9981 - 1899 = 8082
8820 - 0288 = 8532
8532 - 2358 = 6174
It’s an interesting fact that this process when done on 4 digit numbers always ends up at 6174 after a few iterations. In the example above, 1112 took 5 iterations to evaluate to 6174.

Write a program that performs the above operation on all valid 4 digit numbers from 1000-9998 and find out how many iterations it takes each number to evaluate to 6174.

Also: print a frequency distribution table with number of iterations needed to reach 6174 as one column and the total count of numbers corresponding to that iteration as another. [Bonus marks if you can represent this as a nice looking graph.]
Example Output:
Iteration Total Count of Numbers
    1       10
    2      151
    3      240

Here is my Solution 
Here is the github hub of the solutions - https://github.com/jithusunny/Mobme-Codejam

08 August 2011

Mini Project - Computer controlled Vehicle

Our Mini-Project was a Computer controlled vehicle. The user can control the vehicle using a GUI in the system. The GUI was coded in PyQt and it had PySerial code embedded in it for serial communication. 
At the system side,
  • CP2102 USB - UART Bridge.
  • 433 MHz Transmitter. 
  At the vehicle side,
  • Microcontroller - Atmel Atmega8.
  • L293D motor driver IC.
  • 433 MHz Receiver.
  • 12V Battery.
  • 7805 Voltage regulator IC.
  • DC Motors and other mechanical parts like chassis, wheels, etc.
  Screenshot of the GUI:
    Updated on: Sept 7, 2011

    Source Files 
    C-Code for AVR AtMega8 Microcontroller - Download
    PyQt4 Source for GUI - Download
    Project Report pdf - Download 

    The Github repo of Mini-Project is here - https://github.com/jithusunny/Mini-Project