LIBS

libLIST=ReadString["/Users/darashayda1xfer/Desktop/MODULES/BU/libs/libLIST.m"]<>"\n";
libNAMES=ReadString["/Users/darashayda1xfer/Desktop/MODULES/BU/libs/libNAMES.m"]<>"\n";
libIO=ReadString["/Users/darashayda1xfer/Desktop/MODULES/BU/libs/libIO.m"]<>"\n";
libERR=ReadString["/Users/darashayda1xfer/Desktop/MODULES/BU/libs/libERR.m"]<>"\n";
libGUI=ReadString["/Users/darashayda1xfer/Desktop/MODULES/BU/libs/libGUI.m"]<>"\n";
libML=ReadString["/Users/darashayda1xfer/Desktop/MODULES/BU/libs/libML.m"]<>"\n";
libMISC=ReadString["/Users/darashayda1xfer/Desktop/MODULES/BU/libs/libMISC.m"]<>"\n";
libGraphics=ReadString["/Users/darashayda1xfer/Desktop/MODULES/BU/libs/libGraphics.m"]<>"\n";
libSYMLOGIC=ReadString["/Users/darashayda1xfer/Desktop/MODULES/BU/METCS248.m"]<>"\n";
libDEBUG=ReadString["/Users/darashayda1xfer/Desktop/MODULES/Ryerson 2016/TRSM/QMS202/libs/libDEBUG.m"]<>"\n";


(*libLIST=ReadString["C:/mathematica/Work/lossofgenerality/QMS202/QUIZES/libLIST.m"]<>"\n";
libNAMES=ReadString["C:/mathematica/Work/lossofgenerality/QMS202/QUIZES/libNAMES.m"]<>"\n";
libIO=ReadString["C:/mathematica/Work/lossofgenerality/QMS202/QUIZES/libIO.m"]<>"\n";
libERR=ReadString["C:/mathematica/Work/lossofgenerality/QMS202/QUIZES/libERR.m"]<>"\n";
libGUI=ReadString["C:/mathematica/Work/lossofgenerality/QMS202/QUIZES/libGUI.m"]<>"\n";
libML=ReadString["C:/mathematica/Work/lossofgenerality/QMS202/QUIZES/libML.m"]<>"\n";
libMISC=ReadString["C:/mathematica/Work/lossofgenerality/QMS202/QUIZES/libMISC.m"]<>"\n";
libGraphics=ReadString["C:/mathematica/Work/lossofgenerality/QMS202/QUIZES/libGraphics.m"]<>"\n";*)

Module Area

data1="\n"<>"textfghjkl:="<>ToString[<|

(*fghjkl. removed*)


"0"-><|
"0.1"->{"Column[{ToString[\" \"]"<>"}]"}
|>
,
(* let's use key as the option *)
"1"-><|
"1.1"->{"Column[{ToString[\"1. Complete the following: \"]"<>"}]",
"Column[{ToString[\"1. Fill in the missing values: \"]"<>"}]"}
|>

,

"2"-><|
"2.1"->{"Column[{ToString[\"2. Is the following table correct: \"]"<>"}]",
"Column[{ToString[\"2. Determine if the following logic table is correct: \"]"<>"}]"}
|>

,

"3"-><|
"3.1"->{"Column[{ToString[\"3. Is this statement a tautology, indicate T or F: \"]"<>"}]",
"Column[{ToString[\"3. Indicate T if a tautology: \"]"<>"}]"}
|>

,
"4"-><|
"4.1"->{"Column[{ToString[\"4. Is the following logic decomposition valid, indicate T or F: \"]"<>"}]",
"Column[{ToString[\"4. Is the following logic decomposition correct, indicate T or F: \"]"<>"}]"}
|>





|>,InputForm]<>";\n ";



(*data2="listfghjkl:="<>


ToString[{"",""


},InputForm]<>";\n ";*)

(* ANNA add a lot more here *)
data2="listfghjkl:="<>ToString[{
{
{ ¬p,¬q,p∨q,p∧q,¬p∨q,p∧(¬q)},
{¬p,¬q,p\[Implies](¬q),(¬p)⧦q},
{p⊽q,p⊻q,p\[Xnor]q},
{p∧q,(p∧q)\[Implies]q}
},

{{¬r,¬p,¬q,p∨r,r\[Implies]q ,p⊼r},{p\[Implies]q,q\[Implies]r,p\[Implies]q\[Implies]r,p∧q},{p∧q\[Implies]r,(p\[Implies]q\[Implies]r)\[Implies](p∧q\[Implies]r)},
{!r\[Implies]p,p\[Implies]!q,q\[Implies]r,(!r\[Implies]p)&&(p\[Implies]!q)},{(!r\[Implies]p)&&(p\[Implies]!q)\[Implies](q\[Implies]r)},
{q\[Implies]r,p\[Implies](q\[Implies]r)}}
}]<>";\n ";

data3="tautologies:="<>ToString[{p∨¬p, (p∧¬p)\[Implies]p, (¬p\[Implies]p)\[Implies]p,((¬p⇒q)∧(¬p\[Implies]¬q))⇒p,((p\[Implies]q)∧¬q)\[Implies]¬p,((p∨q)∧¬q)\[Implies]p , ((p\[Implies]q)\[Implies]p)\[Implies]p, (p⧦q)⇔((p\[Implies]q)∧(q\[Implies]p)),(p\[Implies](q\[Implies]r))⇒((p\[Implies]q)\[Implies](p\[Implies]r)),((p\[Implies]q)∧(p\[Implies]r))\[Implies](p\[Implies](q∧r)),((p\[Implies]r)∧(q\[Implies]r))\[Implies]((p∨q)\[Implies]r), ((p∧q)\[Implies]r)\[Implies](p\[Implies](q\[Implies]r)),(p\[Implies](q\[Implies]r))\[Implies]((p∧q)\[Implies]r), ((p\[Implies]r)∧(q\[Implies]s))\[Implies]((p∨q)\[Implies](r∨s)), ((p\[Implies]r)∧(q\[Implies]s))\[Implies]((p∧q)\[Implies](r∧s)),((p∨q)\[Implies]r)\[Implies]((p\[Implies]r)∧(q\[Implies]r)), (p\[Implies](q∧r))\[Implies]((p\[Implies]q)∧(p\[Implies]r)), (p⇔q)\[Implies]((p∧r)⧦(q∧r)),(p⧦q)\[Implies]((p∨r)⧦(q∨r)),(p\[Implies](q∧r))⧦((p\[Implies]q)∧(p\[Implies]r))}]<>";\n ";

(*ANNA check*)
data4="tautologiesNOT:="<>ToString[{p∧¬p, ((¬p⇒¬q)∧(¬p\[Implies]¬q))⇒p,((p\[Implies]q)∨q)\[Implies]¬p,((p∧q)∧¬q)\[Implies]p , ((¬p\[Implies]q)\[Implies]p)\[Implies]p, (p⧦q)⇔((¬p\[Implies]q)∧(q\[Implies]p)),(p\[Implies](q\[Implies]¬r))⇒((p\[Implies]q)\[Implies](p\[Implies]r)),((p\[Implies]q)∧(p\[Implies]¬r))\[Implies](p\[Implies](q∧r)),((p\[Implies]¬r)∧(q\[Implies]r))\[Implies]((p∨q)\[Implies]r), ((p∧q)\[Implies]r)\[Implies](p\[Implies](q\[Implies]¬r)),(p\[Implies](q\[Implies]r))\[Implies]((p∧q)\[Implies]¬r), ((p\[Implies]r)∧(q\[Implies]¬s))\[Implies]((p∨q)\[Implies](r∨s)), ((p\[Implies]r)∧(q\[Implies]s))\[Implies]((p∧q)\[Implies](r∧¬s)),((p∨q)\[Implies]r)\[Implies]((p\[Implies]r)∧(q\[Implies]¬r)), (p\[Implies](q∧r))\[Implies]((p\[Implies]q)∧(p\[Implies]¬r)), (p⇔q)\[Implies]((p∧¬r)⧦(q∧r)),(p⧦q)\[Implies]((p∨¬r)⧦(q∧r)),(p\[Implies](q∧¬r))⧦((p\[Implies]q)∧(p\[Implies]r)),p\[Implies]¬p   }]<>";\n ";

data=ToString[{data1, data2,data3,data4}, InputForm];





canvas1="\n"<>ToString[Hold[Module[{vars=ertyuiop,text,text2,answers, options,locs,data,headers, variables,table,h,expr,sentence,expr2,header, table2,table3,header2},

ToExpression@libLIST;
text=vars["text"];
ToExpression@libDEBUG;
debugINIT["adA9xZyD"];


{headers, variables,locs,table,table2,table3,header2}=vars["vars"][[1]];

Grid[{

{Image[,ImageSize->125]},
{"Prof. Madani"},   
{"Homework 1B,Logic Statements, January 2016"},
{Image[]},
{"id"},
{Image[]},
{Image[]},
{Grid[{{Rasterize@ToExpression@text[[2]]}}]},

h=Rasterize/@(TraditionalForm/@(ToExpression@headers));
{Grid[{h}~Join~table,ItemSize->All,Frame->All]},
{Image[]},
{Image[]},
{Grid[{{Rasterize@ToExpression@text[[3]]}}]},
{"x5"},
{Image[]},
{headers, variables,locs,table,table2,table3,header2}=vars["vars"][[2]];
h=Rasterize/@(TraditionalForm/@(ToExpression@headers));
{Grid[{h}~Join~table,ItemSize->All,Frame->All]},
{Image[]},
{Image[]},
{Grid[{{Rasterize@ToExpression@text[[4]]}}]},
{Rasterize@TraditionalForm["Note: &&=∧ , ||= ∨, !=¬"]},
{"x6"},
{Image[]},
{expr}=vars["vars"][[3]];
(*{Rasterize@(TraditionalForm[ToExpression[expr]])}*)
{Rasterize@(TraditionalForm[expr])}
,

{Image[]},

{Grid[{{ToExpression@text[[5]]}}]},
{"x7"},
{Image[]},
{expr, variables, sentence,expr2}=vars["vars"][[4]];
{Rasterize@ToExpression@sentence},
{Image[]},
{"Hint: use this logic table"},
{Rasterize@makeTruthTable4[ToExpression[expr],ToExpression[variables]]},
 {Image[]}

},Spacings->{0,3}]


]],InputForm]<>"\n ";





variation1=ToExpression[data][[1]]<>ToExpression[data][[2]]<>ToExpression[data][[3]]<>ToExpression[data][[4]]<>ToString[Hold[Module[{key,x,a,b,c,d,e,f,g,h,i,j,m,n,y,aa,bb,cc,dd,ee, ff, gg, hh, ii, jj,pp, vars,text,list,sol,aabbee,k, parta, partb, partc, partd,parte,partf,question,list12,teste,sol2,sol3,t,mat,matS,matSRED,locs,nn,index, table,variables,expr,header,d1,d2,varLogic,assoc2,assoc,ss,course, name,varLogicGeneral,table2,table3,flip,neg,or},



ToExpression@libDEBUG;
debugINIT["adA9xZyD"];


question=RandomChoice[textfghjkl["0"]];

(*a*)

key=RandomChoice[Keys[textfghjkl["1"]]];
text=RandomChoice[textfghjkl["1"][key]];


Which[key=="1.1", 



nn=RandomInteger[{1,2}]; (* n could be used as variable name for logic *)
expr=qRandList[listfghjkl[[nn]]];



Clear[p,q,r]; (*FIXME:p and q need to be global and reserved for this boolean table*)
variables=If[ nn==1,nLetters[{"a","o"},2],nLetters[{"a","o"},3]] ; 
header=(variables~Join~expr)/.If[nn==1, {p->variables[[1]],q->variables[[2]]},{p->variables[[1]],q->variables[[2]],r->variables[[3]]}] ; 



index=RandomSample[cartProd[Range[1,2^Length[variables]],Range[Length[variables]+1,Length[header]]],4];




table=BooleanTable[header,variables]/.{True->"T", False->"F"};
table2=BooleanTable[header,variables]/.{True->"\"T\"", False->"\"F\""};
table3=BooleanTable[header,variables]/.{True->"T", False->"F"};
Table[table[[index[[i]][[1]],index[[i]][[2]]]]="x"<>ToString[i],{i,1, Length@index}];
Table[table3[[index[[i]][[1]],index[[i]][[2]]]]=Style[Subscript[table3[[index[[i]][[1]],index[[i]][[2]]]],i],Orange],{i,1, Length@index}];
sol=table2[[#[[1]],#[[2]]]]&/@index;


(*Kludge: reformat and copy to match send*)
vars={ToString[header,InputForm],ToString[variables],ToString[index],table,table2,ToString[table3,InputForm],ToString@ToString[header,InputForm]};
parta={"",vars,ToString[sol[[1]]]} ;




parta={StringReplace[text, { "#a"->a,"#b"->b}],vars,sol} ;



];



(*b*)

key=RandomChoice[Keys[textfghjkl["2"]]];
text=RandomChoice[textfghjkl["2"][key]];



Which[key=="2.1",



nn=RandomInteger[{1,2}]; (* n could be used as variable name for logic *)
expr=qRandList[listfghjkl[[nn]]];



Clear[p,q,r]; (*FIXME:p and q need to be global and reserved for this boolean table*)
variables=If[ nn==1,nLetters[{"a","o"},2],nLetters[{"a","o"},3]] ; 
header=(variables~Join~expr)/.If[nn==1, {p->variables[[1]],q->variables[[2]]},{p->variables[[1]],q->variables[[2]],r->variables[[3]]}] ; 

index=RandomSample[cartProd[Range[1,2^Length[variables]],Range[Length[variables]+1,Length[header]]],4];


table=BooleanTable[header,variables];


If[RandomInteger[]==1,
Table[table[[index[[i]][[1]],index[[i]][[2]]]]=Not[table[[index[[i]][[1]],index[[i]][[2]]]]],{i,1, Length@index}];
sol={"\"F\""},

sol={"\"T\""}];

table=table/.{True->"T", False->"F"};
table2=BooleanTable[header,variables]/.{True->"T", False->"F"};
table3=BooleanTable[header,variables]/.{True->"T", False->"F"};


Table[table3[[index[[i]][[1]],index[[i]][[2]]]]=Style[table3[[index[[i]][[1]],index[[i]][[2]]]],Red],{i,1, Length@index}];

vars={ToString[header,InputForm],ToString[variables],ToString[index],table,table2,ToString[table3,InputForm],ToString@ToString[header,InputForm]};


partb={StringReplace[text, { "#a"->a,"#b"->b}],vars,sol} ;

];

key=RandomChoice[Keys[textfghjkl["3"]]];
text=RandomChoice[textfghjkl["3"][key]];

Which[key=="3.1",



Clear[p,q,r,s]; (*FIXME:p and q need to be global and reserved for this boolean table*)


If[RandomInteger[]==1,vars={RandomChoice@tautologies};sol={"\"T\""},vars={RandomChoice@tautologiesNOT};sol={"\"F\""}];



partc={StringReplace[text, { "#a"->a,"#b"->b}],vars,sol} ;

];


key=RandomChoice[Keys[textfghjkl["4"]]];
text=RandomChoice[textfghjkl["4"][key]];




Which[key=="4.1",


d1={

{<|"¬s"->{"fails to submit a project in course ","cannot do a project in course "},"s"->{"must have submitted a project ","must have done a project "},"f"->{"fails the course ","flunks the course "},
"g"->{"graduates"},"¬g"->{"cannot graduate"},"info"->{"1"}|>},

{<|"s"->"@p is an integer ","f"->"@p is odd","g"->"@p is even","info"->{"2"}|>,
<|"s"->"@p is prime ","f"->"@p = 2","g"->"@p is odd","info"->{"2"}|>,
<|"s"->"@p@r=0 ","f"->"@p = 0","g"->"@r = 0","info"->{"2"}|>(*,
<|"s"->ToString[Abs["@p"],TraditionalForm]<>"=#q","f"->ToString["@p=+#q",TraditionalForm],"g"->ToString["@p=-#q",TraditionalForm],"info"->{"2"}|>*)
},

{<|"f"->{"I take all exams","I complete all exams","I take every exam"},"g"->{"I get an A", "A is my grade","my grade will be A","I will be A student"},"¬f"->{"I do not take all exams","I miss some exams","I do not attend some exams"},"¬g"->{"I will not get an A", "I get less than A", "my grade will not be A"},"info"->{"3"}|>}(*,

{<|"f"->{"it rains ","it is raining ","it showers "},"g"->{"I get wet ", "I will be drenched","I will be soaked","truely wet I will be "},"¬f"->{"it does not rain ","it is sunny skies ","it is clear skies","there is no cloud in sight "},"¬g"->{"I will not be wet" , "I will be dry ", "no way I will get wet "},"info"->{"4"}|>},

{<|"f"->{"I do not eat a lot ","I eat little ","I deit","I go on deit "},"g"->{"I get slim ","I slim up ", "I lose weight " },"¬f"->{"I do eat a lot","I have no control on my eating","I pig out"},"¬g"->{"I will not lose weight ", "I will not be slim " , "I will not be fit ","I get fat ", "I gain weight ","I will be overweight "},"info"->{"5"}|>}*)

};

d2={

<|"or"->{"or"},"then"->{", then ",", consequently ",", ",", thus ",", therefore ", ", as a result ", ", as a consequence "},"if"->{"If","Let's say ", "On condition that ","In the event that ","In the case that ","On the assumption that ","Supposing"},"h"->{"Hence, ", "Therefore, ","In other words, "},"¬"->{"it is not true that "},"¬2"->{"nor it is true that "}|>,

{<|"name"->"Joe","pronoun"->{"he","him", "his"}|>,<|"name"->"Jack","pronoun"->{"he","him", "his"}|>,
<|"name"->"Deidre","pronoun"->{"she","her", "hers"}|>,<|"name"->"Lucia","pronoun"->{"she","her", "hers"}|>}

};


varLogic=qRandList[d1];
(*varLogic=d1[[3]];*)

varLogicGeneral=d2[[1]];

flip=RandomInteger[];

If[flip==1,neg="";or="∧",neg="¬";or="∨"];


vars=Which[


varLogic[[1]]["info"][[1]]=== "1",
{ss,ff,gg}=nLetters[{"a","z"},3];
assoc2:=qRandList/@varLogicGeneral;
assoc:=qRandList/@(varLogic[[1]]);
name=qRandList@d2[[2]];
course=qRandList[{"METCS248","METCS556","METCS662"}];

{
{ToString/@({f,g,s,¬s\[Implies]f,f\[Implies]¬g,g\[Implies]s,(¬s\[Implies]f)∧(f\[Implies]¬g),((¬s\[Implies]f)∧(f\[Implies]¬g))\[Implies]g\[Implies]s}/.{f->ff,g->gg,s->ss}),
ToString/@({f,g,s}/.{f->ff,g->gg,s->ss}),

ToString[
Row[{
Labeled[Framed[assoc2["if"],Background->LightGray], Style["",Bold, Black]],
name["name"]<>" ",
Labeled[Framed[assoc["¬s"],Background->LightGreen], Style[neg<>ss,Bold, Green]],
course<>" ",
Labeled[Framed[assoc2["then"],Background->LightGray], Style["\[Implies]",Bold, Black]],
Labeled[Framed[assoc["f"],Background->LightRed], Style[ff,Bold, Red]],
Labeled[Framed[assoc2["if"],Background->LightGray], Style["",Bold, Black]],
name["name"]<>" ",
Labeled[Framed[StringReplace[assoc["f"],{"the course"-> course}],Background->LightRed], Style[ff,Bold, Red]],
Labeled[Framed[assoc2["then"],Background->LightGray], Style["\[Implies]",Bold, Black]],
name["pronoun"][[1]]<>" ",
Labeled[Framed[assoc["¬g"],Background->LightGreen], Style[neg<>gg,Bold, Green]],assoc2["h"],
Labeled[Framed[assoc2["if"],Background->LightGray], Style["",Bold, Black]],
name["name"]<>" ",
Labeled[Framed[assoc["g"],Background->LightBlue], Style[gg,Bold, Blue]],
Labeled[Framed[assoc2["then"],Background->LightGray], Style["\[Implies]",Bold, Black]],
name["pronoun"][[1]]<>" ",
Labeled[Framed[assoc["s"],Background->LightGreen], Style[ss,Bold, Green]]}]

,InputForm],

((¬s\[Implies]f)∧(f\[Implies]¬g))\[Implies]g\[Implies]s/.{f->ff,g->gg,s->ss}
}
}
,

varLogic[[1]]["info"][[1]]=== "2", 
{ss,ff,gg}=nLetters[{"a","z"},3]; (* caps to avoid confusion with p and q and r *)
{pp,rr}=nLetters[{"a","z"},2];
assoc2:=qRandList/@varLogicGeneral;
tmp=ToString[RandomReal[]];
assoc=StringReplace[#,{"@p"->pp,"@r"->rr,"#q"->tmp}]&/@((qRandList@varLogic));

{{ToString/@({f,g,s,f ∨ g,s\[Implies](f∨g)}/.{f->ff,g->gg,s->ss}),
ToString/@({f,g,s}/.{f->ff,g->gg,s->ss}),
ToString[Row[{

Labeled[Framed[assoc2["if"],Background->LightGray], Style["",Bold, Black]],

Labeled[Framed[assoc["s"],Background->LightGreen], Style[ss,Bold, Green]],
Labeled[Framed[assoc2["then"],Background->LightGray], Style["\[Implies]",Bold, Black]],

Labeled[Framed[assoc["f"],Background->LightRed], Style[ff,Bold, Red]],
Labeled[Framed[assoc2["or"],Background->LightGray], Style[or,Bold, Black]],
Labeled[Framed[assoc["g"],Background->LightBlue], Style[gg,Bold, Blue]]
}],InputForm],


(s\[Implies](f∨g))/.{f->ff,g->gg,s->ss}

}
}

,

varLogic[[1]]["info"][[1]]=== "3", 
{ff,gg}=nLetters[{"a","z"},2]; (* caps to avoid confusion with p and q and r *)
assoc2:=qRandList/@varLogicGeneral;
assoc:=qRandList/@varLogic[[1]];

{{ToString/@({f,g,f \[Implies] g,¬f\[Implies]¬g,(¬f\[Implies]¬g)∧¬(f\[Implies]g)}/.{f->ff,g->gg}),
ToString/@({f,g}/.{f->ff,g->gg}),

ToString[Row[{
Labeled[Framed[assoc2["if"],Background->LightGray], Style["",Bold, Black]],
Labeled[Framed[assoc["¬f"],Background->LightGreen], Style[neg<>ff,Bold, Green]],
Labeled[Framed[assoc2["then"],Background->LightGray], Style["\[Implies]",Bold, Black]],
Labeled[Framed[assoc["¬g"],Background->LightGreen], Style["¬"<>gg,Bold, Green]],
Labeled[Framed[assoc2["¬"],Background->LightRed], Style[neg,Bold, Red]],
Labeled[Framed[assoc2["if"],Background->LightGray], Style["",Bold, Black]],

Labeled[Framed[assoc["f"],Background->LightRed], Style[ff,Bold, Red]],
Labeled[Framed[assoc2["then"],Background->LightGray], Style["\[Implies]",Bold, Black]],
Labeled[Framed[assoc["g"],Background->LightBlue], Style[gg,Bold, Blue]]
}],InputForm],

((¬f\[Implies]¬g)∧¬(f\[Implies]g))/.{f->ff,g->gg}

}
}

];

If[flip==1, sol={"\"F\""}, sol={"\"T\""}];



partd={StringReplace[text, { "#a"->a,"#b"->b}],vars[[1]],sol} ;

];


vars=ToString[<|"text"->{question, parta[[1]],partb[[1]],partc[[1]],partd[[1]](*,parte[[1]],partf[[1]]*)},"vars"->{ parta[[2]],partb[[2]],partc[[2]],partd[[2]](*,parte[[2]],partf[[2]]*)}, "sol"->{ parta[[3]],partb[[3]],partc[[3]],partd[[3]](*,parte[[3]],partf[[3]]*)}|>,InputForm];

{StringReplace[ToString[canvasZ,InputForm],{"ertyuiop"->vars}],{ parta[[1]],partb[[1]],partc[[1]],partd[[1]]},{ parta[[2]],partb[[2]],partc[[2]],partd[[2]](*,parte[[2]],partf[[2]]*)}, { parta[[3]],partb[[3]],partc[[3]],partd[[3]](*,parte[[3]],partf[[3]]*)}}


(* it has to be InputForm or ToExpression fails *)
]],InputForm]<>"\n ";


solution1=data1<>data2<>ToString[Hold[Module[{x, a,b,c,d,aa,bb,cc,dd,ee, ff, gg, hh, ii, jj, text,list,sol, body={},vars=ertyuiop,options=ERTYUIOP, varsa, varsb, varsc,varsd,varse,varsf,g,h,pp,teste,bb1,list123,pos1,t11,t12,t21,t22,s1,s2,sum1,sum2,k},





varsa=vars[[1]];
varsb=vars[[2]];



(*
{aa,bb,cc,dd,ee,gg,pp}=varsa;
sol=Solve[ToExpression[bb]*x-ToExpression[cc]*(x-ToExpression[dd])==ToExpression[ee]-ToExpression[gg]*x,x];
*)


body={
{"",""

}, 
{Null ,Null} 
};


Panel[Grid[Transpose[body]]]

]],InputForm]<>"\n ";


hint1=ToString[Hold[Module[{aa,bb,cc,dd,ee, ff, gg, hh, ii, jj, text,list,sol, body={},vars=ertyuiop,options=ERTYUIOP},  


body={ 
{""
},
{Null}
};

Panel[Grid[Transpose[body]]]

]],InputForm]<>"\n ";

Cloud Area

(* all err functions need to be called here since the Send is entirely happens in one unit *)
send[a0_]:=Module[{a,b,c,d,sol0,canvas0,err1,err2,err3,err4,err5,err6,resF,canvas,vars,sol,sol1,sol2,sol3,sol4,sol5,sol6,μ,σ,g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11,n,xbar,d1,d2,x,df,col, body={},mat,matS,matSRED,locs,url,text,headers, variables, table,table2,table3,h,header2,expr},

ToExpression@libLIST; (*these have to be here *)
ToExpression@libERR;
ToExpression@libDEBUG;
debugINIT["adA9xZyD"];

body={a0["id"]};


{canvas,text,vars,sol}=ReleaseHold[ToExpression[a0["q"],InputForm]];
sol1=(ToExpression@sol)[[1]];



err1=If[{(a0["x1"]),(a0["x2"]),(a0["x3"]),(a0["x4"]),(a0["x5"]),(a0["x6"])}=!=sol, 0, 1];
{headers, variables,locs,table,table2,table3,header2}=(ToExpression@vars)[[1]];



col={

Image[,ImageSize->125],
" ",

"CS 248, Homework 1B\nJanuary 2016",
"Prof. Madani",
" ",
"Student ID:"<>a0["id"],
" ",

ToExpression@text[[1]],
" ",
If[err1==1, "Correct",Style["Incorrect",Red]],"Your answers:",Row@{Subscript[a0["x1"],1],", ",Subscript[a0["x2"],2],", ",Subscript[a0["x3"],3],", ",Subscript[a0["x4"],4]},"Correct Answers:",Row@{sol1[[1]],", ",sol1[[2]],", ",sol1[[3]],", ",sol1[[4]]},
h=Rasterize/@(TraditionalForm/@(ToExpression@header2));

Grid[{TraditionalForm/@header2}~Join~table3,ItemSize->All,Frame->All],
\[SelectionPlaceholder],

{headers, variables,locs,table,table2,table3,header2}=(ToExpression@vars)[[2]];
sol2=(ToExpression@sol)[[2]];
ToExpression@text[[2]],
err2=If[(a0["x5"])==sol2[[1]],1,0];
If[err2==1, "Correct",Style["Incorrect",Red]],
"Your answers:",a0["x5"],
"Correct Answer:",sol2[[1]],
"Red T or F indicate where the POSSIBLE errors were placed:",
h=Rasterize/@(TraditionalForm/@(ToExpression@header2));
Grid[{TraditionalForm/@header2}~Join~table3,ItemSize->All,Frame->All],
\[SelectionPlaceholder],

{expr}=(ToExpression@vars)[[3]];
sol3=(ToExpression@sol)[[3]];
err3=If[(a0["x6"])==sol3[[1]],1,0];
ToExpression@text[[3]],
If[err3==1, "Correct",Style["Incorrect",Red]],
\[SelectionPlaceholder],
" ",


sol4=(ToExpression@sol)[[4]];
err4=If[(a0["x7"])==sol4[[1]],1,0];
ToExpression@text[[4]],
If[err4==1, "Correct",Style["Incorrect",Red]],
\[SelectionPlaceholder],
" "
 };

AppendTo[body,err1];
AppendTo[body,err2];
AppendTo[body,err3];
AppendTo[body,err4];


AppendTo[body,"HW1B"];
AppendTo[body,"CS248"];
AppendTo[body,"Madani"];
AppendTo[body,DateList[]];



url=CloudExport[Column@col,"PNG",Permissions->"Public"][[1]];

AppendTo[body,ToString@url];

resF=URLFetch["http://compclassnotes.com/bu/metcs248/api/ccnl/result","Parameters"->{"results"->(ToString@body)},"ReadTimeout"->10,"ConnectTimeout"->50,"Method"->"POST"];

DatabinAdd[Databin["adTU01Ey"],body];

 HTTPRedirect@url 
];


(* all err functions need to be called here since the Send is entirely happens in one unit *)
sendLOCAL[a0_]:=Module[{a,b,c,d,sol0,canvas0,err,err2,err3,err4,err5,err6,resF,canvas,vars,sol,sol1,sol2,sol3,sol4,sol5,sol6,μ,σ,g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11,n,xbar,d1,d2,x,df,col, body={}},

ToExpression@libLIST; (*these have to be here *)
ToExpression@libERR;
body={a0["id"]};

μ=0;σ=1;
{canvas,vars,sol}=ReleaseHold[ToExpression[a0["q"],InputForm]];
sol1=Sort[(ToExpression@sol)[[1]],Less];

err=If[Norm[{ToExpression@(a0["x1"]),ToExpression@(a0["x2"])}-sol1]<0.01, 1, 0];

g1=Plot[PDF[NormalDistribution[μ,σ],x],{x,-7,7},Filling->Axis,PlotLabel->"Normal Distribution\n"<>"μ="<>ToString[μ]<>", σ="<>ToString[σ], PlotRange->Full,AxesOrigin->{0,0}];
g2=Plot[PDF[NormalDistribution[μ,σ],x],{x,sol1[[1]],sol1[[2]]},Filling->Axis,PlotLabel->"Normal Distribution\n"<>"μ="<>ToString[μ]<>", σ="<>ToString[σ], PlotRange->Full,AxesOrigin->{0,0},FillingStyle->{Red}];



sol2=Sort[(ToExpression@sol)[[2]],Less];
err2=If[Norm[{ToExpression@(a0["x3"]),ToExpression@(a0["x4"])}-sol2]<0.01, 1, 0];

AppendTo[body, err];
AppendTo[body, err2];

col={

Image[,ImageSize->125],
" ",
"F16 QMS 202",
"Practice Questions for Lecture 2\nJanuary 2016",
Image[],
" ",
"1. "<>If[err==1, "Correct","Incorrect"],"Your answers:",Row@{a0["x1"],", ",a0["x2"]},"Solutions:",Row@{sol1[[1]],", ",sol1[[2]]},Show[g1,g2,ImageSize->400],

"2. "<>If[err2==1, "Correct", "Incorrect"],"Your answers:",Row@{a0["x3"],", ",a0["x4"]},"Solutions:",Row@{sol2[[1]],", ",sol2[[2]]},"Visualization:",Rasterize@"See the Red dot, that is ((OverBar[X]),(OverBar[X])), the yellowish region trapped between the two lines shows how spread the samples could be due to σ.",

{a,b,c,d}=vars[[2]];
σ=ToExpression@b;  (*ToExpression has to be here since b is String *)
n=ToExpression@c;
xbar=ToExpression@a;
{d1,d2}=sol[[2]];
g3=DensityPlot[PDF[NormalDistribution[μ,σ],x], {x,-xbar,2*xbar},{μ,0* d1/2,d2*1.5},PlotRange->Full,PlotPoints->40,PlotLegends->Automatic,FrameLabel->{"x","μ"},AxesLabel->{0,0}];

g7=Plot3D[PDF[NormalDistribution[μ,σ],x], {x,-xbar,2*xbar},{μ, 0* d1/2,d2*1.5},PlotRange->Full,AxesLabel->{"x","μ", "P"},PlotPoints->40, ImageSize->400];
g4=Plot [d1, {x,-xbar,2*xbar}];
g5=Plot [ d2, {x,-xbar,2*xbar}];
g6=Graphics[{Red,Point[{xbar,xbar}]}];

Show[g3,g4,g5,g6, ImageSize->400],
" ",
"3D visualization of a Normal distribution as μ varies: (x, μ, P(x)) ≈ N(μ, σ)",
g7,

" ",

sol3=(ToExpression@sol)[[3]];
err3=If[ToExpression@(a0["x5"])-(Abs[sol3[[1]]])<0.01, 1, 0];
{a,b}=ToExpression@vars[[3]];
df=b;
g8=Plot[PDF[StudentTDistribution[df],x],{x,-8,8},Filling->Axis,PlotRange->All];
g9=Plot[PDF[StudentTDistribution[df],x],{x,sol3[[2]],8},Filling->Axis,PlotStyle->Red, AxesOrigin->{0,0},PlotRange->All];
g10=Plot[PDF[StudentTDistribution[df],x],{x,-8,sol3[[1]]},Filling->Axis,PlotStyle->Red, AxesOrigin->{0,0},PlotRange->All];
"3. "<>If[err==1, "Correct","Incorrect"],
"Your answers:",a0["x5"],"Solutions:",Row@{"±",sol3[[1]]},
Show[g8,g9,g10, ImageSize->400],
" ",
(*sol4=(sol)[[4]],*) (* no ToExpression for text *)
sol4=(ToExpression@sol)[[4]];
"4. "<>If[MemberQ[sol4[[1]],a0["x6" ]]==True, err4=1;"Correct",err4=0;"Incorrect"],

" ",
sol5=(ToExpression@sol)[[5]];
err5=If[Abs[(ToExpression@(a0["x7"]))-sol5[[1]]]<0.01, 1, 0];
"5. "<>If[err5==1, "Correct","Incorrect"],
"Your answers:",a0["x7"],"Solutions:",Row@{sol5[[1]]},
" ",
sol6=(ToExpression@sol)[[6]];
err6=If[Abs[(ToExpression@(a0["x8"]))-sol6[[1]]]<1(*0.01*), 1, 0];
"6. "<>If[err6==1, "Correct","Incorrect"],
"Your answers:",a0["x8"],"Solutions:",Row@{sol6[[1]]},
" ",
" "

 };

AppendTo[body,err3];
AppendTo[body,err4];
AppendTo[body,err5];
AppendTo[body,err6];


local`body=Column@{body,col}
];





genFormLOCAL[]:=Module[{a,sol0,canvas0,err,resF,canvas,vars,sol,f,ans},

ToExpression@libLIST; (*these have to be here *)
ToExpression@libERR;
f[]:=(ToString[ReleaseHold[ToExpression[StringReplace[variation1,{"canvasZ"->canvas1,"fghjkl"->ToString[1]}]]],InputForm]); (* InputForm must be here or some problems occurs in ToExpression@variation *)
ans={
{"x1",""}-><|"Required"->True,"Interpreter"->"String","Hint" -> "", "Help" -> ""|>,
{"x2",""}-><|"Required"->True,"Interpreter"->"String","Hint" -> "", "Help" -> ""|>,
{"x3",""}-><|"Required"->True,"Interpreter"->"String","Hint" -> "", "Help" -> ""|>,
{"x4",""}-><|"Required"->True,"Interpreter"->"String","Hint" -> "",  "Help" -> ""|>,
{"x5",""}-><|"Required"->True,"Interpreter"->"String","Hint" -> "",  "Help" -> ""|>,
{"x6",""}-><|"Required"->True,"Interpreter"->"String","Hint" -> "",  "Help" -> ""|>,
{"x7",""}-><|"Required"->True,"Interpreter"->"String","Hint" -> "",  "Help" -> ""|>,
{"x8",""}-><|"Required"->True,"Interpreter"->"String","Hint" -> "",  "Help" -> ""|>,
{"x9",""}-><|"Required"->True,"Interpreter"->"String","Hint" -> "",  "Help" -> ""|>,
{"id",""}-><|"Interpreter"->"String","Help" -> "","Hint"->"Enter Student ID"|>,
"q"->"String"};

FormFunction[ans,sendLOCAL,  AppearanceRules-><|"SubmitLabel" -> "Send Answers"|>,

FormLayoutFunction->Function[


(* we need to use sol in send[] in order to see if the student input is correct or not?*)

{canvas,vars,sol}=ReleaseHold[ToExpression[Setting@#["q"][[1]][[1]][[2]][[1]]]];


ReleaseHold[ToExpression[canvas,InputForm]/.{"x1"->#["x1"],"x2"->#["x2"],"x3"->#["x3"],"x4"->#["x4"],"x5"->#["x5"],"x6"->#["x6"],"x7"->#["x7"],"x8"->#["x8"],"id"->#["id"]}]


], FormTheme -> "Blue"]["q"->f[]]






];

genForm[a0_]:=Module[{a,sol0,canvas0,err,resF,canvas,vars,sol,f,ans,text},

ToExpression@libLIST; (*these have to be here *)
ToExpression@libERR;
ToExpression@libDEBUG;
debugINIT["adA9xZyD"];

f[]:=(ToString[ReleaseHold[ToExpression[StringReplace[variation1,{"canvasZ"->canvas1,"fghjkl"->ToString[1]}]]],InputForm]); (* InputForm must be here or some problems occurs in ToExpression@variation *)
ans={
{"x1",""}-><|"Required"->True,"Interpreter"->"String","Hint" -> "T, F ?", "Help" ->""|>,
{"x2",""}-><|"Required"->True,"Interpreter"->"String","Hint" -> Style["T, F ?",Red], "Help" ->""|>,
{"x3",""}-><|"Required"->True,"Interpreter"->"String","Hint" -> Style["T, F ?",Red], "Help" ->""|>,
{"x4",""}-><|"Required"->True,"Interpreter"->"String","Hint" -> Style["T, F ?",Red],  "Help" ->""|>,
{"x5",""}-><|"Required"->True,"Interpreter"->"String","Hint" -> Style["T, F ?",Red],  "Help" ->""|>,
{"x6",""}-><|"Required"->True,"Interpreter"->"String","Hint" -> Style["T, F ?",Red],  "Help" ->""|>,
{"x7",""}-><|"Required"->True,"Interpreter"->"String","Hint" ->"True, False?",  "Help" ->""|>,

{"id",""}-><|"Interpreter"->"String","Help" -> "","Hint"->"Enter Student ID"|>,
"q"->"String"};

HTTPRedirect@CloudDeploy[FormFunction[ans,send,  AppearanceRules-><|"SubmitLabel" -> "Send Answers" |>,

FormLayoutFunction->Function[


(* we need to use sol in send[] in order to see if the student input is correct or not?*)

{canvas,text,vars,sol}=ReleaseHold[ToExpression[Setting@#["q"][[1]][[1]][[2]][[1]]]];

ReleaseHold[ToExpression[canvas,InputForm]/.{"x1"->#["x1"],"x2"->#["x2"],"x3"->#["x3"],"x4"->#["x4"],"x5"->#["x5"],"x6"->#["x6"],"x7"->#["x7"],"id"->#["id"]}]


], FormTheme -> "Blue"]["q"->f[]], 

Permissions->"Public"][[1]]







];

Deploy Area

DeleteFile[cccc];

ToExpression@libDEBUG;
debugINIT["adA9xZyD"];


cccc=CloudDeploy[FormFunction[{},
genForm,  AppearanceRules-><|"SubmitLabel" -> "Start"|>,

FormLayoutFunction->Function[


(* we need to use sol in send[] in order to see if the student input is correct or not?*)

Column[{Image[,ImageSize->125],
Spacer@100,
"CS 248, Homework 1B, Logic Statements,\nJanuary 2016",
"Prof. Madani",
Spacer@100,

"Press Start button to start the test"

}]


], FormTheme -> "White"], 

Permissions->"Public"]

LOCAL TEST AREA

The local form is slow, take a second to refresh the input fields. Also the Send Answer Button might not chave colour properly.

genFormLOCAL[]

local`body

DATA BIN  AREA

$Permissions = "Public"

Date of Creation

DateString[]

bin=CreateDatabin[<|"Name"->"CS248, HW1 B, Madani"|>]
Options[bin]

DEBUG

bin=CreateDatabin[<|"Name"->"CS248, HW1B, Madani",Permissions->"Public"|>]
Options[bin]