Monthly Archives: July 2010

Old Ruby Script : Rnaarerge ltreets iisnde wdors and sltil raed tehm

This little gem comes from October 20, 2006. I had just heard about the research saying that as long as you keep the first and last letters of a word in place the order of the other letters doesn’t matter. Supposedly your mind will still figure out the correct order. It turns out that this e-mail forward wasn’t as impressive as it made out to be, but the script was fun to make anyways. Here’s what the e-mail said:

Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it deosn’t mttaer in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht the frist and lsat ltteer be at the rghit pclae. The rset can be a toatl mses and you can sitll raed it wouthit porbelm. Tihs is bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the wrod as a wlohe.

I was playing with Ruby at the time, so I put together a short script to make those kinds of sentences. Here’s the code:

#!/usr/bin/env ruby
puts "Enter your phrase to be mixed"
def swap(a,b) ($x[a],$x[b] = $x[b],$x[a]) if (a > 0 && a < $x.length - 2) end
myarray = STDIN.readline.split(' ').each {|$x| $x.split('').each_index { |w| swap(w, rand($x.length - 2) + 1)}}
puts myarray.join(' ')

If you want to try it, save it to as a Ruby file on a computer with Ruby installed, and run it from the command line. It will ask you for a line of text. Each word (or space separated group of characters) will keep its first and last character in place, while the others will be mixed up.

Apparently I used this script to explain Ruby to someone, as I also found an explanation file:

#!/usr/bin/env ruby

# prints the prompt
puts "Enter your phrase to be mixed"

# defines a new subroutine named 'swap(a,b)'. swap takes two parameters
def swap(a,b)
# In ruby, you can add an if after the operations, as I did here. The operation swaps the items at indexes a and b
# The if part makes sure we're not swapping the first or last charachters
($x[a],$x[b] = $x[b],$x[a]) if (a > 0 && a < $x.length - 2)
# End of the subroutine
end

#sets the variable myarray to the results of the operation
#STDIN.readline -- Get one line from stdin (ie. from the terminal)
#.split(' ') -- split the preceding object on each space and put into an array
#.each -- for each element in the array (handle defined here as global variable |$x|) do what follows the variable declaration
#$x.split('') -- split on each letter and put into the array
#.each_index -- similar to each, but instead, the variable (|w|) is the index of each element, one at a time
#swap(w,z) -- call the swap subroutine with parameters of w and z
#rand($x.length -2) + 1) -- gives a random index in the word not including the first or last letter
myarray = STDIN.readline.split(' ').each {|$x| $x.split('').each_index { |w| swap(w, rand($x.length - 2) + 1)}}

#prints each element of the myarray variable with a space in between
puts myarray.join(' ')

So that’s that. Maybe it’ll help with learning Ruby? Or making funny sentences? Or wasting your time?

Posted in Computers, Something Interesting | Tagged | 2 Comments

Grandpa Knew What He was Talking About

Michigan is a Prime Target of Terrorist Attacks?

Old people like to tell stories and my granpda was no exception. He didn’t usually make up stories, but sometimes it was obvious that he had gotten his information from a single source a single time and you didn’t always know how right or wrong he was.

One such story Grandpa used to tell me about how Michigan was a prime terrorist targets because the US had radio towers there to control the nuclear sub fleet. I always thought that was weird, because wouldn’t the army have more than one radio station? And why would you put radio stations for subs so far from the rest of the Navy?

Turns out he was right about the radio tower part (still not convinced on the terrorist target part).

How and Why of Submarine Radio in Michigan (and Wisconsin)

FM radio operates in the Mega Hertz range, eg 106.9 FM means that the station operates a frequency modulated radio broadcast on the 106.9 MHz, or 106,900,000 Hertz, wavelength. That means the radio waves are relatively short and you can use a relatively short antenna to send and receive that radio signal. AM uses longer radio waves and needs longer antennas which is why Walkmans and cell phones can include FM radio easily, but not AM.

To broadcast radio through the water to the depth that submarines operate you need to use extremely low frequencies in the 76 Hz range.

Republic, MI and Clam Lake, WI had Extremely Low Frequency radio stations that did indeed broadcast to the US submarine fleets. The radio stations couldn’t be built just anywhere because the antennas need to be “2,140 to 3,726 miles” (thanks Wikipedia!) long. Since a wire antenna that big would be unwieldy, the army hooked 26 mile wire antennas into the bedrock and let the earth be a big antenna. The locations in Wisconsin and Michigan had the right kind of rock to make that easier to do.

The radio stations have been decommissioned since 2004 which makes me wonder how they’re communicating with subs today.

Reading and Learning Comes Full Circle

My grandpa had lots of interesting stories because he was always learning. He subscribed to and read Scientific American and the Reader’s Digest and the Stars and Stripes magazines, among other publications. He was a news man that loved news. I can’t count the times he would literally pull me to his side, and point emphatically at some section of an article which he had circled and underlined with a ball point pen.

I like to think that he learned about these ELF radio stations from an article in one of his magazines. You see, I also love reading news and science articles. Most of my reading today is done online, including today’s big find. Lockheed Martin and the Department of Defense are working on making two-way communications with submarines possible.

Let me state for the record that I fully intend to tell my grandkids about this some day.

Footnote:

More information about the Clam Lake location can be found at the Federation of American Scientists website.
Posted in Moore's Ramblings, Something Interesting | Tagged , , , | 1 Comment