Forum: Ruby Chris Pine Program Challenges

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8e3f647b38a8d4cc3ddbddcf5eb40a47?d=identicon&s=25 unknown (Guest)
on 2007-06-17 01:26
(Received via mailing list)
puts 'Type in as many words as you want'

puts 'When you are finished, press \'ENTER\' on an empty line'

puts 'I\'ll give \'em all back to you in alphabetical order!'

stuff = []
user_input = gets.chomp

while user_input != ''

 stuff.push(user_input)

 user_input = gets.chomp

end

puts stuff.sort.join(', ')

Here is my code for the "array program"  from chapter 7 and the
challenge is to now adapt the program to have the same output without
using the "sort" method.

I have no idea where to even start. I think we are supposed to iterate
over the array somehow but have no clue. Anybody got a hint for me?

Secondly, I have a question about this code for my Deaf Grandmother
program:

gcount = 0

puts "Hello child! How are yeah?"

while gcount < 3

 r = gets.chomp

  if r == r.downcase or r == r.capitalize
   puts 'WHAT? YOU\'RE GONNA HAVE TO SPEAK UP!'
  end

  if (r == r.upcase and r != 'BYE')
   puts 'NOT SINCE 19' + (rand(21)+30).to_s + '!'
  end

  if r == 'BYE'
   puts 'NOT SINCE 19' + (rand(21)+30).to_s + '!'
   gcount = gcount + 1
  end

  if r != 'BYE'
   gcount = 0
  end

 end

The only problem with my program is when a user inputs something of
mixed case the program doesn't respond with "WHAT? YOU'RE GONNA HAVE
TO SPEAK UP!" like it should...

Any ideas?

Thanks a lot!
Bc8e3e2f7acb3a7d93c10e235dc0b4e1?d=identicon&s=25 yermej@gmail.com (Guest)
on 2007-06-17 01:56
(Received via mailing list)
On Jun 16, 6:24 pm, dani...@sleepingindian.org wrote:
[snip code]
> Here is my code for the "array program"  from chapter 7 and the
> challenge is to now adapt the program to have the same output without
> using the "sort" method.
>
> I have no idea where to even start. I think we are supposed to iterate
> over the array somehow but have no clue. Anybody got a hint for me?

Try inserting each input into a sorted array.

> Secondly, I have a question about this code for my Deaf Grandmother
> program:
[snip code]

> The only problem with my program is when a user inputs something of
> mixed case the program doesn't respond with "WHAT? YOU'RE GONNA HAVE
> TO SPEAK UP!" like it should...

Maybe use a regex to check for any lowercase letters. Instead of:
if r == r.downcase or r == r.capitalize
use:
if r =~ /[a-z]/

Jeremy
2f4d4f9c35ea851bffb9a9cc2e086365?d=identicon&s=25 Harry Kakueki (Guest)
on 2007-06-17 02:38
(Received via mailing list)
On 6/17/07, danielj@sleepingindian.org <danielj@sleepingindian.org>
wrote:
>
> Here is my code for the "array program"  from chapter 7 and the
> challenge is to now adapt the program to have the same output without
> using the "sort" method.
>
> I have no idea where to even start. I think we are supposed to iterate
> over the array somehow but have no clue. Anybody got a hint for me?
>

I haven't read that tutorial so I don't know what he is expecting you to
try.
But, here is a hint for something you could do.

arr = ["foo","bar","arrays are cool","rock"]
p arr.min

Harry

--

A Look into Japanese Ruby List in English
http://www.kakueki.com/
8e3f647b38a8d4cc3ddbddcf5eb40a47?d=identicon&s=25 danielj (Guest)
on 2007-06-17 02:41
(Received via mailing list)
> Try inserting each input into a sorted array.

Mmmm... I'm not quite following? Wouldn't I have to use the "sort"
method if I did that?


> Maybe use a regex to check for any lowercase letters. Instead of:
> if r == r.downcase or r == r.capitalize
> use:
> if r =~ /[a-z]/
>
> Jeremy

I would but, he has yet to talk about REGEX at that point in the
tutorial and I feel like I would be cheating :)
8e3f647b38a8d4cc3ddbddcf5eb40a47?d=identicon&s=25 danielj (Guest)
on 2007-06-17 02:46
(Received via mailing list)
> I haven't read that tutorial so I don't know what he is expecting you to try.
> But, here is a hint for something you could do.
>
> arr = ["foo","bar","arrays are cool","rock"]
> p arr.min
>
> Harry


Thanks for the hint!

However, he hasn't discussed that method at this point either...

I will play around with it that way but I can already see how easy it
would be to write that program out in my head...

Do you know of any websites with programming "challenges" for
beginners/intermediates?
Bc8e3e2f7acb3a7d93c10e235dc0b4e1?d=identicon&s=25 yermej@gmail.com (Guest)
on 2007-06-17 03:51
(Received via mailing list)
On Jun 16, 7:38 pm, danielj <dani...@sleepingindian.org> wrote:
> > Try inserting each input into a sorted array.
>
> Mmmm... I'm not quite following? Wouldn't I have to use the "sort"
> method if I did that?

Don't think about processing the array once you have all the inputs.
Think about processing as the inputs are gathered. You start with an
empty array; technically, that's already sorted. When you get the
first word, you insert that and you still have a sorted array. From
here on out, just insert each word so that you maintain a sorted
array.

>From reading the chapter, you'll be able to do this with what you've
learned, but it will take two arrays and a lot of moving things back
and forth.

> > Maybe use a regex to check for any lowercase letters. Instead of:
> > if r == r.downcase or r == r.capitalize
> > use:
> > if r =~ /[a-z]/
>
> I would but, he has yet to talk about REGEX at that point in the
> tutorial and I feel like I would be cheating :)

Fair enough. Taking a quick glance at the Chris Pine online book, I
think you'll be able to do what you need using swapcase and downcase.

For other Ruby problems to solve, you might want to check out
http://www.rubyquiz.com/ .
2f4d4f9c35ea851bffb9a9cc2e086365?d=identicon&s=25 Harry Kakueki (Guest)
on 2007-06-17 04:01
(Received via mailing list)
On 6/17/07, danielj <danielj@sleepingindian.org> wrote:
>
>
> Thanks for the hint!
>
> However, he hasn't discussed that method at this point either...
>
>
OK, you could select a value from your array.
Then start comparing it to all other values one at a time.
Each time you compare, you select the smallest of the two and use that
as the one to use for future comparisons.
After one iteration, you have the min.

Harry

--

A Look into Japanese Ruby List in English
http://www.kakueki.com/
8e3f647b38a8d4cc3ddbddcf5eb40a47?d=identicon&s=25 danielj (Guest)
on 2007-06-17 04:16
(Received via mailing list)
Ok guys, I'm pretty savvy and I think I have more than enough to go on
here...

One last question though...

I'm gonna run Linux and learn how to use it on my computer and I want
to use Colinux (free open source software) and was wondering if there
is a better alternative before I commit that ya'll know of...
3bb23e7770680ea44a2d79e6d10daaed?d=identicon&s=25 M. Edward (Ed) Borasky (Guest)
on 2007-06-17 08:48
(Received via mailing list)
danielj wrote:
>
IIRC Colinux is some gizmo that lets you run Linux on a Windows
computer. I don't think it's anywhere near as robust as the free as in
beer VMware Server, and it's certainly not as full-featured as VMware
Workstation 6. I'd stick with VMware for a beginner -- I don't think you
want to be futzing around with kernel-level stuff.
6c3285830f11acceeb15c85dd426831d?d=identicon&s=25 Matt (Guest)
on 2007-06-17 14:56
(Received via mailing list)
danielj@sleepingindian.org wrote:

 > Secondly, I have a question about this code for my Deaf Grandmother
 > program:
 >
 > gcount = 0
 >
 > puts "Hello child! How are yeah?"
 >
 > while gcount < 3
 >
 >  r = gets.chomp
 >
 >   if r == r.downcase or r == r.capitalize
 >    puts 'WHAT? YOU\'RE GONNA HAVE TO SPEAK UP!'
 >   end
 >
 >   if (r == r.upcase and r != 'BYE')
 >    puts 'NOT SINCE 19' + (rand(21)+30).to_s + '!'
 >   end
 >
 >   if r == 'BYE'
 >    puts 'NOT SINCE 19' + (rand(21)+30).to_s + '!'
 >    gcount = gcount + 1
 >   end
 >
 >   if r != 'BYE'
 >    gcount = 0
 >   end
 >
 >  end
 >
 > The only problem with my program is when a user inputs something of
 > mixed case the program doesn't respond with "WHAT? YOU'RE GONNA HAVE
 > TO SPEAK UP!" like it should...
 >
 > Any ideas?
 >
 > Thanks a lot!
 >

Hi Daniel,

I'm also working through this book and having a lot of fun with it.
This is what I ended up with on the Granma prog (I added to the middle
part so she'd say a final farewell message, although obviously that
could be reduced to just "if speech == 'BYE' bye_num = bye_num + 1
else... etc" to make the prog shorter):

bye_num = 0

while bye_num != 3
     puts 'What do you have to say to Granma?'
     speech = gets.chomp
     if speech == 'BYE'
         if bye_num == 2
             puts 'OKAY, LEAVE ME THEN.'
             bye_num = bye_num + 1
         else
             puts 'WHAT\'S THAT DEAR?'
             bye_num = bye_num + 1
         end
     else
         bye_num = 0
         if speech == speech.upcase
             puts 'NO, NOT SINCE 19' + (rand(21) + 30).to_s  + '!'
         else
             puts 'HUH?!  SPEAK UP, SONNY!'
         end
     end
end


Take care,
Matt
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (regularfry)
on 2007-06-18 02:36
(Received via mailing list)
M. Edward (Ed) Borasky wrote:
>>
>>
>
> IIRC Colinux is some gizmo that lets you run Linux on a Windows
> computer. I don't think it's anywhere near as robust as the free as in
> beer VMware Server, and it's certainly not as full-featured as VMware
> Workstation 6. I'd stick with VMware for a beginner -- I don't think you
> want to be futzing around with kernel-level stuff.
>
Seconded...  I've never had as clean an experience with Colinux (however
elegant an idea it is) as with virtual machines.
D3ca22a4771647f47533fae7d0d657db?d=identicon&s=25 Pablo Roboto (pabloroboto)
on 2010-10-08 19:59
unknown wrote:
> puts 'Type in as many words as you want'
>
> puts 'When you are finished, press \'ENTER\' on an empty line'
>
> puts 'I\'ll give \'em all back to you in alphabetical order!'
>
> stuff = []
> user_input = gets.chomp
>
> while user_input != ''
>
>  stuff.push(user_input)
>
>  user_input = gets.chomp
>
> end
>
> puts stuff.sort.join(', ')
>
> Here is my code for the "array program"  from chapter 7 and the
> challenge is to now adapt the program to have the same output without
> using the "sort" method.
>
> I have no idea where to even start. I think we are supposed to iterate
> over the array somehow but have no clue. Anybody got a hint for me?
>
> Secondly, I have a question about this code for my Deaf Grandmother
> program:
>
> gcount = 0
>
> puts "Hello child! How are yeah?"
>
> while gcount < 3
>
>  r = gets.chomp
>
>   if r == r.downcase or r == r.capitalize
>    puts 'WHAT? YOU\'RE GONNA HAVE TO SPEAK UP!'
>   end
>
>   if (r == r.upcase and r != 'BYE')
>    puts 'NOT SINCE 19' + (rand(21)+30).to_s + '!'
>   end
>
>   if r == 'BYE'
>    puts 'NOT SINCE 19' + (rand(21)+30).to_s + '!'
>    gcount = gcount + 1
>   end
>
>   if r != 'BYE'
>    gcount = 0
>   end
>
>  end
>
> The only problem with my program is when a user inputs something of
> mixed case the program doesn't respond with "WHAT? YOU'RE GONNA HAVE
> TO SPEAK UP!" like it should...
>
> Any ideas?
>
> Thanks a lot!

Just starting myself and enjoying the book (though I'm about to pull my
hair out with the sort exercise in Chapter 7 -- the one that challenges
you to perform the sort without using .sort)

Here's what I did ...

      # DEAF GRANDMA

        byecount = 0

        puts 'tell me sthign !'

        while byecount < 3

            sonnysays = gets.chomp

            if sonnysays == sonnysays.upcase
               if sonnysays == 'BYE'
                  puts 'do not go'
                  byecount = byecount +1
               else
                  puts 'more to say?'
               end
           else
               puts 'speak up; cannot hear.'
           end
        end

        puts 'i will miss you sonny'
B31e7abd14f1ceb4c4957da08933c630?d=identicon&s=25 Josh Cheek (Guest)
on 2010-10-08 23:31
(Received via mailing list)
On Fri, Oct 8, 2010 at 12:59 PM, Pablo Roboto
<sunjay.pandey@gmail.com>wrote:

> Just starting myself and enjoying the book (though I'm about to pull my
> hair out with the sort exercise in Chapter 7 -- the one that challenges
> you to perform the sort without using .sort)
>
>
If it helps, I made some videos showing what sorts "look like" (some
creative liberties taken) http://vimeo.com/channels/138529 In
particular,
look at insertion, selection, and bubble sorts. Those are usually
considered
pretty easy to understand, and are often the first sorts people are
taught.
Selection and Insertion are the kinds of sorts that humans do, for
example,
when sorting cards. If you have JRuby installed, you can generate the
images
yourself, and then flip through them forward and backward to see how
they
work. For some of them, that makes it more straightforward.
This topic is locked and can not be replied to.