#6/1000: landMass.rb | A Recursion Program

=begin
From Mr. Chris Pine’s wonderful instructional text “Learn to Program (Facets of Ruby):

“Let’s say we have an 11×11 world (represented as an array of arrays…basically just a grid)
and that we want to find the size of the continent in the middle (that is, the continent
of which tile (5,5) is a part). We don’t want to count any land tiles belonging to any of
the other continents. Also, as in Civilization III, we’ll say that tiles touching only at
the corners are still considered to be on the same continent (since units could walk along
diagonals). But before we get to the code, let’s solve the problem in English first. My
initial plan was to look at every tile on the map, and if that tile is a land tile on the
continent I’m looking for, we add 1 to the running total. The problem , though, is how do
we know whether a land tile is on the same continent as some other land tile? There are
ways to solve this problem, but they all seemed to be too messy; either I was keeping
track of lots of information I didn’t feel like I needed or I seemed to be doing the same
computation over and over again. But then I thought, hey, two tiles are on the same
continent if you can walk from one to the other. (That was essentially the operating
definition of continent in Civilization III.) So that’s how the code should work! First,
you count the spot you are standing on (duh); in this case, that means tile (5,5). Then,
you send out eight little guys, one in each direction, and tell them to count the rest of
the continent in that direction. The only rule is that no one can count a tile that
someone else has already counted. When those eight guys return, you add their answers to
your already-running total”

Pine, Chris (2009-03-28). Learn to Program (Facets of Ruby) (Kindle Locations 2109-2120).
Pragmatic Bookshelf. Kindle Edition.
=end

# for ease of visual recognition:
M = ‘land’
o = ‘water’

world =[[o,o,o,o,o,o,o,o,o,o,o],
[o,o,o,o,M,M,o,o,o,o,o],
[o,o,o,o,o,o,o,o,M,M,o],
[o,o,o,M,o,o,o,o,o,M,o],
[o,o,o,M,o,M,M,o,o,o,o],
[o,o,o,o,M,M,M,M,o,o,o],
[o,o,o,M,M,M,M,M,M,M,o],
[o,o,o,M,M,o,M,M,M,o,o],
[o,o,o,o,o,o,M,M,o,o,o],
[o,M,o,o,o,M,o,o,o,o,o],
[o,o,o,o,o,o,o,o,o,o,o]]

puts ”
puts “To calculate the land mass of your world,”
puts “please enter a positive integer for location x.”
x=gets.chomp.to_i
while x < 0
puts “That’s not a positive integer… try again.”
x=gets.chomp.to_i
end
puts “Please enter a positive integer for location y.”
y=gets.chomp.to_i
while y < 0
puts “That’s not a positive integer… try again.”
y=gets.chomp.to_i
end

def landMass world, x, y
if world[y][x] != ‘land’ # if it’s not land, we don’t count it
return 0
end
# count the first tile
size=1
world[y][x] = ‘counted land’
# now accumulate land mass (i.e. size) via recursion of the landMass method
size = size + landMass(world, x-1, y-1)
size = size + landMass(world, x, y-1)
size = size + landMass(world, x+1, y-1)
size = size + landMass(world, x-1, y)
size = size + landMass(world, x+1, y)
size = size + landMass(world, x-1, y+1)
size = size + landMass(world, x, y+1)
size = size + landMass(world, x+1, y+1)
size
end
puts “The land mass of your world is ” + landMass(world,x,y).to_s + “.”
puts ”

Word: Sarah Lewis on Mastery

“Masters are not experts because they take a subject to its conceptual end. They are masters because they realize that there isn’t one.”

Lewis, Sarah (2014-03-04). The Rise: Creativity, the Gift of Failure, and the Search for Mastery (Kindle Locations 513-514). Simon & Schuster. Kindle Edition.

Though I have just begun reading this book, I am impressed.  Not only does it cover a complex subject well, it does so with eloquence.  It is inspiring as well as interesting.  It is enjoyable as well as engaging.  It is a good book; and I am sure I will be posting many quotes, both original as well as quotes of quotes, as I read it.

 

#5/1000: roNums.rb | An Integer Conversion Method

=begin
old-school Roman numerals. In the early days of Roman numerals,
the Romans didn’t bother with any of this new-fangled subtraction
“IX” nonsense. No sir, it was straight addition, biggest to littlest—
so 9 was written “VIIII,” and so on. Write a method that when passed
an integer between 1 and 3000 (or so) returns a string containing the
proper old-school Roman numeral. In other words, old_roman_numeral 4
should return ‘IIII’. Make sure to test your method on a bunch of
different numbers. Hint: Use the integer division and modulus methods
here . For reference, these are the values of the letters used:
I = 1 V = 5 X = 10 L = 50 C = 100 D = 500 M = 1000

Pine, Chris (2009-03-28). Learn to Program (Facets of Ruby)
(Kindle Locations 2011-2017). Pragmatic Bookshelf. Kindle Edition.
=end

def roNum num
roman = ”
roman << ‘M’*(num/1000)
roman << ‘D’*(num%1000/500)
roman << ‘C’*(num%500/100)
roman << ‘L’*(num%100/50)
roman << ‘X’*(num%50/10)
roman << ‘V’*(num%10/5)
roman << ‘I’*(num%5/1)
puts roman
end

while true
puts ”
puts “Please give me a positive integer to convert.”
num = gets.chomp.to_i
if num <= 0
puts “That is not a positive integer.”
else
roNum num
puts ”
break
end
end

#4/1000: favFood | Working w/ Return Values

=begin
working with return values

(From Chris Pine’s “Learning to Program”)
I did two different things in that program: with favorite_food
I used explicit returns, and in favorite_drink I didn’t.
Depending on the feel of the code, I’ll write a method one way
or the other. If I’m trying to prune off special cases, I might
use returns and leave the general case on the last line. If I
think the options are all of relatively equal importance, I
might use elsif and else like that…
feels more egalitarian , you know?

citation: Pine, Chris (2009-03-28). Learn to Program (Facets of Ruby)
(Kindle Locations 1933-1937). Pragmatic Bookshelf. Kindle Edition.
=end

def favFood name
if name == ‘Lister’
return ‘vindaloo’
end

if name == ‘Rimmer’
return ‘mashed potatoes’
end
‘hard to say…maybe fried plantain?’
end

def favDrink name
if name == ‘Jean-Luc’
‘tes, Earl Grey, hot’
elsif name == ‘Kathryn’
‘coffee, black’
else
‘perhaps…horchata?’
end
end

puts favFood(‘Rimmer’)
puts favFood(‘Lister’)
puts favFood(‘Cher’)
puts favDrink(‘Kathryn’)
puts favDrink(‘Oprah’)
puts favDrink(‘Jean-Luc’)

Rites of Love and Math: Ed Frenkel at the Intersection of Art and Science

A confession, I have not seen this film (yet), but I just finished Prof. Frenkel’s book, “Love and Math”, and it fits perfectly into my growing curiosity with the creativity of science, and the investigations of art, recognized as the twins they actually are.  More on this later, but it’s in the air… and it’s a good thing to finally re-realize.

#3/1000: lilPest (a nod to Chris Pine)

=begin

it should be noted that I am following the wonderful Chris Pine book “Learn To Program (Facets of Ruby) from where, at least for the next few weeks, my lessons will come.

This program, copied from my terminal irb, demonstrates how local variables are isolated from variables outside of the local def

=end

2.1.0 :001 > toughVar = “Ya can’t touch this!”

 => “Ya can’t touch this!”

2.1.0 :002 > def lilPest toughVar

2.1.0 :003?>   toughVar = nil

2.1.0 :004?>   puts “HARHARHAR! I ruined your variable!”

2.1.0 :005?>   end

 => :lilPest

2.1.0 :006 > lilPest toughVar

HARHARHAR! I ruined your variable!

 => nil

2.1.0 :007 > puts toughVar

Ya can’t touch this!

 => nil

2.1.0 :008 >

#2/1000: 99 Bottles of Beer | A Classic Loop

# 99 Bottles of Beer
puts “How many bottles of beer shall we start with?”
beer = gets.chomp
puts “”

while beer != 1
puts beer.to_s + ” bottles of beer on the wall,”
puts beer.to_s + ” bottles of beer…”
puts “take one down,”
puts “pass it around…”
beer = beer.to_i – 1
puts beer.to_s + ” bottles of beer on the wall!”
puts “”
end

puts beer.to_s + ” bottle of beer on the wall,”
puts beer.to_s + ” bottle of beer…”
puts “take one down,”
puts “pass it around…”
puts “”
puts “”
puts “Oh no! We drank all the beer!”
puts “”