Learn to Program - Solutions?

P

pixelnate

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

Thanks,
~Nate
 
J

James Edward Gray II

Is there some place that I can solutions to the exercises in the
Learn To Program book by Chris Pine? 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 Gray II
 
N

Nate Turnage

On 7/27/06 said:
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
 
D

Dark Ambient

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
 
W

William James

Nate said:
"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

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
}
 
P

pixelnate

William said:
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
}
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
 
D

Dark Ambient

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

Stuart

William said:
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
}
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
 
J

James Edward Gray II

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

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 Gray II
 
P

Pablo Tejada

pixelnate said:
Is there some place that I can solutions to the exercises in the Learn
To Program book by Chris Pine? 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
 
N

Nate Turnage

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


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
 

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,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top