Learn to Program, by Chris Pine

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

http://pine.fm/LearnToProgram/

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?

···

--
Jan

[snip]

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?

I've been programming for over 20 years and I *still* feel like that
sometimes. While I can't say what way is definitively best to deal
with it, I tend to take a five or ten minute walk when I get to
feeling like that. Often, by time that that's done, I've got a fresh
idea for how to approach the problem, or I've thought of something I
may be missing.

Cheers,

kjw

···

On 29/03/06, Jan_K <non@none.com> wrote:

*snip*

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?

:slight_smile:

Fancy describing the problem, and seeing if someone's got some thoughts on it? Top programming tip - it's amazing how often trying to explain a problem will suddenly make you realise what the answer is. Curious, but true.

···

On 29 Mar 2006, at 22:33, Jan_K wrote:

You could use until or if (or both). You could make a variable the would
count how many times in a row you said "BYE" to grandma, starting at 0 of
cause, and make it add 1 each times and reset it to 0 if you did not say
"BYE".

Here is an example:

counter = 0

Until (counter == 3)
   tell_grandma = gets.chomp
   if (tell_grandma == "BYE")
      counter += 1
   end
end

Just find a way to integrate with your current grandma program :slight_smile:

···

2006/3/29, Jan_K <non@none.com>:

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

http://pine.fm/LearnToProgram/

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?

--
Jan

--
"winners never quit, quitters never win"

Sometimes I find myself leaving a problem without trying to analyze it,
and then when I come back again, it works. Most of the time, I can't
figure out why... I just scratch my head and think "What did I do
different _this time_?"

The encouraging thing about this is that as you get better, the time
between the head scratching gets longer.

Another resource you can try Ruby on when you get frustrated with Pine
is http://tryruby.hobix.com/
This is interactive and fun, and was written by someone who is
certifiably insane. (He wrote Poignant's Guide.)

Paul

Jan_K wrote:

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

http://pine.fm/LearnToProgram/

I'm stuck in "Flow Control" chapter, specificailly the "Extend Deaf
Grandma" exercise.

  I'm stuck trying to do the part to make it do '99 bottles of beer on the
wall' song. I get started in what I hope will work and next thing I know I
have way too many variables and nothing makes sense. I even did something
that made it loop the integer 0. I had to Ctrl-c on that one. <sigh>

  Here's what I quit trying at, because no matter how many breaks I took or
whatever, it just wasn't making any sense. :

puts '99 bottles of beer on the wall, 99 bottles of beer,'
'take one down, pass it around,'
'98 bottles of beer on the wall!'
song = '99 bottles of beer on the wall, 99 bottles of beer,
take one down, pass it around,'
song = 99
song2 = '98 bottles of beer on the wall!'
song2 = 99 - 1
last = '1 bottle of beer on the wall'
last = 1

if song2 == last
        puts 'Yay!'
else
        if song2 - 1
        puts
        while song == 1
        end
endputs '99 bottles of beer on the wall, 99 bottles of beer,'
'take one down, pass it around,'
'98 bottles of beer on the wall!'
song = '99 bottles of beer on the wall, 99 bottles of beer,
take one down, pass it around,'
song = 99
song2 = '98 bottles of beer on the wall!'
song2 = 99 - 1
last = '1 bottle of beer on the wall'
last = 1
if song2 == last
        puts 'Yay!'
else
        if song2 - 1
        puts
        while song == 1
        end
end

  I too wish the author had put examples, say on some pages in the back or
something.

Here's the solution to the Leap Years exercise in the same chapter:

(everything looks OK to me)

···

------------------------------------------------------------
puts 'Please enter the starting year'
input = gets.chomp
puts 'Please enter the ending year'
input2 = gets.chomp
puts ''
puts 'The following are leap years:'

input = input.to_i - 1

while input < input2.to_i
  input = input + 1
    if input%4 == 0 || input%400 == 0 && input%100 != 0
      puts input.to_s
    end
end
---------------------------------------------------------

I might continue posting the solutions for the rest of the book if I
decide to keep on reading.

This way I won't develop any bad habits (because hopefully the
solutions will be scrutinized by at least one other person) and it'll
help anyone else reading the book who gets stuck. I doubt the author
of the book will ever get around to publishing the "official"
solutions.

Solution for the 1st exercise in chapter 8:

(I'm skipping the 2nd exercise - just a bunch of busywork)

···

-----------------------------------------------------------------------
input = []

while input.last != ''
  input.push gets.chomp
end

puts input.sort
-----------------------------------------------------------------------

Jan_K wrote:

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?

Yes, that is a typical outward manifestation, or projection, of the
initial programming learning process. And, no, it won't go away. But
there are a few inner thnings on which one needs to focus, as well. To
wit: start learning to "think outside of the box", be prepared for your
mind to "play tricks" on you, and don't fight the fact that virtually
all of the difficulties you will face at first will be "logic errors" in
your own thinking and not some anomally of the computer. Adopting this
attitude will help you maintain calmness and reduce stress. Programming
is mentally hard work. But the reward is well worth it. Plus, we here
in the programming community are available to help you!

···

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

Chapter 9, exercise 1 (page 76)

"Fix up the ask method. That ask method I showed you was alright,
but I bet you could do better. Try to clean it up by removing the
variables good_answer and answer. You’ll have to use return to
exit from the loop. (Well, it will get you out of the whole method,
but it will get you out of the loop in the process.) How do you
like the resulting method? I usually try to avoid using return
(personal preference), but I might make an exception here."

Solution:

···

--------------------------------------------------------------------------------------
def ask question
  while true
    puts question
    reply = gets.chomp.downcase
    if reply == 'yes'
      return true
    elsif reply == 'no'
      return false
    else
      puts 'Please answer "yes" or "no".'
    end
  end
end

puts 'Hello, and thank you for...'
puts
ask 'Do you like eating tacos?'
ask 'Do you like eating burritos?'
wets_bed = ask 'Do you wet the bed?'
ask 'Do you like eating chimichangas?'
ask 'Do you like eating sopapillas?'
puts 'Just a few more questions...'
ask 'Do you like drinking horchata?'
ask 'Do you like eating flautas?'
puts
puts 'DEBRIEFING:'
puts 'Thank you for...'
puts
puts wets_bed
--------------------------------------------------------------------------------------

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)
----------------------------------------------------------------

Chapter 9, exercise 3 (page 76)

Modern Roman numerals. Eventually, someone thought it would
be terribly clever if putting a smaller number before a larger one
meant you had to subtract the smaller one. As a result of this
development, you must now suffer. Rewrite your previous method
to return the new-style Roman numerals, so when someone calls
roman_numeral 4, it should return 'IV'.

Solution:

···

----------------------------------------------------------------
def roman_numeral 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

  if i == 'IIII' && v != 'V'
    i = 'IV'
  elsif i == 'IIII'
    v = 'IX'
    i = ''
  end

  if x == 'XXXX' && l != 'L'
    x = 'XL'
  elsif x == 'XXXX'
    l = 'XC'
    x = ''
  end
  
  if c == 'CCCC' && d != 'D'
    c = 'CD'
  elsif c == 'CCCC'
    d = 'CM'
    c = ''
  end

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

number = gets.chomp.to_i
roman_numeral(number)
----------------------------------------------------------------

I'm not quite sure there are solutions. However, as a fellow beginning
programmer, I would enjoy discussing our solutions since I finished that
book only one week ago.

Cheers,

Marco

···

On Thursday, March 30, 2006, at 6:33 AM, Jan_K wrote:

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

http://pine.fm/LearnToProgram/

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?

--
Jan

--
Posted with http://DevLists.com. Sign up and save your mailbox.

Hello all. Thanks to all the posters who came before me! You've been
tremendous help. I have a quick question on Chapter 11 - Reading and
Writing, Section 11.3:

<quote>
file_name = 'TestFile.txt'
test_string = 'Nothing!'

File.open file_name, 'w' do |f|
  f.write test_string
end

read_string = File.read file_name
puts(read_string == test_string)
</quote>

I poked around the web, but I can't find out what the 'w' is for. Can
anyone shed any light? Muchas gracias in advance.

-Todd

···

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

Jan_K wrote in post #55253:

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

http://pine.fm/LearnToProgram/

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:

···

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

I recommend dancing. Reading fiction also often works for me.

kj is right, I've been programming for almost 15 years now and
frustration is a frequent companion.

···

I've been programming for over 20 years and I *still* feel like that
sometimes. While I can't say what way is definitively best to deal
with it, I tend to take a five or ten minute walk when I get to
feeling like that. Often, by time that that's done, I've got a fresh
idea for how to approach the problem, or I've thought of something I
may be missing.

forgot to make it set counter to 0 if it wasn't "BYE", but I'm sure you can
figure that out using else.

···

2006/3/29, Jeppe Jakobsen <jeppe88@gmail.com>:

You could use until or if (or both). You could make a variable the would
count how many times in a row you said "BYE" to grandma, starting at 0 of
cause, and make it add 1 each times and reset it to 0 if you did not say
"BYE".

Here is an example:

counter = 0

Until (counter == 3)
   tell_grandma = gets.chomp
   if (tell_grandma == "BYE")
      counter += 1
   end
end

Just find a way to integrate with your current grandma program :slight_smile:

2006/3/29, Jan_K <non@none.com>:

> Does anyone know where I can find the solutions to the exercises in
> this book:
>
> http://pine.fm/LearnToProgram/
>
>
> 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?
>
> --
> Jan
>
>

--
"winners never quit, quitters never win"

--
"winners never quit, quitters never win"

kj WOOLLEY wrote:

···

On 29/03/06, Jan_K <non@none.com> wrote:
[snip]

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?

I've been programming for over 20 years and I *still* feel like that
sometimes. While I can't say what way is definitively best to deal
with it, I tend to take a five or ten minute walk when I get to
feeling like that. Often, by time that that's done, I've got a fresh
idea for how to approach the problem, or I've thought of something I
may be missing.

Good advice.

See also the Programming and Development episodes of Ward's "EPISODES:
A Pattern Language of Competitive Development" available
at PPR: Pattern Language Titles and the episode failure notes from
his Seattle XP talk: SEAPIG

Regards,
--
Bil
http://fun3d.larc.nasa.gov

kj WOOLLEY wrote:

[snip]

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?

I've been programming for over 20 years and I *still* feel like that
sometimes. While I can't say what way is definitively best to deal
with it, I tend to take a five or ten minute walk when I get to
feeling like that. Often, by time that that's done, I've got a fresh
idea for how to approach the problem, or I've thought of something I
may be missing.

Well, might I say that he's picked a good language to try out. With
other languages, it would be much, much worse. :slight_smile:

A major reason I moved to Ruby is because it vastly reduces (or
sometimes eliminates) frustration while programming. Something [I]
frequently encountered with other languages (C, C++ (esp. Windows
stuff), Java, Perl, Tcl).

Pistos

···

On 29/03/06, Jan_K <non@none.com> wrote:

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

JB wrote:

I'm stuck trying to do the part to make it do '99 bottles of beer on the
wall' song. I get started in what I hope will work and next thing I know I
have way too many variables and nothing makes sense. I even did something
that made it loop the integer 0. I had to Ctrl-c on that one. <sigh>

Here's what I quit trying at, because no matter how many breaks I took or
whatever, it just wasn't making any sense. :
...
I too wish the author had put examples, say on some pages in the back or
something.

But if you ask this group, you can get interactive answers!

Let's start again with that one. We need a loop counting down from 99. So
first, we need a counter.

beer = 99

Now, we need to count it down.

beer = beer - 1

But we need to do that repeatedly - it goes in a loop.

beer = 99
while (something to end the loop?)
  beer = beer - 1
end

How long should we keep looping? We don't want to go past zero. In other
words, we loop while our counter is zero or more.

beer = 99
while beer >= 0
  beer = beer - 1
end

OK, now we've got the basic structure of the program - one loop, beer starts
at 99, and decreases until it gets to 0. But there's no output yet.

See how you go putting some flesh on that skeleton, JB - post back when
you're done. (I found learning to program it was better to write than read,
so put aside Herr Froelich's solution and make your own!)

Cheers,
Dave