[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Chris Pine Program Challenges

danielj

6/16/2007 11:24:00 PM

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!

10 Answers

yermej

6/16/2007 11:54:00 PM

0

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

Harry Kakueki

6/17/2007 12:37:00 AM

0

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.ka...

danielj

6/17/2007 12:39:00 AM

0


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




danielj

6/17/2007 12:43:00 AM

0


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

yermej

6/17/2007 1:49:00 AM

0

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.rub... .

Harry Kakueki

6/17/2007 2:01:00 AM

0

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.ka...

danielj

6/17/2007 2:10:00 AM

0

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

M. Edward (Ed) Borasky

6/17/2007 6:48:00 AM

0

danielj wrote:
> 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...
>
>
>

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.

Matt

6/17/2007 12:54:00 PM

0

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

Alex Young

6/18/2007 12:35:00 AM

0

M. Edward (Ed) Borasky wrote:
> danielj wrote:
>> 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...
>>
>>
>>
>
> 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.

--
Alex