Kata 6

I took a swipe at implementing Dave Thomas’ Kata 6 which is an assignment dealing with anagrams. The goal is to parse a list of 45000-ish words, finding all the words that are anagrams of other words in the file. Dave claims there are 2,530 sets of anagrams, but I only got 2,506. I’m not sure where the disconnect is, but here’s my solution. I welcome any comments and critiques.

 words = IO.readlines("wordlist.txt")  anagrams = Hash.new([])  words.each do |word|     base = Array.new     word.chomp!.downcase!      word.each_byte do |byte|         base << byte.chr     end      base.sort!      anagrams[base.to_s] |= [word] end  # Find the anagrams by eliminating those with only one word anagrams.reject! {|k, v| v.length == 1}  values = anagrams.values.sort do |a, b|     b.length  a.length end  File.open("anagrams.txt", "w") do |file|     values.each do |line|         file.puts(line.join(", "))     end end  largest = anagrams.keys.max do |a, b|     a.length  b.length end  puts "Total: #{anagrams.length}" # puts "Largest set of anagrams: #{values[0].inspect}" #  print "Longest anagram: #{anagrams[largest].inspect} " #  puts "at #{largest.length} characters each" 

Update: Of course, 10 seconds after uploading the code, I see something I could change. Instead of sorting the anagram hash descending by array length, I could have done the following:

 longest = anagrams.to_a.max do |a, b|     a[1].length  b[1].length end 

This will sort and pull the largest one off. The key is bucket 0 and the interesting array is in bucket 1.

The Food Industry Strikes Back

In an effort to inform and entertain people about the spate of ridiculous lawsuits being filed by lard-asses against McDonald’s and other food makers for “making people fat”, a food industry group is launching a series of ads that mock the lawsuits. This Washington Times article has the details. Here’s a funny snippet:

The parodies start with “According to the latest study,” and continue with absurd claims such as “If you eat a wheelbarrow full of cheese fries each week, the cheese fries are to blame when people call you Big Daddy Largepants.”

Come on, folks! Take responsibility for the size of your ass! I’ve taken responsibility for mine! Join Weight Watchers, go on the Atkins Diet, get in The Zone. Just do something! It’s your fault that you are fat, not the people who make the food!

Letterman Funny

Last night David Letterman‘s Top 10 List was the “Top Ten Things The Iraqi Information Minister Has Admitted Since Being Captured”. My favorite was number 10:

Okay, Iraq didn’t win the war. It was a tie.

I almost sprayed Pepsi from my nose when I read that. It’s just like the Black Knight in Monty Python and the Holy Grail who, after getting both arms and both legs hacked off by Arthur says to him:

All right. We’ll call it a draw.

Hilarious! And not too different from some of the absurdities that the “Information Minister” really said during the war!

I Hate Fade-Outs…

I really hate songs that end by just fading out instead of coming to an end. It always strikes me as a cop out; the composer couldn’t think of a good way to end it, so they just turn down the volume until it’s gone. But come on! What are these guys going to do when/if they ever play to a live audience? It’s very annoying.

I will say that I’ve heard two bluegrass bands, The Dillards and The Del McCoury Band do live fade-outs that were technically amazing (they actually kept playing softer until you could barely hear them), but I still felt like they just couldn’t come up with a good way to actually end the dang song… Maybe it’s just me…

“Free” WiFi

I found two links to this BusinessWire story this morning; one at /. and the other at Boing Boing about the island country of Niue rolling out nationwide “free” wireless networking. Why can’t people understand that nothing from the government is ever free? Anything provided by government was paid for from the money seized from the citizens in taxes. And in most countries that have “progressive” tax policies, the majority of that money was taken from the “wealthy.” So everyone now has “free” wifi because some working stiff had to pony up to the tax man.

Big in Nigeria… Update!

Yesterday I wrote about how I had gotten two emails trying to get me to help get loads of cash out of Nigeria and Senegal. I replied to one of them with a single line: “Blow me.” Believe it or not, I actually got a response from the spammer. He said that he didn’t understand the contents of my email and would I please respond with an explanation. Ah, the mind reels at the possibilities…