CPSC 215 — Assignment 3
Function Mapping and Pure Functions

Winter 2002 — February 15, 2002 — C. Jacob

<your name here>
<your student ID here>

Instructions for this Assignment

Welcome to the third assignment for CPSC 215 — Introduction to Programming with Mathematica.

(1) In the following sections, enter your answers as Mathematica commands in the Input cells provided after each problem description or question.

(2) After you have solved all the problems, go to section "How to Submit Your Notebook" and follow the instructions.

Mapping Functions on Expressions

Problem 1. Squared Primes

Imagine we want to write a function primesSquared[from_Integer, to_Integer] that gives a list of the squares of the prime numbers between from and to.

Here is an example how this function should work and what its output should be:

primesSquared[2, 10]

As the prime numbers between 2 and 10 are 2, 3, 5, and 7, their squares are the numbers in the example list above.

Note: Check out the function PrimeQ in the Help Browser.

Solution

primesSquared[from_Integer, to_Integer] := 
      Select[Range[from, to], PrimeQ]^2

Problem 2. Primes and Their Squares

Continuing from Problem 1, write a function

primesAndTheirSquares[from_Integer, to_Integer]

that returns a list of sublists {p, (p^2)}, where p is a prime number.

For example, we might get the following result:

primesAndTheirSquares[2, 10]

Give at least two different solutions for implementing this function primesAndTheirSquares , one solution should use a pure function (primesAndTheirSquares1), the other solution should not use any pure function (primesAndTheirSquares2).

Solution 1

\(primesAndTheirSquares1[from_Integer, 
        to_Integer] := 
Map[{#, #\^2} &, 
        Select[Range[from, to], PrimeQ]]\)

primesAndTheirSquares1[2, 10]

Solution 2

primesAndTheirSquares2[from_Integer, 
        to_Integer] := 
Module[{s = 
            Select[Range[from, to], PrimeQ]}, 
{s, (s^2)} // 
          Transpose
]

primesAndTheirSquares2[2, 10]

Solving Problems with Pure Functions

Problem 3. Random Number Lists

Step 1: Lists of Random Number Lists

Generate a list of length 10, where each element is a list of random length, which contains between 2 and 10 elements. Each element is a random integer number between 1 and 9.

Continue the following input cell for your solution:

randomNumberLists=

Solution:

randomNumberLists = 
      Table[Table[
          Random[Integer, {1, 9}], {Random[
              Integer, {2, 10}]}], {10}]

Step 2: Sorting and Counting

Now map a  function on each element of this list, which does the following: For each sublist it should sort this sublist, count the number of elements of this sublist, and combine these two results into a new list. (Note: Check out the functions Sort and Count in the Help Browser.)

Here is an example. For the list

{{3, 1, 2}, {4, 2}, {5, 3, 1, 2}}

the resulting list should be

{{{1, 2, 3}, 3}, {{2, 4}, 2}, {{1, 2, 3, 5}, 4}}

Each of the sublists has been sorted, and the number of its elements appears after each of the sorted lists.

Now, use a pure function applied to randomNumberLists to implement this.

Solution

rnl = Map[{Sort[#], Length[#]} &, randomNumberLists]

Step 3. Sorting

Finally, sort the lists resulting from Step 2 with respect to the number of their elements.

For the example above, we would sort the list

{{{1, 2, 3}, 3}, {{2, 4}, 2}, {{1, 2, 3, 5}, 4}}

like this:

{{{2, 4}, 2}, {{1, 2, 3}, 3}, {{1, 2, 3, 5}, 4}}

Write a function sortRandomNumberLists[lst_List] that takes a list as this one (with a structure of the lists resulting from Step 2) and rearranges the sublists according to their lengths.

Solution

sortRandomNumberLists[lst_List] := 
Map[Reverse, 
        Map[Reverse, lst] // Sort]

sortRandomNumberLists[rnl]

Problem 4. Image Encoding

The following list encodedImage

encodedImage = 
{{0, 11}, {1, 2}, {0, 1}, {1, 
            2}, {0, 3}, {1, 1}, {0, 5}, {1, 1}, {0, 3}, {1, 2}, {0, 1}, {1, 
            2}, {0, 4}, {1, 2}, {0, 1}, {1, 2}, {0, 6}, {1, 1}, {0, 8}, {1, 
            1}, {0, 8}, {1, 1}, {0, 6}, {1, 1}, {0, 3}, {1, 1}, {0, 5}, {1, 
            3}, {0, 7}, {1, 1}, {0, 13}};

describes the pixels (= dots) of an image in the following way:

Each sublist has the form {p, n}, where p is either 0 or 1, and n denotes how many times the element p is repeated in the list. In other words, each sublist {p, n} is a compressed representation (a "code") for a list which repeats element p for n times.

So, for example, the first three sublists of the list above

{{0, 11}, {1, 2}, {0, 1}}

encodes the expanded list

{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0}

According to the "encoding" there are 11 zeros, followed by 2 ones, followed by 1 zeros.

Given the encodedImage list, write a function imageDecoding[imageCode_List] that expands the encoded list representation into its decoded version, as described in the example.

imageDecoding[imageCode_List]:=

Test your imageDecoding function on the example above by using the following function call:

(Partition[imageDecoding[encodedImage], 
            9] /. {0 -> □, 1 -> ■}) // 
      ColumnForm

If you can see an image, you have a working solution!

Note: The described encoding is called "run-length encoding" and is used for compressing binary images.

Solution

imageDecoding[
        imageCode_List] := 
Map[
          Table[First[#], {Last[#]}] &, imageCode] // Flatten

decodedImage = imageDecoding[encodedImage]

(Partition[decodedImage, 9] /. {0 -> □, 
            1 -> ■}) // ColumnForm

This is how I constructed the image: (not required for the student solutions)

faceEncoding = 
      Map[{First[#], Length[#]} &, Split[Flatten[face]]]

face = {
{0, 0, 0, 0, 0, 0, 0, 0, 
            0}, 
{0, 0, 1, 1, 0, 1, 1, 0, 
            0}, 
{0, 1, 0, 0, 0, 0, 0, 1, 
            0}, 
{0, 0, 1, 1, 0, 1, 1, 0, 
            0}, 
{0, 0, 1, 1, 0, 1, 1, 0, 
            0}, 
{0, 0, 0, 0, 1, 0, 0, 0, 
            0}, 
{0, 0, 0, 0, 1, 0, 0, 0, 
            0}, 
{0, 0, 0, 0, 1, 0, 0, 0, 
            0}, 
{0, 0, 1, 0, 0, 0, 1, 0, 
            0}, 
{0, 0, 0, 1, 1, 1, 0, 0, 
            0}, 
{0, 0, 0, 0, 1, 0, 0, 0, 
            0}, 
{0, 0, 0, 0, 0, 0, 0, 0, 
            0}
};

(face /. {0 -> □, 1 -> ■}) // 
      ColumnForm

Bonus question:

Can you write a function to perform the encoding of an image?

faceEncoding = 
      Map[{First[#], Length[#]} &, Split[Flatten[face]]]

How to Submit Your Notebook

Step 1

Save this notebook under the filename Ass3-<your student id>.nb.

For example, if your student ID is 123456, you would save this notebook as Ass3-123456.nb

Step 2

Send your notebook by email to your Teaching Assistant (TA).