Programming Pastimes

I often regard programming as an art form in itself. Naturally, that makes me an artist, but I really do not mind that. The art of writing a good program demands all the skills a traditional art demands - natural talent and a lot of practice.

Note: All the programs here are freeware. If any of my code is interesting, you may copy it for private use or as a base to build other applications by substantially modifying the code. I'd appreciate an acknowledgement if you intend to use my code thusly. If you make substantial functional modifications, naturally, the code is all yours. I'm not responsible for any effects of any code you copy from here, but I'll be glad to help out with explanations if you need any.

C   Palindrome Checker
Prime Number Generator
C-shell   Line Flipper
Anniversary Reminder
Error Logger
Perl   Random Line/Paragraph Selector
Mail Previewer
CPU Usage Reporter
Hindi Music Search Engine
tbl to HTML Convertor
FA to RE Convertor
Graph Generator
Photograph Display
JavaScript   Slot Machine Game
Block Move Game
Tic-Tac-Toe Game

Palindrome Checker: This is one of the shortest programs to check whether a given string is a palindrome or not. It takes exactly one line of executable C-code... and it works!!
[Source code: palindrome.c B]
Prime Number Generator: This is one of the fastest C programs to generate prime numbers up to a specified limit. Compile using gcc. To the best of my knowledge, this program runs faster than some benchmark programs that generate prime numbers. The big bottleneck, naturally, is in the system call to write made within the printf.
[Source code: prime.c B]

Line Flipper: I have programmed in C-shell's scripting language too, sad to say. It took me a while to renounce the unwieldy scripting language, but I finally did. However, I did write a few good scripts like this one. It is very robust and behaves almost exactly like cat, but flips all the input lines. It's recursive, so don't try it on long input.
[Source code: updown B]
Anniversary Reminder: How many of us have forgotten a birthday or anniversary we should have remembered? All of us, at some time or the other! With the popularity of organisers that remind one of important dates, my program seems a bit neanderthal, but it works for me. The unadvertised -update <month> option actually generates the appropriate files for xcal, a calendar program available under X. The current program could be modified to send mail reminder too.
[Source code: bday B]

Error Logger: One of my most controversial programs exposes a unique security hole relying on typing errors. Many organisations encourage users to include a public-writeable directory in their path - for example, /usr/cs/contrib/bin at the Dept. of Computer Science at UVa - so that lay users can install unsupported software for common use. Nothing prevents Trojan horses from being installed, of course. My program differs from a Trojan horse subtly because I do not advertise its use. It so happens that some common commands such as more, if misspelt, invoke my program. All common misspellings invoke the same program which figures out which misspelling was perpetrated. Many, many typographical errors have been caught.
[Source code: mroe B]
lrwxrwxrwx   [...]   /usr/cs/contrib/bin/ls-l -> mroe
lrwxrwxrwx   [...]   /usr/cs/contrib/bin/ls-la -> mroe
lrwxrwxrwx   [...]   /usr/cs/contrib/bin/mial -> mroe
lrwxrwxrwx   [...]   /usr/cs/contrib/bin/moer -> mroe
lrwxrwxrwx   [...]   /usr/cs/contrib/bin/mor -> mroe
-rwxr-xr-x   [...]   /usr/cs/contrib/bin/mroe
lrwxrwxrwx   [...]   /usr/cs/contrib/bin/tial -> mroe
lrwxrwxrwx   [...]   /usr/cs/contrib/bin/whcih -> mroe 

Random Line/Paragraph Selector: Though these look like rudimentary and early-effort Perl programs, I like these because they are very concise. They perform the much-used function of picking out a single line and a single paragraph (separated by one empty line) from an ASCII file respectively.
[Source code: liner B, paragraph B]
Mail Previewer: This is a simple Perl program that tells you of new mail in the current text shell. I invoke it within my prompt command in bash by putting the line
in my ~/.bash_login file. This works like a better biff, telling me of the person and subject of new mail.
[Source code: preview B]

CPU Usage Reporter: This is a Perl program I wrote that checks the CPU usage on a machine (specified as a command-line parameter) at the University of Virginia. A decent programmer should be able to change this script to perform the same objective on any network of Unix systems. The usage program is invoked by another of my Perl programs called usage.cgi to generate the University of Virginia Computer Science Department CPU Usage Page via the crontab line:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /users/an4m/public_html/cgi-bin/usage.cgi
[Source code: usage B, usage.cgi B]
Slot Machine Game: My early experimentation with JavaScript produced the little doodads on my home page. As I began understanding the power of JavaScript, limited as it may be, I tried to write more interesting programs. I try to exploit possible interaction between JavaScript and HTML, as you will notice in this simple duplication of a slot game. Programmatically, the scrolling of the slot windows is an elegant touch. The rewards of winning at this slot machine are not very satisfying as of now.
[Source code: View Source of casino.html B]
Block Move Game: This JavaScript program is based on a popular puzzle. The interaction with HTML is very strong. Enjoy your game! For some reason, I found this game hard to code. Perhaps it was just a bad few hours. Even as it is, I think the look and feel can be improved. Projects for when I have the time...
[Source code: View Source of block.html B]

Tic-Tac-Toe Game: All right, so you have seen enough of these off Yahoo. However, the look-and-feel of my version is much more elegant than anything those kids have. Some of them use 9 frames for the 9 cells - such a pain to load. Some others use CGI to generate the current board - that causes an annoying clearing-and-redrawing of the screen. Yet others have a brute-force number of HTML pages that you wade through. Play those, then check this out. The algorithm behind this version of tic-tac-toe is a simple (simplistic?) branch-and-bound algorithm with a branch depth of two moves per player.
[Source code: View Source of tictactoe.html B]
Hindi Music Search Engine: If you are interested in Hindi film music, then this may be an interesting tool for you. My search engine allows searches with imperfect queries on my song database. Also, you can look up songs by singer combination.
[Source Code: unavailable]

tbl to HTML Convertor: Remember tbl? If you do, boy you've seen some cycles go by, haven't you? tbl is a front-end process that converts table specifications into nroff or troff directives. tbl's input format is very succinct, as opposed to HTML, so I decided to write a convertor from one to the other. My convertor ignores everything tbl would have and then changes the rest to HTML. In the example below, the tbl file on the left was given to tbl and then nroff for the table in the centre. The table on the right was got by feeding the code through STDIN to tbl2html to get HTML. Special mention must be made of the fact that numbers are aligned properly. As an additional feature, row-spanning can be achieved with capital field specification characters. A production-quality example of tbl2html in action is my currency page. A fancier example is the result of my stock portfolio evaluator page.
[Source code: tbl2html B]
c s s
c c s
c c c
l n n.
Household Population
Town	Households
	Number	Size
Bedminster	789	3.26
Bernards Twp.	3087	3.74
Bernardsville	2018	3.30
Bound Brook	3425	3.04
Branchburg	1644	3.49
     Household Population
     Town         Households
                 Number   Size
Bedminster         789    3.26
Bernards Twp.     3087    3.74
Bernardsville     2018    3.30
Bound Brook       3425    3.04
Branchburg        1644    3.49 
Household Population
Town Households
Number Size
Bedminster 789  3.26
Bernards Twp. 3087  3.74
Bernardsville 2018  3.3 
Bound Brook 3425  3.04
Branchburg 1644  3.49

FA to RE Convertor: FA stands for a finite automaton and RE stands for a regular expression. An FA and a corresponding RE can express any regular language, hence are equivalent [Hopcroft & Ullman, Introduction to Automata Theory, Languages and Computation]. There exist tools that will take an RE and construct an equivalent minimised deterministic FA for it. However, I did not find a tool that did the reverse - taking a (minimised or otherwise, deterministic or non-deterministic) FA and converting to the equivalent RE - so I wrote my own. The output RE is not as minimal as it can be for a complex FA, but I do what I can. Below, on the left is a sample input file fed to my program and the on the right is the output RE.
[Source code: fa2re B]
# This is a comment.
states: 1 2 3 4 5  # enumerate states and alphabet
alphabet: a b %    # % is the empty string
start: 1           # start state of FA
final: 3 5         # list of final states

1 % 2              # sample transition
2 a 3              # from, on, to
1 % 4
4 b 5 
The RE for the given FA is (% is empty string, @ is empty set): a+b

Graph Generator: Much time and productivity is expended in massaging output data into graphs. It sounds like an easy task, but there's so much to do, that it takes time, effort and work. My tool, grapher, can take input data in a particular format and convert it to graphs quickly. You can specify what views you want in the morass of data and how you want to manipulate multiple values as well.
[Source code: grapher B]
Photograph Display: With digital cameras getting more common, people often find themselves with "stacks" of digital pictures with no good way to view them. You could buy expensive software to see them or cart the photos over to Yahoo or some other site where you'd be bound by their display formats and their disk usage restrictions. Instead of shipping images to foreign site, why not let the display code come to your images? Use photoph to display your photos simply and easily from wherever you want your data stored. See a sample use with my photos.
[Source code: photoph B]
Sample output of both grapher and photoph.

© Anand Natrajan, anand@anandnatrajan•com