Mathematica Shortcuts and Modifying Lists

Comprehension Questions.

Math 250 - Math with Mathematica
Christopher Hanusa

Questions for All

3-1. For each of the following Table commands, complete the following sub-questions.
(a) BEFORE EVALUATING THE COMMAND, what list do you expect the command to give?  Or do you expect an error?
(b) Now, evaluate the command.  Did it do what you expect it to do?
(c) If not, figure out what went wrong with your reasoning.

Table[{s,7,9},{t,3,5}]

Table[xy,{x,2,4},{y,5,7}]

Table[Plot[x^n,{n,-2,2}],{x,1,10}]

Table[CountryData[country,"Population"],{country,{"France","United States","China"}}]

3-2. Determine which Table commands give the following lists.  There are multiple solutions for each one.

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

{{5,5,5},{6,6,6},{7,7,7},{8,8,8},{9,9,9},{10,10,10}}

{{"France","has",(Quantity[64101308, "people", people, "People"])},{"United States","has",(Quantity[322422965, "people", people, "People"])},{"China","has",(Quantity[1364773138, "people", people, "People"])}}

Challenge Question

3-3. Determine which Table commands give the following lists.

{{B,(B^2),(B^3)},{A B,A (B^2),A (B^3)},{(A^2) B,(A^2) (B^2),(A^2) (B^3)},{(A^3) B,(A^3) (B^2),(A^3) (B^3)},{(A^4) B,(A^4) (B^2),(A^4) (B^3)}}

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

{,,,,,,,,,,}

Questions for All

3-4. What is the difference between the following two lines of code? Explain the difference in outputs.

Table[Plot[x^n,{x,-2,2}],{n,1,10}]

Table[Plot[x^n,{n,-2,2}],{x,1,10}]

3-5. For each of the following commands, complete the following sub-questions.
(a) What list do you expect the command to give?  Or do you expect an error?
(b) Now evaluate the command.  Did it do what you expect it to do?
(c) If not, figure out what went wrong with your reasoning.

Flatten[{{{{1},2},3},4}]

Flatten[{{{{1},2},3},4},2]

Append[{1,2,3,4},{1}]

Append[{{1,2},{3,4}},5,6]

Append[{{1,2},{3,4}},{5,6}]

Challenge Question

3-6. Below is the Mathematica input and output for someone hoping to make a list of square numbers and then append the next square number to the end of that list. How should the code be modified to do these two desired operations?

squares=Table[i^2,{5}]
Append[6^2,squares]

Questions for All

3-7. Here are two lists which represent the x- and y-coordinates of points, and an incorrect Table command that hopes to collect the points into a list of ordered x,y pairs.  Figure out what is wrong with the command and fix it.

x={1,2,3,4,5}
y={2,5,10,17,26}
points=Table[{x[i],y[i]},{i,1,Length[x]}]

Challenge Question

3-8. How can you extract the diagonal of a table?

Questions for All

3-9. Write out in English the first 10 multiples of three.  You may find the command IntegerName useful.

3-10. What should you type to extract the list {{a,b},{q,r}} from veryverydeeplist?

veryverydeeplist = {{{{{a,b},{c,d}},{{e,f},{g,h}}},{{{i,j},{k,l}},{{m,n},{o,p}}}},{{{{q,r},{s,t}},{{u,v},{w,x}}},{{{y,z},{α,β}},{{γ,δ},{ϵ,ζ}}}}}

Exploration Question

3-11. Visit the Documentation Center entry for Getting Pieces Of Lists to see other commands Mathematica uses to take pieces of lists.  What does the ;; operator do?

Questions for All

3-12. What will be output by Mathematica when the following set of commands is evaluated? Explain why you give the answer you give.

fibowhat={1,1,2,3,5,8,13,21};
Total[fibowhat[[4;;7]]]

3-13. Write the code that takes entries 5 through 15 of this list primes

primes=Table[Prime[n],{n,1,20}]

Challenge Questions

3-14. Create a histogram of the number of letters found in the English words for the numbers 1 through 100.  What is the most common length?

3-15. What code should you use to choose the odd entries of the variable powersOfTen?  Can you make it a general piece of code that works no matter how long the variable powersOfTen is?