Friday, 7 May 2010

Online compiler/interpreter/IDE


I had been using www.codepad.org whenever I wanted to run a piece of code and didn't have the required compiler/interpreter in the system I was using. But the main disadvantage with this was that you cannot read input from the user while using this site - scanf(), cin, <STDIN>, etc. (in C, C++, Perl respectively) are promptly ignored. This was often an irritation since I'd have to modify my program to not take an input before testing there, which might not be an easy process (especially given complex data structures).

Today, I came across http://ideone.com, which is very similar to the above, except that it allows providing input to the program! Yay! You type (or paste) the program in a text area as in codepad, choose your language from the list, and then click on "click here to paste input (stdin) or additional note". Whatever you type here becomes the input to your program. That's a neat solution to the problem of providing input to remotely-evaluated programs.
And it supports "more than 40 languages", including Assembler, Common Lisp, and even Whitespace (of course the boring old C, C++, Java, Perl, Python, etc. are there too). It has other goodies like syntax highlighting for all these languages, 'private' pasting for times when you don't want others to see the code, etc. The site is quite very well done.

While we're in the topic, let me also add two more online code evaluation tools (compilers/interpreters):

* Lord of the REPLs: This is a Read-Eval-Print-Loop from Google, where each line you enter is evaluated immediately. Despite the fancy "Lord" name and the Google brand, I haven't found this very useful mainly because: it doesn't preserve function definitions or variables' values after you evaluate them, unlike other REPLs. This makes it hard to run anything more than trivial "hello world" programs. Also, I found it quite buggy at times.

* "Compile & Execute": This is somewhat similar to ideone, allowing you to enter your own input and having a choice of a similar number of languages. However, it doesn't have a 'private' pasting option, and has syntax highlighting for only a few languages. This leads to weird highlighting at times: if you choose to paste Perl code, the Highlight choice remains in cpp (which is the default), so your Perl code gets highlighted as C++ code! Sloppy design like this makes me think this site might not be very well designed.

These two are the only generic code evaluation tools I could find. If you find that none of these do it for you, try googling for something specific to your language. For example, for Python, searching for 'python online (IDE OR compiler OR interpreter)' gives a lot of good results.

If you know some other online code evaluation tool, or have experience with any of the above, do share it in the comments. Thanks!


Thursday, 1 April 2010

Songs list from files list

I had a list of mp3 files like:

ANBE_VAA_ARUGILE.MP3
ANBU_THAYAE.MP3
ANDANVAN_THEANAKTRU.MP3
ANNANENNA.MP3

and wanted the list of song names from this… What shall we do?

* Open Cygwin (Unix emulator within Windows), navigate to that folder, do:
      ls | cut -d. -f1 |  sed –e 's/_/ /g'
   list the files in this directory, cut and extract the 1st field with delimiter as . character, then substitute space character in place of _ character globally. In other words, get the actual filename removing the .mp3 extension, then replace underscores with spaces.

* Save the above output to a file
     Actually this too should be a part of the above step, like:
      ls | cut -d. -f1 |  sed –e 's/_/ /g' > ./songnames.txt
     The file songnames.txt now has:

ANBE VAA ARUGILE
ANBU THAYAE
ANDANVAN THEANAKTRU
ANNANENNA

* Open the file in Vim editor, and select all the text by pressing ggVG. Then, press : to enter command mode, and Vim will show :’<,’> and wait for your input. Type this:
    !perl –lpe "@words = map { ucfirst(lc($_)) } split; $_ = join(\" \", @words)"
and press enter, and voila, the list has become:

Anbe Vaa Arugile
Anbu Thayae
Andanvan Theanaktru
Annanenna

Beautiful, isn't it?
Thank God, Unix exists! :)

Monday, 15 March 2010

Application to fetch Wiktionary definitions

I am preparing for GRE, and am using Anki for learning. However, the definitions Anki gives are not very good (that is, the GRE 'card deck' available to use within Anki is not very good), so for every word I'm having to refer Wiktionary to understand it better.

I got lazy to every time copy the word, type wiktionary in Firefox's address bar, and edit the URL to paste the new word. So, I created a small application to do most of the grunt work for me.

Copying the word with Ctrl-C I still have to do, but the rest of the work I can now invoke with a simple shortcut Ctrl-Alt-D. D for Define.

The application executable is available at http://github.com/sundaryourfriend/DefineMyWord/blob/master/DefineMyWord/bin/Debug/DefineMyWord.exe, though this one is built for my machine (64-bit Intel) and might not run on others. Give it a try if you feel like experimenting.

I created this app in C# to learn the language a little (learnt very little of it though). I've placed the code in a Git repository at http://github.com/sundaryourfriend/DefineMyWord. Yeah, I call it DefineMyWord for now, until I get a better name. :)

It's probably terrible C# code, and there are a lot of rough edges: I couldn't get Alt-D to make it focus on the address bar (any help on this is welcome), it doesn't store previously opened URLs for now, etc.

After I was deep into creating this app, I realized I could have made a much simpler app to open the Wiktionary page in Firefox when a shortcut was pressed, instead of trying to reinvent the wheel. However, I was too much into the project already, so I told myself that this way I'd be able to add features easily whenever I need. Hope I do add some.

Wednesday, 10 March 2010

Directory permissions in Unix

There are umpteen pages in the net about permissions in Unix. This is not yet another one of them.

Even after reading many of those umpteen pages, I couldn't remember what the permissions meant on directories. This is because the names of the permissions are directly related to file operations, and their interpretation for directories is not very straightforward. Even Wikipedia says 'The effect of setting the permissions on a directory (rather than a file) is "one of the most frequently misunderstood file permission issues" (Hatch 2003).'.

So here I'll try to explain them in a way we can remember.
You should already know this, but: Unix permissions are of three kinds - read, write, execute. On files, these have obvious meanings - whether you can read the contents of the file, whether you can write to the file, and whether you can execute the file (for example, if it was a script).

Directories too have these permissions. What does 'read' permission on a directory mean? This is the easiest to answer and remember, provided you start thinking of directories as simply special files that have lists of filenames within them. What does being able to 'read' such a file mean? It means you can get the list of files under the directory. That is, you just have been provided with permissions to see what all files are present under that directory. Note that this permission only means that you can get the names of the files - reading the files themselves depends on other things. Think of it like this: you somehow get hold of a list of room numbers and names of girls staying in a ladies hostel - this would be the read permission on the directory. But that does not mean you can go and meet any of them, since (a) they might not be willing to meet you - you might not have read permissions on the files, or (b) the security at the hostel might not allow you - you might not have execute permission on the directory (this is explained below).
That's directory read permissions for you.
 
Next is the 'write' permission on a directory. Let's continue thinking of directories as special files with list of filenames stored. Then, what would it mean to have write permissions on such a special file? It would mean you could modify the entries in the list, delete entries and add new entries to it. So, a write permission on a directory means you can modify the names of the files in it, delete files from it or create new files in it.
Note that with a write permission on a directory, you can change the name of a file under it, but cannot change the contents of the file (unless you have write permissions on the file too). Of the three kinds of permissions, write permissions are a little special in that they sort of allow some modifications on the files - they allow you to rename the files, or delete them.
A lot of people mistakenly believe that if write permissions on a file are set to off, others cannot delete the file. Now on, hopefully you'll remember that it's the write permissions on the parent directory that matters, not on the file itself.

Ok finally, we've come to the 'execute' permissions on a directory. Huh? What can it mean to 'execute' a directory? Doesn't make any sense!
That's because it isn't meant to - they just had one permission name and one functionality left, so they mapped the two. So, what's the functionality here?
Now imagine the situation at the end of the 'read' permissions paragraph above again, but you don't have the list of room numbers or names with you.You won't have much luck trying to coerce the security into giving you a list of the names of the girls staying there. On the other hand though, if you knew the exact name and room number of a girl, you could tell him those details and ask him to request the girl to come and meet him. Having directory 'execute' permissions alone is like having such a security in front of the directory - if you know the exact path and filename of a file, you can access it, but if you don't, you're out of luck: you can't just peep into the directory and try to find what's there.
Whether the meeting actually happens depends upon the girl's willingness; and whether you can actually read the contents of the file depends upon the file's own read permissions.
When you don't have execute permissions on a directory, it's as if the security was not a helpful person, and instead is a fat old lady who refuses to speak to you, and asks you to simply get out - girls in this hostel are not allowed to meet outsiders(!).

Finally, let's try some combinations to make sure we understood things. What if you have both read and execute permissions on a directory? Well, what if you got hold of a list of room numbers and names of girls, and also had a security in front of the hostel? You could look up any girl, tell the security her room number, and thus try to meet her.
What if you had read permissions on a directory but no execute permissions? Not much luck here, you got hold of a list of room numbers and names of the girls, but it's of no use - the fat old lady (remember, she appears whenever you don't have directory execute permissions) doesn't care about what details you have, so you have only the list to look at and sigh.
What does it mean to have read permissions on a file, but not have read or execute permissions on the directory? This means that even though the girl is willing to meet you, you can't find her room number. Worse, you would be blocked even if you somehow had it - the fat old lady again. One thing to remember is that you have to have permissions on each directory in the path to the file. If the file was at /psgtech/mblock/512/myfile, then you should have permissions on each of the directories psgtech/, mblock/, and 512/. It's as if there was a sequence of securities at each stage to the room.

I hope that explanation helped you understand directory permissions in Unix. Please leave a comment if some part of the explanation isn't clear.

Friday, 12 February 2010

Why are computers unusable?!

"One of the basic problems with computers -- and one that CS has done depressingly little to address -- is that computers suck. Of course I don't mean they suck at computing, but rather that they suck to use."
"The present situation, where administration of a Windows/Linux machine is a near-impossible task for most users, is untenable." 
 - from a comment on The Geomblog

I too have been thinking, why are computers so complicated that you have learn so many things to just use it ? To program the computer, you'd always need to learn things, but why have a big learning curve for users too? It's as if we had to learn car mechanics to drive a car! But things are getting better with time, and I hope and believe that the "appliance" computers they talk about in that post, computers where you plug it in and "it just works", aren't too far in the future. I love programming, but I love my users more - they are the reason my profession exists, and it's my duty to make things as easy as possible to them.

(Why such a short post from my side? To steal from Ars Mathematica
: "My normal tendency is to write long posts that I never finish. I’ll start [...] with small posts to see if I can break the habit.")