Chris Pine Program Challenges


#1

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!


#2

On Jun 16, 6:24 pm, removed_email_address@domain.invalid 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


#3

On 6/17/07, removed_email_address@domain.invalid removed_email_address@domain.invalid
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/


#4

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?


#5

On Jun 16, 7:38 pm, danielj removed_email_address@domain.invalid 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 :slight_smile:

Fair enough. Taking a quick glance at the Chris P. 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/ .


#6

On 6/17/07, danielj removed_email_address@domain.invalid 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/


#7

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…


#8

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


#9

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.


#10

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.


#11

removed_email_address@domain.invalid 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


#12

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'

#13

On Fri, Oct 8, 2010 at 12:59 PM, Pablo Roboto
removed_email_address@domain.invalidwrote:

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.