0% found this document useful (0 votes)
52 views6 pages

Explorations in Computing An Introduction To Compu... - (Appendix A Answers To Selected Exercises)

Ethics computer

Uploaded by

cherrynessmoreno
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views6 pages

Explorations in Computing An Introduction To Compu... - (Appendix A Answers To Selected Exercises)

Ethics computer

Uploaded by

cherrynessmoreno
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Appendix A

Answers to Selected Exercises


Introduction
1.3: Which of these methods for finding a book in a library are algorithms?

• Ask the first person you see: Not an algorithm, since the person probably won’t know where the
book is, and may or may not want to help you look in the catalog.
• Find a librarian: Maybe an algorithm. If the library always has a librarian on duty, and the
librarian is always in a designated location (e.g., a reference desk) you know how to find, and
the librarian on duty is always willing to look in the catalog and give you directions, then
this method would be considered an algorithm. Theoretical computer scientists often analyze
properties of algorithms where key steps are carried out by “oracles” that always give the right
answer.
• Wait by the book return: Not an algorithm. Even if you are very patient, this method will never
terminate unless someone else checks out and then returns your book.
• Use an electronic catalog: Probably the most effective algorithm for this task.
Copyright © 2010. Taylor & Francis Group. All rights reserved.

• Work systematically, shelf by shelf: This is an algorithm, but not the best way to solve this
problem. Note that you would have to search every shelf to learn the book has been checked
out.
• Pick shelves at random: The key word here is “random.” The project in Chapter 9 explores
algorithms that generate sequences of numbers that appear to be random (they are called pseu-
dorandom number generators), and if you use one of these algorithms you can set it up so that it
eventually chooses every shelf. If you use some other method for selecting shelves (e.g., rolling
dice to get shelf numbers) then many people would argue this method is not an algorithm
because there is a chance it could go on forever.
• Ten friends each search a designated area: This is an example of a “parallel algorithm.” Parallel
algorithms are used on systems that have more than one processor, and are especially useful in
large scientific problems where a calculation is divided into smaller parts for each processor to
work on.

Conery, J. S. (2010). Explorations in computing : An introduction to computer science. Taylor & Francis Group.
Created from charismau-ebooks on 2024-11-14 [Link]. 341
342 Appendix A Answers to Selected Exercises

1.6: An algorithm for making a table of squares of numbers:


• Input: a number n. Result: a table that shows the values of i2 for every number i between 1
and n.
• Start by making a table with n rows and three columns.
• Write the numbers between 1 and n in the first column.
• Put a 1 in the second and third columns of the first row.
• Fill in the second column from top to bottom, setting each cell to a value 2 greater than the
value in the cell above.
• Fill in the third column from top to bottom, setting each cell to the sum of the number above
and the number to the left.

The Ruby Workbench


2.5: Ruby should handle this expression with no problem. The equation for converting from Fahren-
heit to Celsius works for negative temperatures as well as positive temperatures.

2.6: To find the freezing point of water call celsius(32). To find the boiling point call
celsius(212).

The Sieve of Eratosthenes


3.3: Ruby removes every string with an even number of letters.

3.4: 1000 = 31.62, so as soon as the first number in the worksheet is greater than 31 the algorithm
can stop.

3.7: To find the number of primes less than some number n call sieve(n-1) to make a list and
use the length method to find the number of items in the list. This can be done with a single Ruby
expression. For example, to count the number of primes less than 1000:
>> sieve(999).length
=> 168
Copyright © 2010. Taylor & Francis Group. All rights reserved.

A Journey of a Thousand Miles


4.6: (a) Ruby will print every string in the array named languages. (b) Ruby will print strings
shorter than 5 characters in length. (c) Ruby will print “found it” if the string "fortran" is in the
array; since it isn’t there, nothing is printed on the terminal.

4.9: The linear search methods stop after they find the first occurrence of the item they are looking
for. Since there is a 7 in the third location in this array the search stops after doing three comparisons.

4.16: Insertion sort will make the fewest comparisons when the list is already sorted. It only makes
one comparison on each iteration, and it does n − 1 iterations to sort an array with n items, so it would
make 19 comparisons for an array of 20 items and 49 comparisons for an array of 50 items.

4.17: Iteration sort makes the most comparisons if the array is sorted, but in the opposite order. The
algorithm needs to make a comparison for each dot shown in Figure 4.9. For an array with 20 items,
isort will make 20 × 19/2 = 190 comparisons, and for an array with 50 items, 50 × 49/2 = 1225
comparisons.

Conery, J. S. (2010). Explorations in computing : An introduction to computer science. Taylor & Francis Group.
Created from charismau-ebooks on 2024-11-14 [Link].
343

Divide and Conquer


5.6: If the item being sought is in the first location in the array, search will find it on the first
comparison, but bsearch will have to “zero in” on the item and do four comparisons.

5.12: If the array is already sorted, the merge operation needs to do half as many comparisons as
it would in the worst case. To see why, suppose it is merging two groups of size four. The algorithm
does four comparisons, each time moving something from the left group to the output. But then it
can copy all four items from the right group without doing any comparisons. In the worst case, there
would be seven comparisons to merge two groups of four items.

When Words Collide


6.6: A 3-digit decimal number has a value between 100 and 999, so an “average” three-digit number
would be around 500, or 103 /2. By analogy, a rough estimate for the numeric value of an n-digit
radix-26 string would be 26n /2.

6.8: A hash table with one row would put all the items in one long bucket in row 0. Since the bucket
would not be sorted, looking for an item in the table would be a linear search (plus the wasted cost of
computing the value of the hash function, which would be 0 for every string).

Bit by Bit
7.1: A 6-bit code has 26 = 64 different patterns, so there is room for 64 − 50 = 14 additional locations
before the code has to be extended to 7 bits.

7.2: Encoding 192 countries would require dlog2 192e = 8 bits.

7.3: If 9 bits are used to encode team names, 29 = 512 different names could be encoded.

7.7: Codes b, f, and g have an odd number of 1 bits.

7.14: If each letter has the (roughly) the same probability, there will be the same number of bits in
each code, and the Huffman code will not be any shorter than a regular code. To see why, consider
what happens when the first interior node is created: its value is the sum of the two letters, and when
it is added to the queue it will go at the end, after the last letter. The same will be true of every new
Copyright © 2010. Taylor & Francis Group. All rights reserved.

interior node, so the final tree will be “balanced.”

The War of the Words


8.1: To multiply 2 × 8, the algorithm makes 8 iterations.

8.3: Multiplying 0 × n means adding 0 n times. It doesn’t matter how many iterations are made, the
answer is always 0.

8.4: The way the Redcode program is written, the loop always does at least one addition before
testing to see if the counter has reached 0. That means x will be added to acc one time, and the
answer will be wrong.

8.5: Having x < 0 is no problem, but if y < 0 the program will give the wrong answer (it’s the same
bug that gives an error when y = 0, described above).

Conery, J. S. (2010). Explorations in computing : An introduction to computer science. Taylor & Francis Group.
Created from charismau-ebooks on 2024-11-14 [Link].
344 Appendix A Answers to Selected Exercises

8.6: If y = 0 the subtract instruction sets x to x − 0, i.e., it doesn’t modify x. That means the program
will be stuck in an infinite loop! Does this exercise give you any insight into why calculators give an
error message (or ∞ ) when you try to divide something by 0?

8.7: Since the Dwarf occupies only four words in memory, and the first bomb goes in the word
right after the end of the program, the bombs will not hit the program’s instructions when they wrap
around.

8.8: If bombs go in every other word, then yes, the Dwarf will bomb itself after the addresses wrap
around. Since there are 4096 memory locations in the RubyLabs version of MARS, the Dwarf can run
2048 iterations before it bombs itself.

Now for Something Completely Different

9.1: An interval of 5 hours leads to a schedule with all 12 times. To make a schedule that includes all
12 numbers, the interval and 12 must be relatively prime, i.e., there is no other number that divides
both evenly. For a 12-hour clock, the only numbers that fit this criterion are 5 and 7.

9.6: Any interval that is relatively prime with 24, so any interval that is a prime number will work,
e.g., 7, 11, 13, 17.

9.9: To see if an array a contains every number between 0 and m-1:


>> [Link] == Array(0..(m-1))

Ask Dr. Ruby

Here are some reasons Eliza would not be able to respond to the statements given in the
exercises for Section 10.7 (page 275).

H: I remember when I lived in the country and there were


cows and chickens everywhere.
C: Why do you mention cows just now?

This sentence is too complex. Eliza would probably apply a rule for the word “re-
Copyright © 2010. Taylor & Francis Group. All rights reserved.

member,” which has a high priority, and make a suspicious response like “Do you
often think of when you lived in the country and there were cows and chickens
everywhere?” instead of simply “Do you often think of cows?”

H: They were always looking at me.


C: The cows were looking at you?

This response depends on the previous sentence. The program would need some
sort of memory to record the fact that the first sentence was about cows, and
then be able to figure out the word “they” in the second sentence refers to the
“cows” in the previous sentence.

Conery, J. S. (2010). Explorations in computing : An introduction to computer science. Taylor & Francis Group.
Created from charismau-ebooks on 2024-11-14 [Link].
345

H: Our parents got mad when we tipped the cows.


C: Did you really push over a cow?

This response requires the program to have some real-world knowledge and to
be able to associate words with their meanings. It would need to know that,
in this context, “tipping” is the same as “pushing,” and that when something is
tipped far enough it will fall over. The context is important, too, since the correct
meaning is the one in “he tipped the cup” but not in “he tipped the waiter.”

H: The one with the big horns was the scariest.


C: Are you referring to a bull?

For a computer to give this response, it would have to remember that the con-
versation was about cows, so “horns” refers to the things on the bull’s head, not
musical instruments, and it would require some real-world knowledge that cows
(females) often don’t have horns.

H: Yeah, that guy was pretty aggressive.


C: Did the horns seem dangerous?

This response is based on context (a conversation about cows and horns) and on
the meaning of the word “horns” – that they are sharp, pointy, and, when on the
head of a live animal, potentially a threat.

The Music of the Spheres


11.2: Every body is influenced by the gravitational pull of the others, so yes, the Sun moves. Since
the Sun is the first body in the array, ask Ruby to print that object’s position:
>> b[0].position
=> (-7.3353e+07,-4.4832e+07,1.4706e+06)

11.3: Repeat the advance and turn commands four times, e.g.,
>> [Link] { [Link](10); [Link](90) }
Copyright © 2010. Taylor & Francis Group. All rights reserved.

=> 4

11.8: With 25 bodies, the simulation does (25 × 24)/2 = 300 force calculations on each time step.

11.10: If f3 starts out at a position halfway between f1 and f2, its track will initially stay between
the tracks for the other two bodies, but at some point f3 will veer away and head off on its own track.
One of the important attributes of chaotic systems is that small changes in the starting conditions may
lead to very big changes later on, so it is impossible to predict where the body will end up.

Conery, J. S. (2010). Explorations in computing : An introduction to computer science. Taylor & Francis Group.
Created from charismau-ebooks on 2024-11-14 [Link].
346 Appendix A Answers to Selected Exercises

The Traveling Salesman


12.2: The cost of the tour A–D–E is the cost of the path from A to D, plus the cost of the path from D
to E, plus the cost of the path from E back to A: 82.02 + 89.55 + 142.56 = 314.13.

12.4: The current path is


[:C, :F, :G, :A, :E, :D, :B]
The path that follows the dotted lines is
[:C, :F, :G, :A, :D, :E, :B]
The difference in these paths is the two cities in locations 4 and 5 of the array, so the call to mutate!
is
>> [Link]!(4)
=> #<TSPLab::Tour [:C, :F, :G, :A, :D, :E, :B] (1185.43)>

12.6: If the current path is


[:C, :F, :G, :A, :E, :D, :B]
a call to [Link]!(1) would produce this path:
[:C, :G, :F, :A, :E, :D, :B]
The new tour is

E
D G

F
B

C
Copyright © 2010. Taylor & Francis Group. All rights reserved.

12.7: A second mutation at the same location would simply change the path back to what it was
before the first mutation.

12.9: There are 15!/2 = 653, 837, 184, 000 possible tours of a map with 16 cities. This is approxi-
mately 6.5 × 1011 . Dividing by 106 means the random search would look at 1 out of every 650,000
tours. We can use IRB to get the exact percentage:
>> 1000000.0 / ntours(16)
=> 1.52943274636396e-06
If you convert this to a percentage, it’s .000153% of the total number of tours.

12.10: The evolutionary search will replace roughly half the tours on each generation, so just multiply
the population size by the number of generations and divide by 2.

Conery, J. S. (2010). Explorations in computing : An introduction to computer science. Taylor & Francis Group.
Created from charismau-ebooks on 2024-11-14 [Link].

You might also like