Wednesday, 17 September 2014

Java factorial program with recursion and BigIntegers

Another initiative at work expects me to have a certain amount of Java knowledge. Oh well. Here are some good resources I am currently going through:
Learneroo's first chapter has a factorial method recursively. No biggie. I wrote a quick program, ran and tested it for small values. When I tried it for '100', though:

Enter a number for which I will find the factorial:

Oops. The same program worked fine with Python, what was I doing wrong? A quick check showed that my program returned the result as an int. The highest Int you can get is 2^(31)-1. So, enter the BigInteger class. It is not a native data type and I had to tweak the program a bit to handle it. This is what it shows as the output now:

Enter a number for which I will find the factorial:

And here is the full program:

import java.math.BigInteger;
import java.util.Scanner;

public class Factorial {

    public static void main(String[] args) {
        Scanner read = new Scanner(;
                .println("Enter a number for which I will find the factorial:");
        int num = read.nextInt();
        Factorial test = new Factorial();
        BigInteger result = test.findfact(num);

    public BigInteger findfact(int n) {
        if (n == 1) {
            return BigInteger.ONE;
        } else {
            return BigInteger.valueOf(n).multiply(findfact(n - 1));

Friday, 22 August 2014

Python for day-to-day tasks: the webbrowser module

An initiative at work requires me to analyze a large spreadsheet of data. One of the columns contains a csv-separated list of bugs that I need to take a look at. Normally, this would be a painful job of copying each defect and pasting it in the browser repeatedly. When hundreds of rows needed analysis, each involving multiple defect IDs,  I switched to python to ease my pain.

I knew where to look because of this cute easter-egg in python:

import antigravity

The help text for this module led me to the library, c:\python33\lib\ in my case. The webbrowser module imported here showed how easy it is to launch a browser tab by passing a URL from python:

import webbrowser
import hashlib"")

This was all I need for my job as well. I wrote a 10 line script that basically:
  1. asked the user for input (in my case a comma separated list of defects),
  2. converted the input to a list and stripped unneeded whitespace,
  3. iterated through this list and appended the defect ID to the base URL, and
  4. called the webbrowser module to open the tabs.
The resulting script looks like this:

import webbrowser

bugs=input('Paste the list of bugs:\n')


for i,x in enumerate(buglist):

for i in buglist:

And that's it! A half-dozen tabs open immediately and I can move on to analyzing them. Python has proved to be a boon in my daily job for quick and dirty scripts like this.

Tuesday, 12 August 2014

Heard joke once

Heard joke once:

Man goes to doctor.  Says he's depressed.  Says life seems harsh and cruel.

Says he feels all alone in a threatening world where what lies ahead is vague and uncertain.

Doctor says "Treatment is simple.  Great clown Pagliacci is in town tonight.  Go and see him.  That should pick you up."

Man bursts into tears.

Says "But, doctor..."

"...I am Pagliacci."

Good joke.

Everybody laugh.

Roll on snare drum.

- Alan Moore, Watchmen.

Wednesday, 30 April 2014

Resident Evil 5 Impressions

Am I glad I got RE:Revelations on the 3DS. Since then I’ve moved on to RE: Mercenaries (3DS) and Resident Evil 5 on the PC. I finally got around to finishing the latter over the weekend, and thoroughly enjoyed it. There was a bit of a difficulty spike towards the ending but I blame myself for trying to shoot my way through the enemies instead of observing their weaknesses and attack patterns (damn those Reapers). Mercenaries was what got me into the series’ rhythm of shooting a limb off and closing in for a melee attack.

Where Revelations told the story from multiple characters’ perspectives, RE5 allows you to play only as Chris for the entirety of the game (completing the campaign does allow you to play as his partner Sheva). I decided to stick with the handgun and shotgun for most of the game. The inventory box gets a bit frustrating due to its small size, so I did not have the luxury of carrying (and experimenting with) a lot of weapons.

The campaign is suitably long, and manages to be so without any unnecessary padding. The pacing is great: quiet moments allow you to soak in the atmosphere. A couple of zombie attacks follow, progressively ratcheting up into bigger fights (more enemies, or a mini-boss), all of which finally culminate into a massive boss fight.The levels are well designed and fit the setting pretty well. The game throws in a few driving missions to keep things fresh. These work pretty well and do not outstay their welcome.

Special mention should be made of Chris’s partner Sheva, who does not get in your way all the time, knows how to find her way to you without running in circles, does not continuously demand support in the middle of a firefight, and actually feels like a useful partner. Of course a blindly helpful teammate would have been too boring, and there is enough give and take to make it feel like a true partnership.

Finishing the main campaign unlocks some nice goodies, such as Mercenaries mode and an infinite ammo option that I’m looking forward to try.

The RE movies have long been a bit of a guilty pleasure of mine, and I’ve now become a fan of the games as well.

Thursday, 3 April 2014

Turtle Power

Thanks to this great website, I’ve been having some more fun with Python.  I’m now in the chapter on Recursion, which introduces Python’s Turtle module. Turtle is an implementation of Logo, a language I remember playing with happily in my younger years. What you can do with it in Python is quite amazing sometimes.  There are some demo scripts that come pre-installed, and can be accessed this way:
python -m turtledemo
I’ve still not wrapped my head around the more complex ones, but the Sierpinski example in the tutorial is a nice start. I learned from their example and wrote my own (fairly similar) version. The result looks pretty nice:

A couple of weeks back I wouldn’t have been able to come up with something like this at all, so I’m pretty happy with what I’ve learned so far.
Below is the script. Just pasting it in a python3 prompt should suffice.

Tuesday, 1 April 2014

Tagore on critics

The manner in which literary analysis is engaged with in our country is completely uneducated. There’s no point in hearing: ‘I liked it’ or ‘I didn’t like it’. That only gives you a particular person’s opinion; it doesn’t give you the truth of that opinion. If that opinion comes from somebody who is sufficiently capable of appreciation or experienced in literary affairs then even that might make you think a little. But just any person’s opinion has no value at all. Our country lacks good reviewing skills—and the primary reason is that the people of our country do not have an intimate acquaintance with literature.

Monday, 24 March 2014

Flask trial run on PythonAnywhere

I felt it was about time I got into the web development side of Python, having learned enough of it to be dangerous. Since I moved my site to a PaaS, I wanted a quick and dirty alternative Python host that was easy to set up and use. Enter PythonAnywhere. This seemed to fit my immediate needs, as the site provides web-based bash, python and ipython shells for free users. They support both Python 2 and 3, so I went with the latter.

Next up, Flask. Their quickstart tutorial was what I used as a baseline. It seemed easier to start with than heavier alternatives like Django. Although the tutorial covers a minimal blog, I was able to make enough tweaks to it to get what I wanted.

Finally, a purpose. My wife is about a month away from delivering our first baby (oh let it be a girl, please :) ), so I decided to make a simple site where the two of us could enter names that we wanted, for both genders. Each entry is equivalent to a blog post in the Flask tutorial, so the underlying code remained largely the same. It was easy enough to add the rest of the parts I needed. Here then is what it ended up looking like, and here's a screenshot if I end up taking down the site later:

PythonAnywhere turned out to be a great experience. The founders were friendly enough to exchange a couple of mails directly, which was a fresh change from the welcome mails that other sites favour. Getting the site up from my local test setup was a simple matter of ftp'ing a tar file over and extracting it.

Overall it took me less than half a day to get everything up, and another few hours of tweaking to add cute pictures and stuff.