Wednesday, April 25, 2007

iWaste: Apples aren't green :-(




I am the happy owner of a MacBook Pro, a nice computer with a really nice OS. Or, at least I used to be happy about it. I have had quite ambivalent feelings since I discovered that Apple is one of the biggest pigs when it comes to toxic chemicals in computers :-(

Then I heard that Apple is going to let the share holders vote about removing those toxics. Actually it seems that the board opposes doing it:

"Apple's board says it opposes the resolution, arguing that the company already has adequate environmental standards."

from computer world article



Yeah right! Your standards are about the lousiest in the industry! I hope that your sales drops if this doesn't change. Is the only way of being environmental about this to not buy Apple computers any more? If that is the case, I already bought my last mac. Doesn't Apple realize that this is going to drive their stock down?

Greenpeace is running a campaign where you write Steve and tell him that you are unhappy to. Consider doing it. Go to www.greenmyapple.org to check it out.

Update
Follow up: Apple has responded to criticism from Greenpeace

Sunday, April 22, 2007

Sleipner is dead

With a slight feeling of sadness, I updated the Sleipner website to reflect the fact that the project isn't actively being developed anymore. Neither me nor Jens has the time to do it and other more promising projects in the same category has surfaced since the dawn of Sleipner. But it was a fun project to work on back then :-)

My answer to ruby quiz 121

My solution to Ruby quiz 121.

Given some morse code without breaks between letters (which can have ambiguous interpretations), it will generate the words that the morse code can generate.

It's implemented as a recursive depth-first search in Ruby. Branches are expanded dynamically in the first_letters function.


require 'pp'

class Morse
@@alpha = {
"a" => ".-",
"b" => "-...",
"c" => "-.-.",
"d" => "-..",
"e" => ".",
"f" => "..-.",
"g" => "--.",
"h" => "....",
"i" => "..",
"j" => ".---",
"k" => "-.-",
"l" => ".-..",
"m" => "--",
"o" => "---",
"p" => ".--.",
"q" => "--.-",
"r" => ".-.",
"s" => "...",
"t" => "-",
"u" => "..-",
"v" => "...-",
"w" => ".--",
"x" => "-..-",
"y" => "-.--",
"z" => "--.."
}

def initialize
# turn around hash index to use morse chars index
@rev = {}
@@alpha.each { |k,v| @rev[v] = k.to_s }
end

# Returns all letters matching the morse str at this pos
def first_letters(morse, pos)
letters = []
@rev.keys.each do |k|
letters << k unless morse[pos..-1].scan(/^#{k.gsub(".","\\.")}.*/).empty?
end
letters
end

# Returns an array of words that matches 'morse' string
# It's basically a recursive function implementing depth-first search
def morse2words(morse, pos = 0 , seen = "")
solutions = []
first_letters(morse, pos).each do |l|
if morse.length == pos + l.length
solutions << "#{seen}#{@rev[l]}"
else
result = morse2words(morse,(pos+l.length),"#{seen}#{@rev[l]}")
solutions += result
end
end

solutions
end

# Converts a word to a morse string, used for testing
def word2morse(word)
morse = ""
word.each_byte { |b| morse << @@alpha[b.chr] }
morse
end
end


######################
# Test:

def test_word2morse
m = Morse.new
raise unless m.word2morse("sofia") == "...---..-....-"
end

def test_first_letters
m = Morse.new
raise unless m.first_letters(".", 0) == [ "." ];
raise unless m.first_letters("--.--..--.-.", 0) == ["--", "-", "--.", "--.-"]
end

def test_morse2words
m = Morse.new
sofia = "...---..-....-"
solutions = m.morse2words(sofia)
pp solutions
solutions.each do |s|
if m.word2morse(s) != sofia
puts "bad solution: #{s}"
puts "yields #{m.word2morse(s)} in morse"
raise
end
end
end

test_word2morse
test_first_letters
test_morse2words

substr in Ruby

Some times you want to extract the characters from a certain offset within a string. In Perl and PHP you have the substr function, e.g.


$str = "Hello world";
substr($str, 6); # --> "world"


In Ruby, this is done using the slice method of the String class. However, slice works a little different; slice is really an alias to [], so:


str = "Hello world"
str.slice(6) # --> 119
str[6] # --> 119

This may come as a surprise if you are used to substr; this returns the value of the character and not the rest of the string. Instead, we can do the same using negative indices, since negative indices count from the end of the string:


str.slice(6..-1) # --> "world"
str[6..-1] # --> "world"