Using a linear array as a bidimensional matrix

Often times I find the need to use a list or linear array as if it was a table.

Everytime I need to do so, I always end up coding functions to convert a (x,y) coordinate to the real index n in the array.

Let me illustrate, with an example. You have a string that defines the elements of a game board, and you want to work using (x,y) coordinates.

[python]
s="xxxxx@xx@xxx@@xx"
[/python]

If you were to look at it as a matrix (width=4), it’d be something like this
[python]
s="xxxx
x@xx
@xxx
@@xx"
[/python]

However, I can’t do
s[x,y], since it’s a linear array, it’s a string.

You need to convert from (x,y) to a number that represents an index in the array.

This is very simple:
[python]
width=4
def getNforXY(x,y):
return x + width*y
[/python]

What if you want to do it backwards. What if you need to know what’s the X and Y for a given index N in the string?

[python]
def getXYforN(n):
y = int(n/width)
x = n – width/y
return (x,y)
[/python]

Cheers

Jack Bauer: “HE DID WHAT???”:

My first attempt at a political satire using some fiction. Obama always reminded me of President Palmer on 24. The future release of a bunch of uncertain status prisoners in Guantanamo reminds me of Jack Bauer going through hell, doing a lot of illegal shit to catch the bad guys, and then they’ll get released, they’ll say they were put in there ilegally and they will be back outside more pissed than ever.

Unsigned band breaks into top 20 most shared music on the planet

From Georgia Wonder:

London, UK: Georgia Wonder, an unsigned British band, climbed into the top 20 most shared music in the world last weekend, reaching 14th position in just four days with their debut EP ‘Hello Stranger’.

The EP was put onto the Pirate Bay tracker on Wednesday 14th January and by Saturday 17th was the only music in the top 100 from an unsigned artist. The EP of five songs, which the duo released themselves last year, hit the Pirate Bay Top 20 on Saturday evening and peaked at the 14th position in the early hours of Sunday morning. The Pirate Bay is the worlds largest tracker of shared files.

The band were able to propel their music into well over 50,000 homes across the world in just a few days after making their EP readily available to download from the welcome page of the Limewire software offshoot ‘Frostwire’.

“What Frostwire helped us achieve is incredible” explained Stephanie Grant, vocalist with Georgia Wonder. “Their support has put our music into the hands of tens of thousands of potential fans in a matter of days. 40 billion songs were shared online last year – that’s 6 songs for every person on earth. You can imagine what just five days in the top 20 means to a relatively unknown band like us.”

“We met the Frostwire guys on Twitter” said Julian Moore, the other half of Georgia Wonder. “I’d recently dusted off our Twitter account after hearing that British celebrities such as Jonathan Ross and Stephen Fry had become avid users, and after a few weeks we got chatting to the Frostwire team. They were really keen to let us try their new approach to band promotion and as we were already letting people share our music on Twitter we agreed to give it a shot. I’m so glad we did!’

Pirate Bay, the world’s largest BitTorrent tracker has repelled many unsuccessful attempts to shut it down over the last few years including a police raid in 2006 and bitter disputes with a variety of music and media companies in 2007. “It’s easy to forget that while the major record labels and anti-copyright groups have been taking high-profile shots at each other there are a lot of independent artists out here who are simply trying to navigate their way through the wreckage” said Julian. “Our success with Frostwire proves that you can cut through the argument and chart your own course.”

About Georgia Wonder

Georgia Wonder played in front of over 14,000 people for Simply Red frontman Mick Hucknall on his UK tour and performed at an historic election night at the London American Embassy alongside Glenn Tilbrook from Squeeze. Their song ‘Girl You Never Knew’ from their debut EP ‘Hello Stranger’ has been on rotation on Bliss TV for six months and has received plaudits from Record Of The Day and Pop Justice who called it ‘..one Grey’s Anatomy soundtrack appearance away from being an international hit single.’ They were recently nominated for an interactive award at Eurosonic and won best video in the rock category on OurStage.com in November ’08 for their song ‘Would Love To Meet’. Originally picked up by legendary BBC presenter ‘whispering’ Bob Harris who played their demos for two concurrent weeks on his BBC Radio 2 show, they have performed live on BBC Southern Counties, BBC Radio Solent, Original FM and a host of other regional radio stations.

–ENDS–

Contact

Julian Moore
julian at gwonder dot com

References
Pirate Bay Snapshot 18/1/09

40 Billion Songs Shared Online

Links

Georgia Wonder Website

Frostwire

‘Hello Stranger’ EP Torrent on Pirate Bay

Reviews

Record Of The Day

Record Of The Day – Record Of The Week

Pop Justice – Song Of The Day

Britney Breaks the ice with Anime Fans

A little too many references to Matrix, but it’s pretty cool. Seems like it’s part one of more to come.
<a href="http://www.joost.com/1359x0r/t/Britney-Spears-Break-The-Ice">Britney Spears &#8211; Break The Ice</a>

Remaining Questions about Palm’s Pre and it’s WebOS

It was an impressive presentation the one Palm gave about it’s new product, but there are still many things they didn’t mention:

  • Battery life? It’s cool that it has a removable battery, but how long does it last?
  • What can we build on it? They mentioned HTML, CSS and Javascript as the tools of the trade, but if their JS interpreter doesn’t give us namespaces or global access to built in functions to access core functionality of the phone, there won’t be nothing more than a bunch of web widgets available. There was no mention whatsoever of games for this platform, maybe they’re going after the blackberry crowd and not the iPhone crowd
  • How do developers distribute apps? I didn’t like the whole Sprint Exclusive crap, give us a palm-pre store!
  • Flash Support?
  • No demo of the camera, can it do video, no demo whatsoever of multimedia so far
  • No demo of the device making a phone call

Not sure if those are things that we should take for granted, or that the device still has a long way to go.

Preparing your primary mount and swap with fdisk

I have 4 big drives on a new machine, each can hold up to 2Tb of data, at first I thought I’d use the first drive for the OS and the other 3 for a RAID5 (software controlled)

Then after I had installed the operating system, I decided it was a big waste, and that I’d only need about 80gigs for the OS, and that I should use the 1920Gb (-swap,-other blocks) to be part of the RAID.

The first thing I needed to do was to resize the primary partition, but you can’t really do this while you’re using it. So I restarted with the Ubuntu CD in rescue mode, and jumped to the part where you setup the partition sizes. I told it I wanted to make the first partition smaller, I made it 80Gb, I accepted the rest of the options, and after a long while, it was finished creating the file systems, but it seems like it did a mess.

I restarted without the CD, crossed my fingers, and it did boot.

I did a:

sudo fdisk -l
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *          63   156264254    78132096   83  Linux
/dev/sda2       156264255  2930272064  1387003905    5  Extended
/dev/sda5      2882334168  2930272064    23968948+  82  Linux swap / Solaris
/dev/sda6       156264381  2834380079  1339057849+  83  Linux
/dev/sda7      2834380143  2882334104    23976981   82  Linux swap / Solaris

and there were 6 partitions, some overlapping, but the primary looked right. I entered fdisk and deleted the 5 other partitions, and started a new.

Created a new partition (2) for Swap. Since the machine has 8GB of Ram, that’s the amount of Swap I used for it. After the partition was defined, I changed it’s type to type 82 (Linux Swap)

Then I created another partition (3), this one using the remaining space, and set it’s type to 83 (Linux)

I wrote the changes (w) and exited. It told me that the changes were written but the partitions would not be used until I rebooted, so I rebooted.

When I came back and did:

sudo fdisk -l

There they were:

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        9727    78132096   83  Linux
/dev/sda2            9728       10700     7812500+  82  Linux swap / Solaris
/dev/sda3           10700      182402  1379193956   83  Linux

But now there was a problem, when I checked for the memory available there was no swap

$ free
             total       used       free     shared    buffers     cached
Mem:       8183920     198524    7985396          0       6668      70284
-/+ buffers/cache:     121572    8062348
Swap:            0          0          0

So, first thing, I made sure the swap was specified on /etc/fstab for the next boot to include the swap

/dev/sda2 none swap sw 0 0

And then I had to make a file system for the swap

sudo mkswap /dev/sda2

Then, the “free” command did show the swap:

$ free
             total       used       free     shared    buffers     cached
Mem:       8183920     220420    7963500          0       7552      86880
-/+ buffers/cache:     125988    8057932
Swap:      7812492          0    7812492

In this case I didn’t have to make the file system for the primary partition because it was already made, I suppose the term “making the file system” is the equivalent of saying “formatting the drive with a specific filesystem”

However I had to do it for the remainder of the disk:

sudo mkfs.ext3 /dev/sda3

I’ll let you know if I find anything different from the tutorials on how to make RAID5.

New /etc/fstab format in Ubuntu
I while later, I updated the /etc/fstab to include the UUID of the swap partition on the fstab, instead of the device name.
To obtain this UUID I learned about the vol_id command

$ sudo vol_id /dev/sda2
ID_FS_USAGE=other
ID_FS_TYPE=swap
ID_FS_VERSION=2
ID_FS_UUID=b9b825c2-85dc-4def-bfd1-9071042452fa
ID_FS_UUID_ENC=b9b825c2-85dc-4def-bfd1-9071042452fa
ID_FS_LABEL=
ID_FS_LABEL_ENC=
ID_FS_LABEL_SAFE=

Now my /etc/fstab file looks like this:

# /etc/fstab: static file system information.                                                                                          
#                                                                                                                                      
#                                                                                 
proc            /proc           proc    defaults        0       0
# /dev/sda1                                                                                                                            
UUID=13676a4b-b669-4a7a-9776-cb10c7b492b9 /               ext3    relatime,errors=remount-ro 0       1
#/dev/sda2                                                                                                                             
UUID=b9b825c2-85dc-4def-bfd1-9071042452fa none swap sw 0 0
/dev/scd0       /media/cdrom0   udf,iso9660 user,noauto,exec,utf8 0       0