Solution for First MidTerm Exam

Written Dec, 29, 2008.

Group A :

Part A :

((Fold[Times,1,Table[i,{i,5}]])/(Fold[Times,1,{1,2,3}]Fold[Times,1,{1,2}]))

Graphics[{Rectangle[{0,0},{1,1}],Polygon[{{-.25,1},{0.5,1.5},{1.25,1}}]}~Join~Table[Line[{{1,1.5},{1+0.25 Cos[t],1.5+0.25Sin[t]}}],{t,0,2 Pi,Pi/8}]]

Apply[Or,Map[EvenQ,NestList[2 #+1&,1,100]]]

Part B :

First Question

f[x_]:=If[x<0,-x,x]
Plot[f[x],{x,0,1}]

Second Question

reverse[lst_]:=First[NestWhile[{Append[#[[1]],Last[#[[2]]]],Drop[#[[2]],-1]}&,{{},lst},#[[2]]≠{}&]]

Third Question :

min[lst_]:=First[NestWhile[{If[First[#[[2]]] <  #[[1]],First[#[[2]]], #[[1]]], Rest[#[[2]]]}& ,{Infinity,lst} ,#[[2]]≠ {}&]]

Group B :

Part A :

Fold[#1^#2&,2,Table[i,{i,3}]]

Graphics[{Line[Table[{x,If[x<0,-x,x]},{x,-1,1,.01}]]},Axes->True]

Nest[D[#,x]&,x^5,5]

Part B :

First Question

max[x0_]:=Module[{i,x=x0,max},
max=First[x];
i=2;
While[i ≤ Length[x],
max = If[x[[i]]>max,x[[i]],max];
i++
];
max
]

Second Question

circles[lst_]:=Graphics[Map[Circle[#,0.5]&,lst]]

Third Question :

IsSorted[lst_]:=Fold[And[#1,#2[[1]]<#2[[2]]]&,True,Transpose[{Drop[lst,-1],Drop[lst,1]}]]