**Hotspots**: Admin Pages | Turn-in Site |

**Current Links**: **Cases** ** Final Project Summer 2007**

## Spring 2004 Quiz 2

### Problem 1

Write workspace code using appropriate control messages that will perform and print out to the Transcript factorial computations, as illustrated below. The successive lines show the factorial computations starting from 2 factorial. They should continue until a result greater than 1000 has been computed and printed. (Your code must be written to test this condition, not have an appropriate stopping point built-in because you can pre-compute the first factorial greater than 1000).

I want to see

1 * 2 = 2
1 * 2 * 3 = 6
1 * 2 * 3 * 4 = 24
...

*final computation, with answer > 1000*

### Solution 1A

| n |
n := 1.
[ (n factorial) <= 1000 ] whileTrue: [
1 to: n do: [: N | Transcript show: (N asString), ' * ' ].
n := n + 1.
Transcript show: (n asString), ' = '.
Transcript show: (n factorial asString), (String cr).
].

### Solution 1B

| factorial n |
factorial := 1.
n := 2.
[ factorial <= 1000 ] whileTrue: [
1 to: (n - 1) do: [: N | Transcript show: (N asString), ' * ' ].
factorial := factorial * n.
Transcript show: (n asString), ' = ', (factorial asString); cr.
n := n + 1.
].

### Solution 1C

| n equation |
n := 1.
equation := '1'.
[ (n factorial) > 1000 ] whileFalse: [
n := n + 1.
equation := equation, ' * ', (n asString).
Transcript show: equation, ' = ', (n factorial asString); cr.
].

### Problem 2

Write workspace code that begins by sending a message to a collection of strings called myStrings. The result of the code should be a new collection that includes the length of any string in the original collection that is less than 10 characters long. Note that the size of the collection produced will not match the size of hte original collection unless all of the strings in myStrings are less than 10 characters long.

You must use collect:, select:, reject:, or do: at least once in your solution.

### Solution 2A

(myStrings select: [: s | (s size) < 10 ]) collect: [: t | t size ].

### Solution 2B

(myStrings reject: [: s | (s size) >= 10 ]) collect: [: t | t size ].

### Solution 2C

| newList |
newList := OrderedCollection new.
myStrings do: [: s |
( (s size) < 10 ) ifTrue: [ newList add: (s size) ].
].

### Link to this Page