Fall 2001

CPSC 215

MIDTERM EXAM

Name: _________________________________

Note: All questions and programs are about the Mathematica programming language.
Assume that the Mathematica kernel is restarted for each question.

Good Luck!

Department of Computer Science
The University of Calgary

CPCS 215 — Fall Session 2001

(Dr. Christian Jacob)
L01

MIDTERM EXAMINATION

October 25, 2001

Time: 60 Minutes

First Name, Last Name: ___________________________

Student ID:                   ______________

Instructions

({{({{\(1. \ Please\ write\ your\ name\ on\ the\ cover\ \(\(page\)\(.\)\)\)}, {\(2. \ Please\ write\ your\ name\ and\ ID\ on\ this\ page\)}, {3.  Read the questions carefully!}, {\(4. \ Write\ your\ answers\ in\ the\ provided\ \(\(spaces\)\(.\)\)\)}, {5.  THIS IS A CLOSED BOOK EXAM!}}), ({{          , ({{Q, Max P., Points}, {1, 6,  }, {2, 12,  }, {3, 22,  }, {4, 10,  }, {5, 10,  }, {6, 10,  }, {7, 10,  }, { , Sum,  }, { , Max,  }, { , Grade,  }})}})}})

(1) Cells

Why are Input cells special in Mathematica?

(2) Inputs and Outputs

What output does Mathematica produce for the following inputs?

cos (((1 + (Sqrt[1 - x]))/(1 - (Sqrt[1 - x]))))

Output:

f[x] f (x)

Output:

[1, 2, 3] + [1, 1, 1]

Output

Range[5]^2 + 1

Output:

Table[Range[n], {n, 5}]

Output:

Table[Part[Range[5, 20], n], {n, 1, 10, 2}]

Output:

(3) Filling in the Blanks

For the following commands, fill in the missing spaces (□) so that the following respective outputs are produced:

Table[{i, 
        j}, {i, -1, \[Placeholder]}, {\[Placeholder], \[Placeholder], \[Placeholder]}]

Table[{i, j}, {i, -1, 1}, {j, 0, 1}]

sines = Table[Sin[i x], {i, 1, 4}];

Plot[\[Placeholder][sines], {\[Placeholder], \[Placeholder], 
        2  π}]

Plot[Evaluate[sines], {x, 0, 2  π}]

filter[x_] := \[Placeholder] < 50
Select[Range[0, 100, \[Placeholder]], \[Placeholder]]

filter[x_] := x < 50

Select[Range[0, 100, 7], filter]

p[x_] := (x^2)

\[Placeholder][\[Placeholder][5]]

p[Range[5]]

digits = IntegerDigits[65433221]

\[Placeholder][digits] // \[Placeholder]

Union[digits] // Reverse

\[Placeholder][\[Placeholder][\[Placeholder][{1, 2, 3, 4, 
            5}, \[Placeholder]], y], \[Placeholder][(x^2)]]

Append[Prepend[Append[{1, 2, 3, 4, 5}, x], y], {(x^2)}]

\[Placeholder][\[Placeholder][
        Range[-5, 5], -3], \[Placeholder]]

Drop[Drop[Range[-5, 5], -3], 3]

(4) Sorting

(a) What is the output of the following Mathematica command?

Sort[{g[z], h[b], g[z, 1], h[a, b, c], f[k], aaa, ab, a, 5, 
        2}]

(b) What is the output of the following Mathematica commands?

a = z;  b = a; h = g;
Sort[{g[a, b], g[b, a, a], h[z]}]

(c) What is the output of the following Mathematica commands?

g = Plus; a = 1; b = 2; z = 0;
Sort[{g[a, b], g[b, a, a], g[z]}, Greater]

(d) What is the output of the following Mathematica command?

Sort[{(4/2)*4, (2^3)}, Less]

(5) Flattening Expressions

Assume we enter the following command:

expr = f[g[a], h[b], g[c, g[d]], f[e, f[a]]];

What is the output for the following Mathematica commands?

Flatten[expr]

Flatten[expr, Infinity, f]

Flatten[expr, 1, g]

Flatten[expr, 1, List]

Flatten[expr, 2, h]

(6) Extracting Patterns

Assume we enter the following command:

lst = {(a^2), f[c, h[a, (d^3)], g[(b^2)]], b, (d^2), e};

What is the output for the following Mathematica commands?

\(Position[lst, _\^\(\(2\)\(\ \)\)]\)

\(Position[lst, _\^_Integer]\)

Extract[lst, %]

Extract[lst, {{1}, {2, 2, 2}, {4, 2}}]

Position[lst, d]

(7) A Draw Function

Consider the following definition of a function draw which is supposed to implement the selection and subsequent deletion of a randomly picked element from a list.

draw[
        lst_List] := 
Module[{selectedIndex}, 

selectedIndex := 
          Random[Integer, {0, Length[lst]}]; 
Drop[
          lst, {selectedIndex, selectedIndex}]
]

For example, the command

draw[{1, 2, 3, 4, 5}]

might give the result

{1, 2, 4, 5}

where the third element has been selected at random and deleted from the list.

There are two problems with this implementation of the draw function; one problem causes a malfunction of draw, the other problem is minor (and is actually ignored by Mathematica). Explain these two design flaws of the draw function.

Note: Assume that we always pass the right argument (a list) to the draw function.