Deaf grandma

i picked up ruby 2 days ago & have been bustlin’ chris pine’s “learn to
program” tutorial, which goes over various aspects of ruby on a very
fundamental level. up until today, i’ve had relatively no problem
working out the exercises. but, as i expected, i’ve run into my first
significant obstacle. the exercise is as follows, “write a deaf grandma
program. whatever you say to grandma (whatever you type in), she should
respond with 'huh?! speak up, sonny!”, unless you shout it (type in all
capitals). if you shout, she can hear you (or at least she thinks so)
and yells back, 'no, not since 1938! to make your program really
believable, have grandma shout a different year each time; maybe any
year at random between 1930 and 1950."

now, i have a pretty good idea which methods i should be using to have
grandma respond with “huh!? speak up, sonny!” whenever something is said
that is not in all capitals & i’ve got a firm grasp on the fact that for
the “any year at random between 1930 & 1950” i’ll need to use the rand
method & define it as rand(21) + 1930. i’ve just been toying around with
the little i know & haven’t been able to get the result i want. i
promise you i’ve toyed around with branching, looping, etc., but am
still not able to get even remotely close to what the exercise asks of
me.

if anyone’s able to get me started in the right direction, i’d
appreciate it. there’s also an extension to the exercise & it states,
“what if grandma doesn’t want you to leave? when you shout ‘bye’, she
could pretend not to hear you. change your previous program so that you
have to shout ‘bye’ three times in a row. make sure to test your
program: if you shout ‘bye’ three times, but not in a row, you should
still be talking to grandma.” but, i’ll worry about that after i’ve got
the previous stuff squared away. thanks in advance.

On Sat, Jul 26, 2008 at 8:31 PM, Houston Barnett-gearhart
[email protected] wrote:

if anyone’s able to get me started in the right direction, i’d
appreciate it. there’s also an extension to the exercise & it states,
“what if grandma doesn’t want you to leave? when you shout ‘bye’, she
could pretend not to hear you. change your previous program so that you
have to shout ‘bye’ three times in a row. make sure to test your
program: if you shout ‘bye’ three times, but not in a row, you should
still be talking to grandma.” but, i’ll worry about that after i’ve got
the previous stuff squared away. thanks in advance.

Here’s an iterative approach to solving this:

  1. Write an echo program. Ask the user for some input, repeat it back
    and exit. E.g.

$ ruby grandma.rb
Please say something: Hello world
You said: Hello world

  1. Set up an echo loop: ask the user for some input, then say it back,
    then repeat those two steps endlessly

  2. Write an expression that returns ‘true’ if a string is in all caps
    and false otherwise

  3. Have your program only echo back if the input is in all caps

  4. Add the “huh? i can’t hear you!” if the input is not in all caps

Look up the section titled “Branching” in
http://pine.fm/LearnToProgram/?Chapter=06 if you have trouble with
steps 3 and 4

  1. Now go back to the code that says what to do if grandma can hear
    you, and add your random number code

It might be helpful to write your program out as an algorithm first
(i.e. forget about syntax, just write out the flow of logic. For
example, this is the Mrs Gabbard code from chapter 6 written out as an
algorithm:

Introduce yourself as Mrs Gabbard
Ask the student for his/her name
See if the name is properly capitalised
—If it is
------Ask the student to take a seat
—Otherwise
------Work out the properly capitalised form
------Ask if that is what the student meant
------If the answer is ‘yes’
---------Say “take a seat”
------If not
---------Say “get out!”

martin

thank you, martin, for your reply. i took your advice & wrote an
algorithm & it’s been helping me along quite well. but, i’ve a few
questions that arose while i was writing the program. before i get
started, here’s the program thus far: http://pastie.org/242167. i
realize that the code is a mess & i probably did much more than was
necessary, but like i said, i’m fairly new to ruby, let alone
programming. by all means, please suggest how i could clean it up a bit.
now, for my questions. foremost, after grandma’s spiel about it being so
long since she last saw you & how mother said you had a question for
her, if you ask the question & you capitalize any letter in your
question, the program ends. why is that? second, visually, when the
program is running, it’s not particularly fun to look at. i figured
centering the text would make a bit more pleasing to the eye, but not so
much. is there a string method that enables you to output whatever
grandma is saying as if she is typing it in real time? if so, how can i
implement that in my program?

that’s the gist of my questionnaire. it’s been frustrating, but as i
figure things out by myself or through the hints people give me, i get a
feeling of accomplishment. i understand that i’m just starting out & i
shouldn’t expect to have an elite knack to code in ruby, but i’ve been
at this program on & off for 2 days & i’m not really getting all the
results i’ve wanted. there’s still a lot i need to work in.
specifically, grandma’s response if you give her initial question an
answer that is not “yes,” “Yes,” “no,” or “No,” on top of the extension
to the program, which is a whole other monster for the time being.

again, any help is appreciated.

On Sun, Jul 27, 2008 at 2:32 PM, houston barnett-gearhart
[email protected] wrote:

thank you, martin, for your reply. i took your advice & wrote an
algorithm & it’s been helping me along quite well. but, i’ve a few
questions that arose while i was writing the program. before i get
started, here’s the program thus far: http://pastie.org/242167. i
realize that the code is a mess & i probably did much more than was
necessary, but like i said, i’m fairly new to ruby, let alone
programming. by all means, please suggest how i could clean it up a bit.

I looked at your code, and it looks like you’re taking an inside-out
approach to the problem; that is, you’re concentrating on the little
bits and then trying to fit them into some sort of overall structure.
Particularly since you’re learning to code, I’d strongly recommend
leaving that code alone, and starting from scratch using a more
top-down approach. Specifically, get the structure of your program
correct first, then slowly fill in that structure to have all the
features you need. Go back and check out the iterative approach I
suggested in my last post, where you build up the code feature by
feature starting with the basic question-answer-question-answer…
loop.

After you’ve been at it a while, this will become second nature to
you, and you’ll be able to keep the structure of the program in your
head and fill in details even before you have the whole skeleton
mapped out, but that takes experience, and unnecessarily complicates
matters at this stage. Also, when you come to chapter 8 in the
tutorial you’ll learn about methods, which are a powerful way to keep
your program well-structured throughout its development.

martin

houston barnett-gearhart wrote:

is there a string method that enables you to output whatever
grandma is saying as if she is typing it in real time? if so, how can i
implement that in my program?

def type(string, speed=5)
old_sync = $stdout.sync
$stdout.sync = true
string.each_char do |c|
print c
sleep 1.0/speed
end
$stdout.sync = old_sync
end

type “foo\nbar\bz”

This will type 5 letters per second (the default value for speed), first
typing foo, then a newline, then bar and then deleting the r in bar and
replacing it with a z.
So put this method definition at the beginning of your script and then
use
type instead of puts. If you’re using ruby 1.8.6, you’ll also have to
require ‘jcode’ at the top of the file.

HTH,
Sebastian

houston barnett-gearhart wrote:

thank you, martin, for your reply. i took your advice & wrote an
algorithm & it’s been helping me along quite well. but, i’ve a few
questions that arose while i was writing the program. before i get
started, here’s the program thus far: http://pastie.org/242167. i
realize that the code is a mess & i probably did much more than was
necessary, but like i said, i’m fairly new to ruby, let alone
programming. by all means, please suggest how i could clean it up a bit.
now, for my questions. foremost, after grandma’s spiel about it being so
long since she last saw you & how mother said you had a question for
her, if you ask the question & you capitalize any letter in your
question, the program ends. why is that? second, visually, when the
program is running, it’s not particularly fun to look at. i figured
centering the text would make a bit more pleasing to the eye, but not so
much. is there a string method that enables you to output whatever
grandma is saying as if she is typing it in real time? if so, how can i
implement that in my program?

that’s the gist of my questionnaire. it’s been frustrating, but as i
figure things out by myself or through the hints people give me, i get a
feeling of accomplishment. i understand that i’m just starting out & i
shouldn’t expect to have an elite knack to code in ruby, but i’ve been
at this program on & off for 2 days & i’m not really getting all the
results i’ve wanted. there’s still a lot i need to work in.
specifically, grandma’s response if you give her initial question an
answer that is not “yes,” “Yes,” “no,” or “No,” on top of the extension
to the program, which is a whole other monster for the time being.

again, any help is appreciated.

Well. you have a whole lot of “if” statements. An if statement has a
something which is true or false; in your case

reply1 == “yes”

If it’s true, the code behind it will run. If it’s false, the code after
an else-statement will run. If there is no “else” , the if statement
will ignore all code until it finds it’s “end” statement.

In your case that means all following code is skipped. The "end"s are
misplaced.

You might want to look into “while” or “until” to tackle this.

childs_reply = “”
until childs_reply == “BYE”
childs_reply = gets.chomp
##do smart stuff
#if
#else
#end
end

Regarding grandma’s typing. She’s going to do this all the time, so
let’s make a method. Basically we want to recieve a string, take each
letter, print it and then pause a little.

def grandma_says(str) #str is the stuff we’re going to show.
str.each_byte do |byte| #Whoa. each_byte? Never mind, it’s there to
print byte.chr #handle non-western character sets.
sleep 0.1
STDOUT.flush #major pain in the ass, this (on windows).
end
puts #forces a line ending when grandma is
finished.
STDOUT.flush
end

grandma_says(“I may be old but I’m not slow!”)

Hth,

Siep

I don’t want to give away TOO much, but here is the shell of what you
should
have. The last part of the problem (having to shout BYE three times) is
left as
an exercise to the reader.

Initialize some variables.

text = “”
leave_message = “BYE”

Welcome message.

puts “Well, hello, sonny!”

Start loop.

while text != leave_message
text = gets.chomp
if text == text.upcase
random = rand(21) + 1930
puts “No, not since #{random}!”
else
puts “Huh?! Speak up, sonny!”
end
end

HTH,
Dana

On Sun, Jul 27, 2008 at 11:51 PM, houston barnett-gearhart
[email protected] wrote:

Martin, is there any way I can get in touch with you outside of the
forum? Preferably through a medium that is as close to real-time as
possible, i.e., Skype, Gmail’s chat function, etc. I appreciate the help
you’ve given me thus far & would like to be able to bounce my progress
off you as I work on the exercise, so as to be able to ascertain whether
or not I’m on the right track. This will undoubtedly help me
significantly, alongside being a bit more time efficient, considering I
spent a considerable amount of time on the code I posted & I was on the
wrong track from the get-go. I think it’d be easier this way, not to
mention space & time efficient.

I’m a bit too busy at work for realtime stuff these days - but have
you discovered the ruby IRC channel? It’s #ruby-lang on
irc.freeenode.net, and there are lots of helpful, friendly people on
there.

martin

I don’t know why this is so hard for me to get my head around, but
thing’s just aren’t working out as I’d planned. Martin, I’ve followed
your iterative approach, but I can’t seem to implement each specificity
into a conglomerate program. I have discovered #ruby-lang & I use it
quite frequently, but it’s much more difficult to explain the problems
I’m having when I need to explain, in detail, what it is I’m doing, what
I’ve tried, etc. In large part, that difficulty pushed me to posting
here. Now, because you’ve said you’re busy with work & unable to fiddle
with any real-time stuff, I’ll continue to post my questions here. As
for the iterative approach & what I’ve written for each:

puts
puts “Please say something.”
puts
reply = gets.chomp
puts
puts “You said #{reply}.”

loop {puts “Please say something.”
puts
reply = gets.chomp
puts}

puts
puts “The world is flat. True or false?”
puts
reply = gets.chomp.downcase
if reply == “true”
puts
puts “You must’ve read Thomas L. Friedman’s book!”
elsif reply == “false”
puts
puts “Ah, submitting to Pythagoras’ proposal that”
puts “the world is round, I see. That’s so 2,508”
puts “years ago!”
end

  1. (I cannot take ownership for writing this. sepp2k from #ruby-lang
    helped me considerably. He alerted me to the fact I shouldn’t be using
    go-to style flow control & told me looping was the better bet.)

reply = “abc”
while reply != reply.upcase
puts
puts “Simon says, 'Input your text in all”
puts “caps! Otherwise, I won’t echo back”
puts “anything you say! Instead, I will”
puts “spit out my ‘Simon says’ spiel once”
puts “more. & you wouldn’t want that!”
puts
reply = gets.chomp
end

puts reply

… for now, that’s all I’ve got. I’ve been working on each individual
step of the iterative approach & trying to pick up what I can through my
questions on #ruby-lang. I’ll post the code for 4 & 5 later on in the
day. Please take a look over what I’ve written (with the help of an
array of people) & let me know if there’s anywhere I can clean it up.
Thanks!

Martin, is there any way I can get in touch with you outside of the
forum? Preferably through a medium that is as close to real-time as
possible, i.e., Skype, Gmail’s chat function, etc. I appreciate the help
you’ve given me thus far & would like to be able to bounce my progress
off you as I work on the exercise, so as to be able to ascertain whether
or not I’m on the right track. This will undoubtedly help me
significantly, alongside being a bit more time efficient, considering I
spent a considerable amount of time on the code I posted & I was on the
wrong track from the get-go. I think it’d be easier this way, not to
mention space & time efficient.

On Mon, Jul 28, 2008 at 1:32 PM, houston barnett-gearhart
[email protected] wrote:

  1. (I cannot take ownership for writing this. sepp2k from #ruby-lang
    helped me considerably. He alerted me to the fact I shouldn’t be using
    go-to style flow control & told me looping was the better bet.)

This is actually shaping up very well. Here’s a comment:

end

puts reply

The algorithm for this is:

loop
---- get a reply
---- if the reply is not in all caps
------ repeat loop
---- otherwise
------ exit loop

You’re not doing anything with the reply yet. The following skeleton
might be easier to work with

0 loop
1 ---- get a reply
2 ---- if the reply is in all caps
3 -------- do something
4 ---- otherwise
5 -------- do something else
6 ---- repeat loop

Note that steps 1-5 simply get a reply and deal with responding to it,
and that they are contained cleanly inside the loop body. Of course,
you do want to exit the loop at some point, but I’d suggest doing it
this way:

0 loop
1 ---- get a reply
2 ---- if the reply is in all caps
3 -------- do something
4 ---- otherwise
5 -------- do something else
6 ---- break out of the loop if we need to
7 ---- repeat loop (unless we’ve broken out)

Ruby does support the ‘break’ call to exit a loop, but another, and
often helpful way to do things is to have a variable act as a flag,
and check the flag each time to see whether we loop again. Then,
within the body of the loop, you signal “ready to exit” by setting the
flag. Here’s some code that demonstrates both approaches:

one way: use a flag to end the loop

done_talking = false
while (not done_talking) do
puts “Loop 1> Say something (or ‘quit’ to exit)”
reply = gets.chomp
if (reply.downcase == “quit”)
done_talking = true
else
if (reply == reply.upcase)
puts “that was in all caps”
else
puts “that was not in all caps”
end
end
end

puts “exited loop 1”

and the other way: loop infinitely, but break out if needed

while true do
puts “Loop 2> Say something (or ‘quit’ to exit)”
reply = gets.chomp
if (reply.downcase == “quit”)
break
else
if (reply == reply.upcase)
puts “that was in all caps”
else
puts “that was not in all caps”
end
end
end

puts “exited loop 2”

an advantage of the ‘break’ method is that it saves one level of

if/else
while true do
puts “Loop 3> Say something (or ‘quit’ to exit)”
reply = gets.chomp
if (reply.downcase == “quit”)
break
end

if (reply == reply.upcase)
puts “that was in all caps”
else
puts “that was not in all caps”
end
end

puts “exited loop 3”

… for now, that’s all I’ve got. I’ve been working on each individual
step of the iterative approach & trying to pick up what I can through my
questions on #ruby-lang. I’ll post the code for 4 & 5 later on in the
day. Please take a look over what I’ve written (with the help of an
array of people) & let me know if there’s anywhere I can clean it up.
Thanks!

No problem :slight_smile: Looking forward to seeing the next two parts.

martin

On Mon, Jul 28, 2008 at 8:24 PM, Todd B. [email protected]
wrote:

I haven’t looked at Pine’s specific question, but I’d be a little
surprised if it’s this simple…

s = some_string; s.upcase == s

BTW, that doesn’t give you a direct answer to your whole question, but
may be what Pine was looking for the shout part.

Todd

On Sat, Jul 26, 2008 at 10:31 PM, Houston Barnett-gearhart
[email protected] wrote:

year at random between 1930 and 1950."

if anyone’s able to get me started in the right direction, i’d
appreciate it. there’s also an extension to the exercise & it states,
“what if grandma doesn’t want you to leave? when you shout ‘bye’, she
could pretend not to hear you. change your previous program so that you
have to shout ‘bye’ three times in a row. make sure to test your
program: if you shout ‘bye’ three times, but not in a row, you should
still be talking to grandma.” but, i’ll worry about that after i’ve got
the previous stuff squared away. thanks in advance.

I haven’t looked at Pine’s specific question, but I’d be a little
surprised if it’s this simple…

s = some_string; s.upcase == s

Todd

Oh, guess I should have checked the forum before I said I wasn’t sure
how to go about writing the extension. It looks like Phee got further
than I did without much help (good for him, really), but got tangled up
on what I would end up inevitably getting tangled up in myself. With the
replies from his topic, I’m bound to be able to write the extension &
have the total package done by tomorrow morning or afternoon.

Martin, I believe I’ve successfully written the first portion of Chris
Pine’s “Deaf Grandma” exercise. With the help of the stuff you posted
about the various approaches to take to break a loop & various people on
#ruby-lang (particularly Giraph), I’ve fulfilled the majority of my
goal, which turned out to be quite a frustrating task. Although the text
being outputted suggests that you are speaking to Grandma through an
intermediary (at first), the main idea is still heavily rooted in what
is said & how user input is replied to. This is the finished program, &
as you can tell, I’ve implemented various things from the suggestions
given in posts on here & in #ruby-lang. I’m most proud of the fact that
it’s as if Grandma & her intermediary are talking to you in real-time…
the effect is nice. So, here’s the code for the finished program (void
of the extension, which I will work on tomorrow):

==========

def granny_says(str)
str.each_byte do |byte|
print byte.chr
sleep 0.05
STDOUT.flush
end
puts
STDOUT.flush
end

while true do
granny_says(“Got a question for granny? Make sure to”)
granny_says(“input yo’ question in all caps, or else”)
granny_says(“granny can’t hear ya’!\n\n”)
reply = gets.chomp
if (reply == reply.upcase)
random = rand(21) + 1930
granny_says("\nNo, not since #{random}!")
granny_says(“That’s all the time I’ve got, child!”)
granny_says(“Hasta la-bye-bye!”)
break
else
granny_says("\nGranny can’t hear you! Wanna know why?")
granny_says("‘Cause that wasn’t in all caps, yo’!")
granny_says(“So, I’ll repeat what I said before …\n\n”)
end
end

==========

Now that I’ve got that squared away, I’ll look into adding the extension
to the exercise. & to tell you the truth, I’ve no idea how to do that.
I’m thinkin’ I need to create another loop, & define (some how, I’m
still not sure) that Grandma will only say goodbye after the user has
inputted “bye” three times in a row. I guess I have an inkling of an
idea of how to go about it, just not sure it’ll work. If all goes well,
I’ll be posting later tomorrow with the total exercise, extension
included, complete. Wish me luck!

On Mon, Jul 28, 2008 at 11:30 PM, houston barnett-gearhart
[email protected] wrote:

the effect is nice. So, here’s the code for the finished program (void
of the extension, which I will work on tomorrow):

It looks good :slight_smile: And as you note in your next post, the extension
doesn’t really need another loop, just something within the same loop
that adds an additional check for whether to exit or not.

martin

Martin, regardless of having been away I don’t need another loop, I seem
to have added one anyway. Now, I’ve satisfied every requirement the
exercise ascertained. But, I’ve been told that the indentation is funky
& that the print statements could be shortened. Most significant is the
fact that I seem to have “one loop too many,” but I’m unsure of where it
is & how to fix it. Here’s the URL to the code on Pastie
(http://pastie.org/243587).

I’d say it’s about 99% complete. Although I do need the following, as
mentioned above:

• uniform indentation;
• shortened print statements;
• one less loop

If you could suggest how I go about doing any of the above, or throw
some URLs my way that speak on the topics of indentation & the like, I’d
appreciate it. Once I get your suggestions (or toy around more with the
code) & I’ve re-worked everything, I’d say we’re golden.

You will need

class Grandma

end

:slight_smile:

On Tue, Jul 29, 2008 at 2:06 PM, houston barnett-gearhart
[email protected] wrote:

(http://pastie.org/243587).

I’d say it’s about 99% complete. Although I do need the following, as
mentioned above:

• uniform indentation;

This is something a good editor should do for you. If you let us know
what platform you are on, people can recommend their favourite editors
and IDEs :slight_smile: I use gvim myself; below is an example of how it
autoindents your code:

http://pastie.org/243603

A heavyweight but very nicely polished option is netbeans with ruby
support: http://wiki.netbeans.org/Ruby

• shortened print statements;

Not sure what you mean by that. If you have a lot of text to print, it
needs to be included, after all :slight_smile: There are things you could do to
compact the code but they add little value at this stage - I’d say go
ahead and do it the straightforward way as you have here.

• one less loop

There’s a problem with your code as written - note that once you’ve
said “bye” you can never say anything else - it sticks in the loop
till it counts three ‘byes’ in a row. Whereas what you want is to exit
the whole loop if you see three byes in a row, otherwise just carry on
as normal. What you should do is this -

if the user has yelled, so that granny hears what he says, do an
additional check for his having said “bye”.
---- If that is true, then increase the bye count.
-------- If the bye count is 3, exit the loop
---- If that is false, reset the bye count to zero and carry on with
the rest of the code

Hope that helps!

martin

On Tue, Jul 29, 2008 at 2:32 PM, Marc H. [email protected]
wrote:

You will need

class Grandma

end

Actually, if you listen to the problem statement, he wants

def grandma

end

:slight_smile:

martin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs