CPS 149, Problems for Week 4

  1. Question 1 The Wormhole Regatta
  2. Question 2 Numerical Differentiation
  3. Question 4 Exobiological Entanglements

Question 1 The Wormhole Regatta

In the 22nd century spaceships have become common. Racing on a sea of stars is a very romantic and exciting pasttime. Unlike sailing on an Earth ocean, racing in space is done from sector to sector through a network of wormholes which riddles local space. Each sector of space, which is one part of a quad (a 4x4x4 cube of sectors), may have the entrance of one hole and the exit of another hole. By exhaustive analysis, it has been found that the wormholes connect in such a way that they eventually lead back to where you started (i.e. A-> B-> C-> A).

Suzy Nova, a student in the 22nd century, has been given the following task:

Ships are at the entrance of each worm hole in a quad and in concert pass through to the next sector via the wormhole. Continuing this until they all arrive at their original sectors on the same jump, how many jumps will they have to make?

Input will consist of a line containing a single number, that being the number of sectors containing wormholes. The rest of the input is a series of lines containing a pair of x,y,z coordinates of the current and destination sectors, separated by a single colon `:'. The output will be a single line containing the number of jumps followed by a single space and the word `jumps'.

Input:              Output:
13                  60 jumps
3,2,2:1,0,3
2,0,0:2,0,0
1,0,3:2,1,3
0,1,2:2,1,0
0,1,0:0,2,2
2.1,3:0.1.0
0,2,0:3,2,3
2,1,0:0,2,0
0,2,2:3,2,2
3,2,3:0,1,2
0,1,1:0,3,3
0,3,3:3,1,0
3,1,0:0,1,1

Question 2 Numerical Differentiation

For this problem, you will be asked, given a function of x, and a numerical value a, to output the value of the derivative of the given function (with respect to x), when x=a.

The given function will only involve the single variable x (though it may appear more than once in the function), floating-point constants, and the four operators +, -, *,/, representing addition, subtraction, multiplication, and division, respectively.

Remember: if f and g are functions of x, then the following are true (where f' denotes the derivative of f):

(f + g)' = f' + g'
(f - g)' = f' - g'
(f * g)' = f' * g + f * g'
(f / g)' = (f' * g - f * g') / (g ^ 2)

Input:

The input will consist of a number of problems, one per line. Each input line will consist of the floating-point value a, a space, and a function f. The function f will be written in postfix (Reverse Polish) notation: f will be represented as a list of words, separated by whitespace. Each word will be either a floating-point constant, the symbol x, or one of the four arithmetic operators above.

For example, the input line

1.72 x 3.23 - 2 x * 7 + /

would be asking you to compute the derivative of the function f=(x-3.23)/(2x+7) (which happens to be f'=13.46/((2x+7)^2)), and to substitute the value 1.72 for x, resulting in the value 0.123493 (to six decimal places).

You may assume that the input is well-formed.

Output:

For each line of input consisting of the floating-point value a and a function of f of x, output the value of f'(a), rounded to six decimal places (with a 0 before the decimal point if the magnitude of the result is less than 1).

Sample input:

1.72 x 3.23 - 2 x * 7 + /
-17.91326491 x x * 2 /
12 x
15 7.4

Sample output:

0.123493
-17.913265
1.000000
0.000000

Question 4 Exobiological Entanglements

Finding a Mole in a Haystack

Suzy Nova is having some problems with her exobiology teacher's assignment. She has been asked to take a picture and count the animals she finds in the picture. Based on her counts, she has been asked to give certain totals.

The input is a series of lines, separated into three distinct blocks: the section defining the size of the picture, the picture, and the queries to be answered. The output will consist of the responses to the queries.

The size of the picture is defined on a single line containing two integers separated by a space. The numbers represent the respective width and length of the picture. Directly after the definition of the size of the picture will be the picture. The picture is composed of a rectangular block of various characters, including

"0123456789-+ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

each of which represent different species. If two or more identical characters make contact (i.e. if one is above, to the left, to the right, or below another), they are considered connected and represent the same animal. A `connected' groups character count is considered the `size' of the animal. All other characters in the picture are considered noise and should be ignored.

The picture is separated from the queries by a single line with a `#' on it. The queries come in two types, animal `A' queries, and size `S' queries. Both queries appear singly on a line, beginning with an `A' or `S' followed by a `:' then followed by the selection. Selections for an 'A' query will consist of a string of `species' characters. Selections for an 'S' query will consist of a series of integers separated by single spaces.

The output for an `A' query will be the sum of all the animals matching the species in the selection. The output for an `S' query will be the sum of all the animals of the given sizes.

Input:                         Output:
20 6                           5
12345678901234567890           1
aaa  bbbbb bb      =           3
aaaa     b   bb    =           2
aaaa bbb b ()* bb  =           20
 aaa b   b    c    =           21
aaa  bbbbb ccc     =           4
#
A:ab
A:aa
A:ac
A:c
A:0123456789
S:1
S:2 3

Notes: picture lines will be no longer than 80 characters. There will be no more than 160 lines in a picture. Picture lines may and will likely contain trailing spaces, which should have no effect on the totals. Selections may contain repeated characters or numbers. The counts should only count the first and not the rest of the repetitions (see example query `A:aa'). An animal size will never exceed 100 characters. There is no limit to the number of queries.