Chris Pine Program Challenges

D

danielj

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!
 
Y

yermej

On Jun 16, 6:24 pm, (e-mail address removed) 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
 
H

Harry Kakueki

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
 
D

danielj

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

danielj

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

yermej

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

Harry Kakueki

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
 
D

danielj

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

M. Edward (Ed) Borasky

danielj said:
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.
 
M

Matt

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
 
A

Alex Young

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top