Learn to Program - Solutions?

Is there some place that I can solutions to the exercises in the Learn
To Program book by Chris P.? I am stuck on the second exercise in
Chapter 8.

Thanks,
~Nate

On Jul 27, 2006, at 11:22 AM, pixelnate wrote:

Is there some place that I can solutions to the exercises in the
Learn To Program book by Chris P.? I am stuck on the second
exercise in Chapter 8.

Refresh my memory on what that exercise is and I’m happy to give a
hint or two.

James Edward G. II

On 7/27/06, James Edward G. II [email protected] wrote:
Refresh my memory on what that exercise is and I’m happy to give a

hint or two.

“Rewrite your table of contents program on page 35. Start the
program with an array holding all of the information for your table
of contents (chapter names, page numbers, etc.). Then print out
the information from the array in a beautifully formatted table of
contents.”

The chapter is about array methods. For each chapter there is a
chapter number, a chapter title, and a page number. I understand how
the info for each chapter
can live in an array, but I don’t understand how all of the data for
all of the chapters lives in a single array that holds all that data.
Thanks in advance.

pixelNate

Nate,
Is this what your referring too ?

toc = [‘Table of Contents’, ‘Chapter 1’, ‘Getting Started’,
‘page 1’, ‘Chapter 2’, ‘Numbers’, ‘page 9’,
‘Chapter 3’, ‘Letter’, ‘page 13’]

    line_width = 30

    puts(              toc[0].center(line_width*3))
    puts
    puts(              toc[1].ljust(line_width) +

toc[2].ljust(line_width) + toc[3].ljust(line_width))
puts( toc[4].ljust(line_width) +
toc[5].ljust(line_width) + toc[6].ljust(line_width))
puts( toc[7].ljust(line_width) +
toc[8].ljust(line_width) + toc[9].ljust(line_width))

Hope that helps
Stuart

Nate T. wrote:

The chapter is about array methods. For each chapter there is a
chapter number, a chapter title, and a page number. I understand how
the info for each chapter
can live in an array, but I don’t understand how all of the data for
all of the chapters lives in a single array that holds all that data.
Thanks in advance.

pixelNate

toc = [‘Table of Contents’, ‘Chapter 1’, ‘Getting Started’,
‘page 1’, ‘Chapter 2’, ‘Numbers’, ‘page 9’,
‘Chapter 3’, ‘Letter’, ‘page 13’]

wd = 60

puts toc.shift.center(wd)

toc.inject([]){ |a,e|
a << e
if 3 == a.size
a[0,2] = a[0,2].join( ": " ) + " "
a.push " " + a.pop[ /\d+/ ]
print a.first, “.” * (wd - a.first.size - a.last.size),
a.last, “\n”
a.clear
end
a
}

Nate, my version does not. I followed the book along.

Stuart

William J. wrote:

a << e

Thank you. I think you have some things in there that hadn’t yet been
mentioned in the book, but it gives me something to study.

pixelNate

Dark A. wrote:

Nate, my version does not. I followed the book along.

Stuart
I am afraid I don’t follow you.

Nate

On Jul 27, 2006, at 11:44 AM, Nate T. wrote:

The chapter is about array methods. For each chapter there is a
chapter number, a chapter title, and a page number. I understand how
the info for each chapter
can live in an array, but I don’t understand how all of the data for
all of the chapters lives in a single array that holds all that data.
Thanks in advance.

I see you are already getting some hints, but here’s another. You
can nest Arrays in Ruby. Your book did show this, but only on one
line and it wasn’t explained much. Have a look though:

[ [“I”, “First Chapter”, “Page 20”],
?> [“II”, “Second Chapter”, “Page 43”],
?> [“III”, “…”, “Page 102”] ].each do |chapter|
?> puts chapter[0]

puts chapter[1]
puts chapter[2]
end
I
First Chapter
Page 20
II
Second Chapter
Page 43
III

Page 102
=> [[“I”, “First Chapter”, “Page 20”], [“II”, “Second Chapter”, “Page
43”], [“III”, “…”, “Page 102”]]

See if that gives you some new ideas.

James Edward G. II

James Edward G. II wrote:

See if that gives you some new ideas.

Yes, it does. I could have sworn I had seen that, but I couldn’t find
it. Thanks.

~Nate

pixelnate wrote:

Is there some place that I can solutions to the exercises in the Learn
To Program book by Chris P.? I am stuck on the second exercise in
Chapter 8.

Thanks,
~Nate

I just finished chapter 9 and here is my version of the program.

lineWidth = 50
toc=[‘Table of Contents’,‘Chapter 1: Numbers’,‘page 1’,
‘Chapter 2: Letters’,‘page 72’,‘Chapter 3: Variables’,
‘page 128’]
puts ’ ’
al=1
puts toc[0].center(lineWidth)
puts ’ ’
puts ’ ’
while al <toc.length
puts toc[al].ljust(lineWidth/2)+toc[al+1].rjust(lineWidth/2)
al=al+2
end

Is it possible to use subarrays and still make the output look like a
real table of contents? How would you iterate through the data if each
chapter had its own subarray with three indices (chapter number,
subject, page number.)

toc = [[‘Chapter 1’, ‘Getting Started’, ‘page 3’], … ]]

__
Mikeatgl

On 8/2/06, Pablo T. [email protected] wrote:

puts ’ ’
while al <toc.length
puts toc[al].ljust(lineWidth/2)+toc[al+1].rjust(lineWidth/2)
al=al+2
end

Thanks, Pablo. I think what was throwing me off is that all of the
information went into the same array. I was under the impression that
each chapter had it’s own array nested in a larger array (I am new to
this). Thanks, again to everyone that helped me.

~Nate

I ended up solving it like this.

width = 50

puts (‘Table of Contents’.center(width*1.5))

puts ‘’

toc = [[‘Chapter 1:’, ‘Getting Started’, ‘Page 3’], [‘Chapter 2:’,
‘Numbers’, ‘page 9’], [‘Chapter 3:’, ‘Letters’, ‘page 13’]]

a = 0
b = 0
c = 0

while c < toc.length

puts toc[a][b].ljust(width/2) + toc[a][b +=1].ljust(width/2) +
toc[a][b+= 1].rjust(width/2)
puts ‘’
b=0
a += 1
c += 1
end