Learn to Program, by Chris Pine

Thanks, donald. I don’t know why I missed that before.

Probably because it makes more sense to people new to Ruby when the code
is
in a more traditional format;

i.e.

File.open(file_name, ‘w’)

Personally the openess of Ruby cased problems for me when I first moved
over
(and still does sometimes). I love the fact that you can do things many
different ways in ruby, but when you’re trying to learn the language and
you’re given 10 different options with 10 different explanations it
becomes
much more confusing than only having 1 or 2 ways. :frowning:

On Jun 13, 2007, at 4:16 PM, Tm Bo wrote:

Thanks, donald. I don’t know why I missed that before.


Posted via http://www.ruby-forum.com/.

You will do well to make notes on File and read/write modes!
The basic implementation of this kind of thing is almost the same in
many lanugages, Ruby’s is no exception, the style of this comes
straight from C .
You will see it in PHP too, and a host of other languages… (all C’s
children and relatives.)
Some books fail to document this stuff well for beginners. they
either don’t want to go there yet, or they forget about it since they
assume you know it from other languages.
Mr. Pine just isn’t going there in his book. He’s focusing on other
subjects

This is my take on the same problem:

def roman_num number
set1 = [ 1, 5, 10, 50, 100, 500, 1000 ]
set2 = [ ‘I’, ‘V’, ‘X’, ‘L’, ‘C’, ‘D’, ‘M’ ]
numeral = []
while number > 0
if (number/(set1.last)) >= 1
roman = (number/(set1.last))
numeral.push((set2.pop)*roman)
number = (number%(set1.pop))
else
set2.pop
set1.pop
end
end
puts 'Old Roman Numeral is ’ + numeral.join + ‘.’
end

puts ‘Please enter a number to see what it is in old roman numerals.’
number = gets.chomp.to_i
while number < 1 || number > 3999
puts ‘Please enter a number between 1 and 3999’
number = gets.chomp.to_i
end
roman_num number

Jan_K wrote:

Chapter 9, exercise 2 (page 76)

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 on page 36.
For reference, these are the values of the letters used:
I = 1 V = 5 X = 10 L = 50
C = 100 D = 500 M = 1000

Solution:

def old_roman_number input

while input < 1 || input > 3999
puts ‘Please enter a number between 1 and 3999’
input = gets.chomp.to_i
end

m_mod = input%1000
d_mod = input%500
c_mod = input%100
l_mod = input%50
x_mod = input%10
v_mod = input%5

m_div = input/1000
d_div = m_mod/500
c_div = d_mod/100
l_div = c_mod/50
x_div = l_mod/10
v_div = x_mod/5
i_div = v_mod/1

m = ‘M’ * m_div
d = ‘D’ * d_div
c = ‘C’ * c_div
l = ‘L’ * l_div
x = ‘X’ * x_div
v = ‘V’ * v_div
i = ‘I’ * i_div

puts m + d + c + l + x + v + i

end

number = gets.chomp.to_i
old_roman_number(number)

On Feb 2, 2008 1:33 PM, Kelly T. [email protected] wrote:

  numeral.push((set2.pop)*roman)

number = gets.chomp.to_i
while number < 1 || number > 3999
puts ‘Please enter a number between 1 and 3999’
number = gets.chomp.to_i
end
roman_num number

Just for fun. Certainly not great for speed for large numbers, but the
integer max was low so…

H = Hash[*(([1,5,10,50,100,500,1000].zip %w|I V X L C D M|).flatten)]
def roman(n, s=“”)
H.keys.sort.reverse.each do |k|
s << (H[k] * (n / k))
n %= k
end
s
end
puts roman(ARGV[0].to_i)

The usage would simply be “ruby .rb ”

I’m sure someone could come up with a one-liner, though.

Todd

On Feb 2, 2008 7:47 PM, Todd B. [email protected] wrote:

s
end
puts roman(ARGV[0].to_i)

The usage would simply be “ruby .rb ”

I’m sure someone could come up with a one-liner, though.

Actually, better production code that keeps in line with OO concepts
would be…

class Integer
ROMAN = {
1 => “I”,
5 => “V”,
10 => “X”,
50 => “L”,
100 => “C”,
500 => “D”,
1000 => “M”
}

def roman
number, roman_string = self, “”
ROMAN.keys.sort.reverse.each do |key|
roman_string << (ROMAN[n] * (number/key)
n %= key
end
roman_string
end
end

#usage
puts 2137.roman

=> “MMCCCXVII”

I’m pretty sure there were better and more thorough examples at
Ruby Quiz - Roman Numerals (#22).

Todd

On Feb 2, 2008 8:49 PM, Todd B. [email protected] wrote:

1000 => "M"

end

#usage
puts 2137.roman

=> “MMCCCXVII”

I’m pretty sure there were better and more thorough examples at
Ruby Quiz - Roman Numerals (#22).

gsub(‘number’, ‘n’) on that code…and there’s a missing parenthesis
“)” number/key line

Yeah, yeah

That’s what I get for not using cut and paste :slight_smile:

Todd

BTW a little trick for later, if you i.e. have an array, and find
yourself using a counter, you can stop using the counter and instead use
array.size and check on that value.

I like “Learn to Program” but I never bothered to make the examples.
Like with homework in school many years ago - I did this in school. Not
at home… :slight_smile:

actually ROMAN[key] and not working with numbers having a 9.

On Feb 4, 2008 12:20 PM, Moises T. [email protected] wrote:

actually ROMAN[key] and not working with numbers having a 9.

Here’s the copy and pasted code…

class Integer
ROMAN = {
1 => “I”,
5 => “V”,
10 => “X”,
50 => “L”,
100 => “C”,
500 => “D”,
1000 => “M”
}

def roman
n, roman_string = self, “”
ROMAN.keys.sort.reverse.each do |key|
roman_string << (ROMAN[key] * (n/key))
n %= key
end
roman_string
end
end

#usage
puts 2319.roman

=> “MMCCCXVIIII”

cheers,
Todd

Hey everyone I was having a slight problem with one of the examples in
the book. I was doing the ‘99 beers’ example and ran into a problem at
the end. The task asks you to create a prog that will recite the lyrics
for ‘99 beers on the wall’ which seemed fairly easy to do but I wanted
to make it more challenging for myself and make it more grammatically
correct. So I tried to create an if statement to change the word
‘bottles’ to ‘bottle’ when bottles = 1. I have seen another example
creating a working solution but requires many more lines of code and I
was trying to do it in a more clever way (maybe it is backfiring). I
just can’t figure out why it is not working properly. Anyway here is
the code:

bottles = 99
bword = ' bottles'
while bottles != 0
  puts(bottles.to_s + bword + " of beer on the wall!")
  puts(bottles.to_s + bword + " of beer!")
  puts("Take one down and pass it around!")
  bottles = bottles - 1
  puts(bottles.to_s + bword + " of beer on the wall!")
  puts ''
  if bottles == 1
    bword = ' bottle'
  else
    bword = ' bottles'
  end
end

and here is the output that isn’t showing up as planned:

2 bottles of beer on the wall!
2 bottles of beer!
Take one down and pass it around!
1 bottles of beer on the wall!

1 bottle of beer on the wall!
1 bottle of beer!
Take on down and pass it around!
0 bottle of beer on the wall!

It doesn’t work for the first line with a 1 on it and I wanted it to
switch back to ‘bottles’ with the zero on the last line. Any thoughts?

On 28 May 2008, at 18:05, Patrick Hummer wrote:

bword = ’ bottle’
else
bword = ’ bottles’
end
end

You need to reorder your logic slightly so that bword is changed as
soon as the bottle count is decremented:

bottle = 99
bword = ‘bottles’
while bottles != 0
puts(bottles.to_s + bword + " of beer on the wall!“)
puts(bottles.to_s + bword + " of beer!”)
puts(“Take one down and pass it around!”)
bottles = bottles - 1
if bottles == 1
bword = ’ bottle’
else
bword = ’ bottles’
end
puts(bottles.to_s + bword + " of beer on the wall!")
puts ‘’
end

Ellie

Eleanor McHugh
Games With Brains
http://slides.games-with-brains.net

raise ArgumentError unless @reality.responds_to? :reason

Jan_K wrote in post #55253:

Does anyone know where I can find the solutions to the exercises in
this book:

Learn to Program, by Chris Pine

I’m stuck in “Flow Control” chapter, specificailly the “Extend Deaf
Grandma” exercise.

I feel like beating the shit out of some punching bag somewhere. Is
this a normal reaction when one is trying to learn to program for the
first time?

Hi: I googled the book and answers and here’s where they are:

Pine’s book is, for it’s price, rubbish! You can find better on the web
for free. And this applies to most of the Ruby books out there
See how many actually create a full program with an interface?

Hi! I’m newbie and this are my first steps. This is my solution for
Modern Roman numerals using arrays and each.

romans =
[1000,‘M’],[900,‘CM’],[500,‘D’],[400,‘CD’],[100,‘C’],[90,‘XC’],[50,‘L’],[40,‘XL’],[10,‘X’],[9,‘IX’],[5,‘V’],[4,‘IV’],[1,‘I’]
result = []

puts “Please enter a number”
num = gets.chomp.to_i

print “\n” + num.to_s + " in Modern Roman Numerals is: "

romans.each do |nroman|
number = nroman[0]
letter = nroman[1]

while num >= number
result.push letter
num -= number
end

end

print result.join

cheers!!

Martin

On Jan 23, 2013, at 09:41 , Martin A. [email protected]
wrote:

romans.each do |nroman|
number = nroman[0]
letter = nroman[1]

can become:

romans.each do |nroman|
number, letter = nroman

can become:

romans.each do | (number, letter) |

can become:

Jan_K wrote:

So if I have any more questions I’ll post the exercise instructions
first.

Good idea; it’s sure to avoid further confusion, and I’m sure Mr. Pine
wouldn’t mind this kind of use of his text.

Thanks for your help so far Dave. I was completely stuck in that Flow
Control chapter and pretty much became content that another attempt at
learning programming has successfully failed (just like the previous
half dozen times). It’s amazing how hard it is to grasp incredibly
fucking simple concepts for the first time.

You’re more than welcome. I’m chuffed that you say this, and very glad
to have helped.

I love teaching programming. I learnt the basics of programming QBasic
in a day, back in grade 6, from a friend (probably a little less than
the equivalent of what you’re up to now). And it is difficult and
strange, new vocabulary, “strings”, “methods”, but it’s so useful once
you’ve got it. A lot of repetitive tasks can be reduced to a brain
exercise by automating them. (See thread, “Does Ruby simplify our tasks
and lives?”) And knowing some of these things help you understand
computers (and bend them to your every whim bwahahaha… oh, you’re
still reading?)

But mostly it’s just fun.

Cheers,
Dave