Converting base-16 roman numbers to arabic numbers (and vice-versa)

Here is neat python programming challenge.

A hex roman numeral is very much like the standard roman numeral, except with different values. In normal roman numerals, I = 1, V = 5, X = 10 and so on. In hex roman numerals, I = 1, V = 8, X = 16, L = 128, C = 256, D = 2048 and M = 4096. So for example:

VIIII = 8 + 1 + 1 + 1 + 1 = 12
IX = 16 – 1 = 15
XV = 16 + 8 = 24
XL = 128 – 16 = 112

The goal is to write a program in python that converts it in either direction. If given a decimal number, it should return the hex roman numeral version of the number and if given a hex roman numeral, it should return the decimal version of the number.

I started this by creating a program that performs a normal roman to arabic conversion. This wasn’t too hard, especially since python has a ton of neat features such as as dictionaries and solid string parsing methods. Since I am using unittest to test my code, I’ve named this file

The package unittest provides a great way to test your programs. I love it. You can pretty much run a another script and it will perform all the necessary assertions as it tests the proper package. Here is my unittest code, which I named

Here are some screenshots of the program in action: first testing through the command line, and then testing it with unittest.

Taking this base code and making it compatible with base-16 numerals was trivial. All that I needed to do was make a minor modification to the dictionary roman_dict and adding extra elements to the lists units, tens, hundreds and thousands. Of course I had to perform a base conversion with the hex2dec function each time I wanted to access a position in the list.
Here is the code that converts base-16 roman numbers to arabic numbers. I saved this file as

And here are my test cases, taken directly from the problem statement and saved as

Here is a screenshot of the program in action.

Finally, if you need some python book recommendations I suggest Learning Python. That’s how I learned.

Password protecting entire directories in MacOSX

Every once in a while I have some files in a directory that need a password. I am not looking for a fancy encryption mechanism like PGP… I just want to compress the entire directory, put in a strong password and forget about it. Anyway, there are several applications that do this on the mac store… but none provide the flexibility that I was looking for. Plus, I realized this can be done in a couple of lines in the terminal.

Lets say you have a directory tmp with a bunch of files you want to compress and password protect it. You can go the standard route using the zip utility. Just type the following:

zip -R tmp/* -e

This will compress the directory tmp/ and store all the files in the It will also ask you for the password you would like to use. Unfortunately you cannot give the password as an argument. So, you are stuck typing the password every time you want to compress a new directory.

There is a hack you can use to bypass the manually password submission… It involves using expect. This program is a really neat utility. It basically allows you to create interactive dialogues with your terminal programs, which makes task automation a walk in the park. You can check its manual pages here.

Step #1: Go into the mac terminal and find out where the expect utility is located. This is done using the whereais command. Type the following on your terminal window.

whereis expect

In my macosx version, the program expect is located at /usr/bin/expect.

Step #2: Create a file (e.g. with the following lines. Make sure you modify the location of the expect program in the first line of the script.

Step #3: Make sure your script is executable and run it.

chmod u=+rwx
./ tmp hello

The first argument will be the output filename (, the second argument is the directory you wish to compress (tmp) and the last argument is the password you wish to use (hello). Neat no? Here is a screenshot of all the steps.

Since I am running a mac, I could also have done this easily using AppleScript… maybe next time.

Choose your own adventure… in audio

One of the things that bores me the most is driving. Ten minutes into my daily commute and I am checking out my email and reading my twitter feed. Anyway, the other day when I was coming from Boston I had this interesting idea; why not create a choose your own adventure game, which could be played while driving. Instead of reading a book, the book would be read to us by a speech synthesizer. The choices would be done by pressing buttons instead of manually flipping pages. The idea was so neat that I proceeded to create a prototype, so I could send it to a magazine, but the outcome was so bad that I pushed it aside until I find a better technology. For my failed prototype I used the linux open-source flite speech synthesizer and a beagleboard XM. As the driving engine I created a simple script in python that read a particular story and used buttons to control the flow of the adventure. The first problem is that beagleboard requires a pretty clean 5V power supply which is a mess to get in a car. Also, the beagleboard is fairly expensive ($120) to use as a dedicated game engine. Finally, creating a customized OS that is fast enough to launch the a particular application on the beagleboard is not trivial.

Anyway, I still think that audio based interactive entertainment systems have potential, but my technical solution I chose was not the best. Most definitely I will revisit this idea soon with Android phones and tablets. Implementing this in android seems to be very easy, thanks to the IOIO connectors. For those interested, this connector is available for purchase through sparkfun. Regardless, here is a very simple code for my choose your own adventure. It is done in python and if you have a macosx it will read out the entries and choices using the built in speech synthesizer.

The story itself (hardwired on the previous file as test_story.txt) is pretty self-explanatory.

Finally, here is a screenshot of the program in action.


Loading a new beaglebone OS onto a SD card using Mac OS X

Installing a new version of the angstrom Linux distribution onto a beagle bone board is as simple as copying the operating system into a SD card. For Mac OS X users, this task can be done through the terminal.

Step #1 – Go to the beaglebone angstrom website, and download the latest *.img.xz pre-compiled file.

Step #2 – Since xz is probably not installed in your base Mac OS X system, download the homebrew software package. After you install homebrew you can also install xz by typing the following in the command line:

brew install xz

Step #3 – In order for you to uncompress the OS into the SD card you need to have administrator privileges. In my computer, I have an account called administrator, so first I need to substitute my current user identity with the administrator. This can be done using the su command. You also need to be in the superuser mode, so after su you also need to use sudo.

su – Administrator
sudo -s

Unlike what is reported on the angstrom website you probably won’t be able to write to the SD card without running unmount first. When you try to uncompress the OS file into the SD card you will probably get a Resource busy error message.
Here is a summary of all commands this far:

Step #4 – In order to overwrite all the partitions on the SD card, you have to make sure the mac OS isn’t using it first. On the command line, type mount and once you know in which directory the device is mounted on (eg. /Volumes/BEAGLE_BONE), type:
[stextbox id="grey" float="false" image="null"]diskutil umountDisk /Volumes/BEAGLE_BONE[/stextbox]

Step #5 – Once that is done you need to figure out what is the system identifier for your SD card (e.g. disk1). Just type:

diskutil list

Step #6 – You are now ready to load the OS into the SD card. This process will take around 30 minutes.

xz -dkc Angstrom-Cloud9-IDE-eglibc-ipk-v2011.10-core-beaglebone-r0.img.xz > /dev/disk1

Using a SD card in Mac OS X terminal

Since all Macbook Pros, come with a SD card drive, the other day I started to work on a set of automated scripts that would backup all my data in a SD card. This means I had to learn how to read and write data into a SD card using the Mac OS X terminal.

Step #1- When you insert a card into the system, you need to find out what is the system identifier for that particular SD card. So go to the console and type:

diskutil list

And you will get something like:

In my case, the SD card is identified as disk1.

Step #2- In order for you to write anything directly into the SD card, you need to know where it is mounted. Go to the console and type:


And you will get something like:

In my case, the mount point for the SD card is /Volumes/BEAGLE_BONE.

Step #3- To actually copy something into the SD card, just type any unix command considering the mount point for the SD card as a normal directory. For example:

ls > /Volumes/BEAGLE_BONE/tmp.txt

This command will create a new file (tmp.txt) in the SD card with a listing of all files in the current directory.

Step #4- When you are done, make sure to unmount the SD card by typing, something like:

diskutil umountDisk /dev/disk1