[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Learn to Program, by Chris Pine

Gregor

3/29/2006 9:33:00 PM

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

http://pine.fm/Learn...


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
62 Answers

kj WOOLLEY

3/29/2006 9:45:00 PM

0

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 thatsometimes. While I can't say what way is definitively best to dealwith it, I tend to take a five or ten minute walk when I get tofeeling like that. Often, by time that that's done, I've got a freshidea for how to approach the problem, or I've thought of something Imay be missing.Cheers,kjw

Chris Alfeld

3/29/2006 9:52:00 PM

0

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.


benjohn

3/29/2006 9:55:00 PM

0


On 29 Mar 2006, at 22:33, Jan_K 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?

:)

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.




Jeppe Jakobsen

3/29/2006 9:59:00 PM

0

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



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/Learn...
>
>
> 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"

Jeppe Jakobsen

3/29/2006 10:03:00 PM

0

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 :)
>
>
>
> 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/Learn...
> >
> >
> > 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"

paul.denlinger@gmail.com

3/30/2006 2:35:00 AM

0

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....
This is interactive and fun, and was written by someone who is
certifiably insane. (He wrote Poignant's Guide.)

Paul

Gregor

3/30/2006 3:19:00 AM

0

On Thu, 30 Mar 2006 07:03:17 +0900, "Jeppe Jakobsen"
<jeppe88@gmail.com> wrote:

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


I can't use "Until" because it wasn't covered in the book so far (even
though it seems pretty straightforward what it does). Ditto for "+-"

What was covered so far up until this chapter:

if, else, elsif, while

The first thing I tried was nesting IF's and WHILE's in different
combinations without using a variable to track the "BYE"s. I thought
that this would be the elegant solution, but really couldn't come up
with a way to do it.

So then I falled back to my first idea of "use variable to track
BYE's." But it looks like I'm not understanding the rules of nesting
and when/how a branch finishes and kicks you back to the previous
branch.

Here's what I had when I gave up:

------------------------------------------
input = gets.chomp
number = rand(21)

number2 = 0
keep_count = 0

if keep_count != 3
while
input != 'BYE'
puts 'HUH?! SPEAK UP, SONNY!'
keep_count = 0
input = gets.chomp
while
input == 'BYE'
puts 'HUH?! SPEAK UP, SONNY!'
number2 = (keep_count + 1)
keep_count = number2
input = gets.chomp
end
end
else
puts 'NO, NOT SINCE ' + (1930 + number).to_s + '!'
end
-----------------------------------------


And here's the regular Deaf Grandma solution that I used:

----------------------------------------
input = gets.chomp
number = rand(21)

if
while
input != 'BYE'
puts 'HUH?! SPEAK UP, SONNY!'
input = gets.chomp
end
else
puts 'NO, NOT SINCE ' + (1930 + number).to_s + '!'
end
-------------------------------------


Thanks.

--
Jan

Dave Burt

3/30/2006 10:18:00 AM

0

Hi Jan,

Have you been typing his examples into IRB and running them as you go? I
found that technique really useful learning Perl - try variations on the
example code until you understand it. Try reading Chapter 6 again with IRB,
including the mini exercise in the Looping section.

But let's unpack your code while it's here:

Jan K wrote:
> Here's what I had when I gave up:
>
> ------------------------------------------
> input = gets.chomp
> number = rand(21)
>
> number2 = 0
> keep_count = 0
>
> if keep_count != 3
> while
> input != 'BYE'
> puts 'HUH?! SPEAK UP, SONNY!'
> keep_count = 0
> input = gets.chomp
> while
> input == 'BYE'
> puts 'HUH?! SPEAK UP, SONNY!'
> number2 = (keep_count + 1)
> keep_count = number2
> input = gets.chomp
> end
> end
> else
> puts 'NO, NOT SINCE ' + (1930 + number).to_s + '!'
> end
> -----------------------------------------
>
>
> And here's the regular Deaf Grandma solution that I used:
>
> ----------------------------------------
> input = gets.chomp
> number = rand(21)
>
> if
> while
> input != 'BYE'
> puts 'HUH?! SPEAK UP, SONNY!'
> input = gets.chomp
> end
> else
> puts 'NO, NOT SINCE ' + (1930 + number).to_s + '!'
> end
> -------------------------------------

When you start dealing with flow control, you'll find that indenting makes a
big difference to how easy it is to read and understand your code. Let's
start by indenting your Grandma code:

if
while
input != 'BYE'
puts 'HUH?! SPEAK UP, SONNY!'
input = gets.chomp
end
else
puts 'NO, NOT SINCE ' + (1930 + number).to_s + '!'
end

Note in all Chris' examples, "if" and "while" are always followed by a
condition. If Ruby expects something, but doesn't get it, it'll wait until
the following line. That's what's happening with your if and while. It's the
same as this:

if (while input != 'BYE'
puts 'HUH?! SPEAK UP, SONNY!'
input = gets.chomp
end)
else
....

See, while's condition is "input != 'BYE'", and if's condition is the result
of the whole while loop. The result of a while loop is always nil, so the
condition is always false, so you get 'NO, NOT SINCE'...

That means this section of your program is the same as this:

while input != 'BYE'
puts 'HUH?! SPEAK UP, SONNY!'
input = gets.chomp
end
puts 'NO, NOT SINCE ' + (1930 + number).to_s + '!'

Indenting and putting the condition on the same line as the "if" or "while"
make the flow of your other program a little easier to see, too.

if keep_count != 3
while input != 'BYE'
puts 'HUH?! SPEAK UP, SONNY!'
keep_count = 0
input = gets.chomp
while input == 'BYE'
puts 'HUH?! SPEAK UP, SONNY!'
number2 = (keep_count + 1)
keep_count = number2
input = gets.chomp
end
end
else
puts 'NO, NOT SINCE ' + (1930 + number).to_s + '!'
end

Note you've got a loop inside a loop. To get out of that, the inside loop
has to exit, and then the outside loop, and if you look at their respective
conditions (input == 'BYE') and (input != 'BYE'), that will never happen.

Try going back to the example in the Looping section, and modifying that
little bits at a time. Indent lines inside control structures to help you
see at a glance where they begin and end.

Cheers,
Dave


Bil Kleb

3/30/2006 2:09:00 PM

0

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 http://c2.com/ppr/t... and the episode failure notes from
his Seattle XP talk: http://www.seapig.org/Ward...

Regards,
--
Bil
http://fun3d.lar...

Pistos Christou

3/30/2006 3:48:00 PM

0

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.

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

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

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