Skip to main content

Posts

Showing posts from 2012

Partitioning - or, students into n groups - in Ruby

A while back a friend of mine asked if I could automate the creation of groups in a class of students - to maximize the variation within each group, but minimize the difference between them. This sounded like an interesting problem, so I set about to solve it in my own naive experimental Monte Carlo (inspired) way - without looking into ways this has been solved (surely elegantly) before. The result was this little Ruby script: Loading... First I just require some code I have previously written. (I guess I really should make them into gems or something instead of copying code around...) names.rb  tries to guess sex from a person's name and/or title, and countries.rb  maps countries to continents, regions etc with some fuzzy matching of names. (I'm looking at you Democratic Republic of Kongo,  Koreas  etc.) Easy-peasy. Then I set some standard variables. (Maybe I'll make this dynamic in the future, why not?) The most interesting entry here is the classifiers  hash. This

Unicode characters in irb on OS X

I noticed that my RVM-handled irb in OS X (Mountain Lion) didn't like Norwegian characters today. So instead of an 'ø' , I got '\U+FFC3\U+FFB8' during entry, and nothing afterwards. Like so: Not really satisfactory. After googling: "os x irb unicode characters rvm" I found the answer on stackoverflow . Almost. My adapted solution was this: So now I can do: Better. (Not quite sure why the '--verify-downloads 1' part is needed, but it did the trick.)

Un, deux, tscha-tscha - or Counting in Ruby

Inspired by my little son's eagerness to count when he sees groups of things that (he thinks) can be grouped together these days, I wanted to post my often used, tiny, fairly naïve extension to Ruby's Array class that I call count_by . This might not be the most beautiful way to solve this, but it is fairly readable and is leveraging some key aspects of the Ruby language - open classes, or monkey patching if you must, and dynamic calling of functions. Basically I open up the class Array and add a count_by function to it - that takes as argument the name of the function one wants to count the elements by. The function itself starts off by instantiating a Hash , called map , with default values of 0. For each element in the Array I then call the function by the name provided and increments the  entry in map  corresponding to the result. Like this, if I have a array of, let's say Olympic medalist objects with functions nationality in list , I can easily say:  list

Kjært barn har mange navn - or, the story of how I discovered Ruby's each_cons

Reading the Pragmatic Programmer's PragPub from March (I must admit I don't have/take the time to read them regularly enough) I stumbled upon Scala's sliding function on Arrays. This creates a sliding window of consecutive sub arrays of an array. So an  Array(1,2,3,4).sliding(2) would produce an iterator of arrays (1,2), (2,3), and (3,4). Useful! While googling around to see if Ruby has something similar I stumbled across the windowed function in F# that pretty much works the same way, before finding Ruby's each_cons . The first thing I did to test this was to revisit an old (fairly obvious, brute force) seven line Ruby solution (from back in the day when I had just discovered the language) to an Euler problem , and replaced it with the following (admittedly still brute force) one liner (where 'number' is a 1000 character long string containing only numbers): Unlike Scala where you would have the function sliding on string objects, we need to call chars

Parenthesis (and dots) (in Scala (as compared to Ruby))

I must say that I quite like what I encounter of the Scala language in Martin Odersky's online " Functional Programming in Scala " course at EPFL (as well as the course itself!). It is almost as expressive as my favourite language these days, Ruby . (Of course it will never really be able to match it on expressiveness, I'd say (the latter being duck typed, and the former being strictly typed (even with a most excellent type inference)).) One detail that I like about both languages is that parenthesis are (most of the times) optional. (By "(most of the time)" I mean that you (obviously) can't really skip them if it leads to problems with operator precedence.) This (can) really lead(s) to more readable code. Scala, however, (seemingly) has some inconsistencies when it comes to when  they are needed. Most notably the println  (and similar?) function(s). ((Quite possibly inspired by scripting languages) you have a quick way to display text at the user w

Fix upside down Skype video in Ubuntu 12.10 [UPDATED]

When launching Skype in 64-bit Ubuntu 12.10 on my Asus U35J the webcam image was all topsy-turvy. Since I don't live in Australia, or something (tsk-tsk), this was not really cutting it for me.  Some quick googling led me to this forum post:  http://forums.pcpitstop.com/index.php?/topic/198236-why-is-my-skype-video-showing-upside-down/   After making sure that the necessary packages was installed (notably  libv4l-0) I adapted the command from the forum post to: LD_PRELOAD=/usr/lib/i386-linux-gnu/libv4l/v4l1compat.so skype and voila, the image was OK. Next step is for this to be set to default, which seems to be outlined here (in steps 2 and 3):  http://pc-freak.net/blog/how-to-fix-upside-down-inverted-web-camera-laptop-asus-k51ac-issue-on-ubuntu-linux-and-debian-gnu-linux/  (Actually this post seems to cover most of what is useful from the forum post above...) UPDATE (19/04/2013): Since my laptop was working fine, I decided it was about time to fix it. Also I wanted to

Rocksmith, guitars, and music making on the iPad

The other day I invested in the long awaited (in the EU, that is) Rocksmith music game. This lets you play with your good old electric guitar or bass hooked up to your PS3, instead of the plastic gadgets made infamous in the 00s. I haven't had/taken the time to sit down rock out much with the game itself, but I have had a quick try at something that I guessed might be possible - to use the included cable, the so-called Real Tone Cable, as an interface between my guitar/bass and my iPad. Plugged, played, and lo and behold, it worked! Amp modelling and recording in Garageband, analog modelling filters with Moog's filtatron, etc. Classe ! (I have not confirmed the sound quality , but latency wise it is not too bad.)

Tabular data from PDFs with the help of Ruby

Sometimes I need to get data out of tables buried in PDFs, so I've written a tiny adaptable Ruby hack for when copying and pasting doesn't cut it - exploiting a couple of excellent libraries. Here's an example. Loading... (Admittedly not the most elegant of scripts, but hey! it gets the job done.) What I do first here is just load up the relevant libraries. (The pdf-reader I use is this one: https://github.com/yob/pdf-reader - installable with a simple command line 'sudo gem install pdf-reader' if you are using Ruby 1.9. (Drop the sudo if you're on Windows.)) Next up is to instantiate input pdf and output table. Straight forward enough. By examining the pdf I found the data I was interested in on pages 42 through 69, so a simple call to pages give me those -  pdf_reader . pages [ 42 . . 69 ] . I'll go through the text on each of them line by line and decide if they contain what I want based on regular expressions. These are my simple rules (in th

Gists on Blogger

Today I wondered how to add nice-looking gists to future posts here on my dynamic themed blog, and found the following solution: http://blog.moski.me/2012/01/gist-with-bloggers-dynamic-views.html  (Thanks Moski.) That's all. Update 10/12/2014: The script has now moved (Ref: https://stackoverflow.com/questions/24005430/moski-gist-blogger-not-loading-gists-in-chrome-for-gists-in-blogger-dynamic-vie ) Update 25/04/2020: This guide is better: https://betterstack.dev/blog/how-to-embed-a-github-gist-in-your-website/ .

XCOM revisited revisited

I figured I should give the XCOM demo another shot tonight, attempting what I thought was another level - since the game lets you choose to do either a mission in the US or China. Turned out to be the same level. Only skinned slightly differently. Fair enough. Also, I thought I could change the difficulty level, but alas, no. (Afterwards I found a guide on how to do it though:   http://www.gamefaqs.com/boards/655782-/64136437  (Not that I have tested it...)) Also, I figured I would try to capture one of the  Sid M  Thin Men alive. (Since the cute German scientist asked me nicely, but I just fragged them to smithereens last playthrough.) If I understand well, this is not possible in the demo, due to lack of things to stun things. Anyway - after two play-throughs I'm looking even more forward to the full game. Excellent. (If only it was delivered with a fair dose of time.)

XCOM Enemy Unknown Demo Out Today!

I just played through the demo  of Firaxis'  re-imagination  of one of my all time favourite computer games: UFO: Enemy Unknown . Short, but sweet. Very very sweet! (But very very short... (Quite possibly the shortest 5864 MB I've ever experienced.)) I don't have/take the time to game as much as I want to/ should, but this is a game that I have been looking forward to since it was announced.

Solid State FTW - Part 2: Asus U35J

Inspired by the success of the new SSD in my Mac Pro (and puzzled by a weird HD error) I went ahead and invested in a SSD for my 2010 Asus laptop as well. To get everything up and running the way I wanted took a bit more hacking than with the Mac, but now everything seems great - dual-booting Ubuntu 12.04 (main OS) and Windows 7 (mainly for gaming - till Steam is launched for Linux). This is what I did. I went for a 128 Gb CRUCIAL SSD m4 2,5" with a noname external 2,5" drive case. I tore out the old 600Gb-ish drive from the laptop, put it in the external case, and fit the new drive in its place. "Klok av skade", I went for installing windows first. (The Windows 98 installer overwrote my SuSE back in the day...) Windows 7 Since this Asus doesn't have an optical drive, I couldn't generate the "Recovery disc(s)" needed to reinstall the operating system. (Clever Asus.) The way I got around that was to boot Ubuntu 12.04 from a USB-stick, us

Solid State FTW - Part 1: Mac Pro (Early 2008)

Vroom! Recently I have added SSDs to things around me. That is, at least to two things. My laptop and my trusty old Mac Pro. Both experiences were fairly hitchfree, but I would like to point out one or two things. First one out was my Mac Pro. I decided to add a 256 GB SSD drive and went for a OCZ SSD Vertex 4 Series 256 Gb 2.5" SATA III drive with a Pro Sled 2.5" SSD/HDD. This fit snuggly in the 4th HD bay of my machine. I also wanted to take advantage of this commotion for doing a clean install of the newfangled Mountain Lion. (After making sure using good old googling that Cubase and Lightroom would still work.) I bought the upgrade in the AppStore, downloaded the installer, and followed this guide to get it onto a USB-stick:  http://www.macworld.com/article/1161069/make_a_bootable_lion_installer.html  . Reboot holding down the alt-key, chose to boot from USB and got a clean OSX in zero point swish. Since I still have my old HDs in there somewhere, and some bigger,

Progfun, Eclipse, JDKs, and Platform Agnosticism

Fattigfolket I'll be following the   Functional Programming Principles in Scala  course offered by Coursera this Autumn.   To get you started the good folks behind the course have provided lots of info on how to install necessary software etc. However, I ran into a problem, and it's subsequent solution, that I would like to document here. I'll be following the course a bit from here and a bit from there. Sometimes on my mac, sometimes on a linux laptop, sometimes on a windows machine. Anyway, I have my course files in my Dropbox and pick them up from there as needed on the various OSes. Problem was when opening the my scala projects in eclipse in OSX after compiling them under Windows, I got a cryptic " error in scala compiler null " and was unable to clean and (re)compile my scala programs. I realised that I was running JDK 6 on my mac and JDK 7 on anything else, so I figured that might have something to do with it. After following Mary Hamlin 's ans

First post!

This is my fourth attempt at blogging, I think... The first one was a quite active, at least in the beginning, blog on video games running from 2005 to 2009 - called "gglob", yes "gglob".  Catchy! (I wonder what the waybackmachine has to say about this. (Aha!:   2005  and  2009  (I could have sworn the layout looked better back then...)) The second one was a blog/cms-thingy on music making - running from 2006ish to 2009ish. (Not much on the waybackmachine on that one.) The third one was started mid 2010, is still going strong, and is a hidden, collaborative (co-authored with my better half) blog about the life of our son. This one will be about code, hacks, life, music, the universe, and everything. I hope. Basically I'll try to write up solutions to problems I encounter so that I won't have to solve them again (from scratch at least). And maybe others can benefit as well. Who knows. Anyway, "Velkommen".