question 1

f[x__,n_Integer]/;Length[{x}]>n->g[x]

:=

(Pi^f[2,3,4,2])/f[-1,1]/.
f[x__,n_Integer]/;Length[{x}]>n->g[x]

(Pi^f[2,3,4,2])/f[0,0,1]/.
f[x__,n_Integer]/;Length[{x}]>n->g[x]

question 2

Map[
{
#[[1]][#[[2]]],
#[[1]][2]#[[2]],
#[[2]]#[[1]][#[[1]][1]]
}&,
{
{#^2-1&,2},
{(#-1)(#+1)&,-2}
}
]

question 3

Join[{1,2,3},
Select[{-4,3,2},#>0&]
]

Select[{-4,3,2},#>10&]

Join[{2,3},5]

Clear[tr]

tr[input_List]:=
tr[Select[input,#>0&],{}]

tr[input_List,output_]:=
tr[
Select[input-1,#>0&],
Join[output,{Length[input]}]
]

tr[{},output_]:=output

tr[{3,2,5,1}]

Trace[
tr[{3,2,1}]
]

l={1,3,4,9,9,9,9};

tr[l]

tr[tr[l]]

tr[tr[l]]==Sort[l]

question 4

Clear[f]

f[x_/;x≤2]:=0

f[x_/;x>2]:=1

Plot[f[x],{x,-1,4}]

Clear[f]

f[x_]:=0

f[x_/;-1≤x≤1]:=1

Plot[f[x],{x,-2,4}]

g[x_,y_]:=f[x]f[y]

Plot3D[g[x,y],{x,-2,2},{y,-2,2}]

ContourPlot[g[x,y],{x,-2,2},{y,-2,2}]

Clear[f]

f[x_,y_]:=0

f[x_,y_]/;Sqrt[x^2+y^2]≤1:=1

ContourPlot[f[x,y],{x,-2,2},{y,-2,2},PlotPoints->100]

question 5

Stock inventory
Input: a list of triplets 
l = {
{(Subscript[good, 1]),(Subscript[q, 1]),(Subscript[c, 1])}, {(Subscript[good, 2]),(Subscript[q, 2]),(Subscript[c, 2])}, {(Subscript[good, 3]),(Subscript[q, 3]),(Subscript[c, 3])}, 
... };
Output: for each good in l, a triplet of the form {good, tot, cost}, where tot is the sum of the q’s, and cost is the weighted average of the c’s.

Input =  {{"pears",10 Kg,((4 $)/Kg)},{"apples",6 Kg,((4 $)/Kg)},{"plums",3 Kg,((2 $)/Kg)},
{"plums",6 Kg,((2 $)/Kg)},{"apples",4 Kg,((3 $)/Kg)},{"apples",5 Kg,((2 $)/Kg)}}

Output = {{"pears",10 Kg,((4.` $)/Kg)},{"apples",15 Kg,((3.066666666666667` $)/Kg)},{"plums",9 Kg,((2.` $)/Kg)}}

l1={"apples","pears","peaches","grapes","plums"}[[RandomInteger[{1,5},10]]];

l2=RandomInteger[{2,10},10] Kg;
l3=RandomInteger[{2,4},10] $/Kg;

Transpose[{l1,l2,l3}]

input=MapThread[{#1,#2,#3}&,{l1,l2,l3}]

Clear[l]

input//.
{l___,{fr1_,q1_,c1_},m___,{fr1_,q2_,c2_},r___}:> {l,m,r,{fr1,q1+q2,N[(c1 q1+c2 q2)/(q1+q2)]}}

N[Pi + 4 Pi]

N[((8 $)/Kg)+((4 $)/Kg)]

question 6

Clear[p]

f={f1,f2,f3};
p={p1,p2,p3,p4};

Outer[#1@#2&,f,p]//MatrixForm

Transpose[{f,p}]

Map[#[[1]][#[[2]]]&,
Transpose[{f,p}]
]

f
p

Map[h,p]

Map[
Map[#,p]&,
f]//MatrixForm