6. Lists II:  Iterators and List Handlers: Homework

Last revision:  January 26 1998

Author: YourNameGoesHere

The exercises below ask you to use certain functions which we have not covered.  Never fear!  Use the "?" command to ask Mathematica how to use a named function.

1

First, execute the commands below to create a list named "oddNumbers" of the first 10 positive odd integers using Table[].

Clear[i]

oddNumbers = Table[i,{i,1,19,2}]

(a) Create this list using "Range[]".

(b) Obtain the first four elements of "oddNumbers" using "Take[]".

(c) Obtain the first four elements of "oddNumbers" using "Drop[]".

(d) Obtain the last four elements of "oddNumbers" using "Take[]".

(e) Obtain the last four elements of "oddNumbers" using "Drop[]".

2

Use the examples below to explain the difference between "Length[]" and "Dimensions[]".  Experiment with other lists of lists if necessary.

Length[{{1,2,3},{4,5,7}}]

Dimensions[{{1,2,3},{4,5,7}}]

3

Use "Map[]" and "Length[]" to compute the length of each sublist of the list "mixedLength".  Your answer should be the list {1,2,4,3,1}.

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

4

Use "Table[]" and "Random[]" to create two lists "listOne" and "listTwo" each containg ten random integers between 1 and 100.

(a)  Sort the elements in "listOne".

(b)  Append a 5 to the end of "listTwo".

(c)  Using "Intersection[]", find which numbers are in both "listOne" and "listTwo".

(d)  Join the two lists together with "Join[]".

(e)  Make a list of the numbers that appear in either list.  (Don't include duplicates.)

(f)  List the elements in "listOne" that are not in "listTwo".  (Hint:  use "Complement[]".)

5

"PrimeQ[n]" returns true if "n" is prime and false otherwise.  Use "Select[]", "Table[]", and "PrimeQ[]" to produce a list of all primes from 1 to 100.

6

A two-dimensional random walk on a square lattice is sometimes called a Drunkard's Walk, and it is produced as follows.   Starting from the origin take a random step either left, right, up or down.  Continuing this random behavior for 900 steps produced the path shown below.    Each step can be in one of four directions: {1,0}, meaning right; {0,1}, meaning up;  {-1,0}, meaning left; or {0,-1}, meaning down.  Starting with the "dirList" create a list of 10 random steps in a walk.    Hint:  Step 1--Decide how to get a random element from "dirList".  Step 2--Generate a table of 10 of these random elements.

dirList={{1,0},{0,1},{-1,0},{0,-1}}

7

Use nested "Table[]" commands to create the following list display:

8

Define a function of one positive integer parameter "n" which returns a list of all pairs of positive integers which sum to "n".  For instance, "splitin2[9]" should return "{{1,8},{2,7},{3,6},{4,5}}".  Note that the list does not include both "{4,5}" and "{5,4}".

9

"Building up slowly":  Write a function called "onlyEven" that that takes a list as input and outputs the elements located at even positions.  For example onlyEven[{1,2,3,4,5,6}] should output {2,4,6};  onlyEven[{"a","b","c","d"}] should output {"b","d"} (or {b,d}, as Mathematica likes to omit quotation marks).   Hint:  As a start, consider using "Partition[ ]".

10: Extra Credit

Define a function of one positive integer parameter "n" which returns a list of all triples of distinct positive integers which sum to "n".  For instance, "splitin3[9]" should return "{{1,2,6},{1,3,5},{2,3,4}}".  Note that the list does not include "{1,4,4}" or each of "{2,3,4}", "{2,4,3}", "{3,4,2}", and so on.