Skip navigation.

Maudlin ruminations of a mind bit by wanderlust.

And a shaft of light shall sunder the heavens...

Posts tagged with "Programming"

Babbage, the language of the future...

Check this out : http://www.tlc-systems.com/babbage.htm

You have to have reasonable programming knowledge to understand the humor, but most people have that nowadays.

I still haven't stopped laughing.

Playing the 'Guess my number' game recursively

,

This has probably occurred to a lot of you, but I only just realized it, after reading a book on Haskell. I'd never thought of it before.

Most programmers have written their own version of the 'Guess my number' game -- You know, it picks a number, you guess, it says if you're too high or too low, that kind of thing.

What we usually do is this :

def guess(n) :
    while True :
        x = int(raw_input('Enter a number : '))
        if x < n :
            print 'You're too low.'
        if x > n :
            print 'You're too high.'
        if x == n :
            print 'You're absolutely right.'
            return


I'd never realized that it could be done recursively, although it looks pretty obvious. No, it doesn't offer any performance boosts, and no, it doesn't make it easier to read. It's just something different.

def guess(n) :
    x = int(raw_input('Enter a number : '))
    if x < n :
        print 'You're too low.'
        guess(n)
        return
    if x > n :
        print 'You're too high.'
        guess(n)
        return
    if x == n :
        print 'You're absolutely right.'
        return


The usefulness of recursion is not perhaps as apparrent in this case as it usually is, but still...

Variety

, , , ...

My exams start tomorrow, and end on the 23rd of May. An era will have ended, as I will no longer be subjected to Physics and Chemistry after these exams. Anything I do in these subjects after this will be my own folly.

In other news, I was recently keeping up with the French Presidential elections. I was backing Segolene Royal (Inconsequential, since I can't vote, but still), for the same reason why many people voted for her -- Because I didn't like the other alternative. Being a student, I can sympathize with the student riots in 2005, and can't see my way to elect a president who did tantamount to incite it. Granted, I know almost nothing about any of these issues, but still this is what my gut feeling says.

The Python posts are on hold, because my exam on C is tomorrow. I don't like C. One quote compares a C program to a dance on a waxed dancefloor holding knives. That's pretty much how I feel. In C, you need to think about what you can do practically, and then write a program. In Python, you write a program, with the confidence that you can make it work.

Australia won the World Cup, and the Sri Lankans are in uproar about Adam Gilchrist's keeping a squash ball in his glove. I don't know what to say.

Let the circus begin...

,

Over the last 6 months, and especially in the last month, I have immersed myself in the study of Python (Hence my inactivity, or so I justify it). Because of this, My next few posts will probably be Python related, with this one being an inaugural post of sorts. For anyone who's going or thinking of going into Computer Science or related fields, or even anyone who likes nifty shortcuts to get things done, or anyone geeky enough to use Linux, read on. Even if you don't fit into one of the above categories, maybe I can change your mind.

What is Python?

The Python website says :

Python® is a dynamic object-oriented programming language that can be used for many kinds of software development. It offers strong support for integration with other languages and tools, comes with extensive standard libraries, and can be learned in a few days. Many Python programmers report substantial productivity gains and feel the language encourages the development of higher quality, more maintainable code.



I more or less agree.

Python is arguably one of the most elegant and simplest programming languages in existence. It has very simple syntax, (ie to say, almost none at all), which borders on natural English. Many people have remarked that Python is akin to "executable pseudocode". Because of this, it's very easy to pick up, and lets you concentrate on the logic, rather than grappling with complicated syntax rules. In effect, it's a perfect beginner's language.

But don't think that it's just a beginner's language. Its extensive libraries (more on that later) and exhaustive constructs give it as much functionality as any program would require, and then some. Many mundane/difficult tasks become simpler when using Python.

I'll just give the one example this post, to keep it readable. This is the "Hello World" Program in C++

/* File HelloWorld.cpp */
#include <iostream>

using namespace std;

int main(int argc, char* argv) {
    cout << "Hello World";
    return 0;
}

And to run it, you'd have to do

$ gcc HelloWorld.cpp -o HelloWorld.o
$ ./HelloWorld

on a linux box. Don't ask me for Windows, I hate programming on Windows.

And now for something completely different...

# File HelloWorld.py
print "Hello World!"

and to run,

$ python HelloWorld.py

See the difference? Inevitably, both these programs are the first taught in their respective languages, and yet I'd never want to teach what exactly '#include', 'namespace', and 'int main(int argc, char* argv[])' mean. All I'd want to teach would be the 'print'/'cout' statement, which is exactly what Python asks for.

Biased though I am, I must tell you which are the only 2 inadequacies I've found so far in Python :

  • It's slow compared to other compiled languages. Not that slow, but it becomes noticeable in large programs of the number crunching sort.
  • You must have Python installed on any computer you want to run a Python program on. This is not a problem for linux machines, as most of them have python by default, but can be a minor headache for windows users.


But these aren't that important for many people, and they certainly aren't important for the casual programmer.

One other point worth mentioning about Python is its 'Batteries Included' Policy. This means that when you install Python (all of a 9MB download), you get a huge wealth of standard libraries -- almost all the built-in functions you're ever going to need. There are very few separate downloads I have needed to make to obtain code to use in my programs.

A final note for this post :
The name 'Python' does not come from the class of non-venomous constricting snakes genus pythonidae, but rather from the name of the British comedy troupe Monty Python's Flying Circus and subsequent creations.

Links worth looking at :

The KlueLESS Pythonist.

, , , ...

You know about me and Klueless. I don't know if you know about me and python, but if you don't, know it now.

The Python Challenge is a riddle similar to Klueless (Perhaps such a format is standard for all internet riddles), but requires programming knowledge in Python. It was launched in May 2005 (which means I just found out about it) and has 33 levels. The challenge helps you find your way through the nooks and crannies of all the amazing modules included with python, features you didn't know were there and can almost certainly find a use for in the future. It's as addictive as klueLESS is/was, although you need to know python first.

For those who are KlueLESS :wink: as to what Python is, it's a very simple and elegant language that rids you of syntax problems so you can concentrate on the important stuff. Python is an awesome programming language which I'm definitely going to blog about in the near future. It's very easy to learn, and very soon you'll start applying it to problems you have everyday.

NB, About the challenge : You can do the problems in a language other than python (I considered doing some in C++ because I was lazy) but in the end you'll find that python was simpler, faster and more elegant. Besides, it's against the spirit of the game (And all the hints are in python code :wink:).

Go try it out, and tell me what you think.

Of UIs that drive you stark, raving mad...

, , ,

The user interface is one of the most important parts of a program. It can make a new user like it, or it can make an expert user hate it. The aim of any UI is to minimize the work needed to do what the user needs to do. While perusing lecture notes for a course on UI design at MIT, I came across this website. The "Hall of Shame" section showcases some of the stupidest user interfaces ever written. Not surprisingly, Microsoft comes up fairly often in that list.

One which I particularly liked, was this one:




This was given in the mail client, cc:Mail. The person who submitted this particular error to the Hall of Shame, gave the following explanation.


My favorite error message is the delightful 'No Error' occasionally encountered with cc:Mail. While having 'No Error' sounds like a good thing, it is labeled as a fatal error, which sounds like a bad thing. When the user clicks on the OK button, cc:Mail terminates, which seems to indicate that having 'No Error' is a serious error in cc:Mail. Then again, maybe using cc:Mail was the error, in which case exiting the program just corrects the error with the result of there being no error.

Download Opera, the fastest and most secure browser
December 2009
S M T W T F S
November 2009January 2010
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31