(* Require XMLgen_MC.nb ver >=0.20 *)
SetDirectory[NotebookDirectory[]];
<<XMLgenMC.mx;
DebugMode=False;
ThisNo=StringReplace[StringSplit[ NotebookFileName[],"/"][[-1]],{"LA_"->"",".nb"->"","_"->"-"}];
Category="LA/"<>ThisNo<>".";

(* Global Options *)
Numbering="abc";Single="false";Shuffle="true";
(* Numbers of select options and questions *)
SELNUM=6;NUM=200;Title="階段行列の定義";

TTS[mt_]:=ToString[TeXForm[StandardForm[mt]]];
TOT[mt_]:=ToString[OneLineTeXForm[mt]];
TOTT[mt_]:=ToString[OneLineTeXForm[Transpose[mt]]];
TOC[mt_,c_]:=TOTT[{Transpose[mt][[c]]}];
TTD[r_]:=StringReplace[TT[r],"\\frac"->"\\dfrac"];
Rnz[m_]:=(RandomInteger[m]+1)*(2*RandomInteger[1]-1); (* \\pm 1,\pm2,...,\pm(m+1) *)
Rpos[m_]:=RandomInteger[m]+1; (* 1,2,...,m+1 *)
Rbtw[m_]:=RandomInteger[2m]-m;
TTC[A_,n_]:=TOTT[{(Transpose[A])[[n]]}];
Col[A_,n_]:=Transpose[{A[[Range[Length[A]],n]]}];
ColCat[CL_]:=Join@@@(Transpose@CL);
RowCat[CL_]:=Join@@(CL);
PtString[M_]:=StringReplace[ToString[M],{"{"->"(","}"->")"}];
ai2tex[s_]:=StringReplace[TOT[s],Table["\\text{a"<>TOT[i]<>"}"->"{\\mathbf a}_"<>TOT[i],{i,1,5}]];
TpStr[L_]:="\\("<>TOT[L[[1]]]<>"\\times"<>TOT[L[[2]]]<>"\\)";
TpTpse[L_]:=If[L=={},{},{L[[2]],L[[1]]}];
PType[A_,B_]:=If[A=={}||B=={},Return[{}],
If[A[[2]]===B[[1]],Return[{A[[1]],B[[2]]}],Return[{}]]];
PTypeL[L_]:=Module[{ct,len,tmp},len=Length[L];
Which[len==0,Return[{}],len==1,Return[L[[1]]],
len==2,Return[PType[L[[1]],L[[2]]]],
True,tmp=L[[1]];For[ct=2,ct≤len,ct++,tmp=PType[tmp,L[[ct]]]];Return[tmp]]];

(* select pairs of matrices *)
QtList={}; For[ct=1,ct≤300,ct++,
Cn=RandomChoice[{2,2,3,3,3,3,4,4,4,5}];
row=RandomChoice[{3,4,5}];col=RandomChoice[{4,5,5,6}];
QtList=Append[QtList,{Cn,{row,col}}]];
(* QtList=DeleteDuplicates[QtList]; *)
QtList=RandomSample[QtList,NUM];Length[QtList]

Clear[QtextF];QtextF[mm_]:=Module[{Cn,row,col,MA},
{Cn,{row,col}}=QtList[[mm]];
Return["次の行列のうち，階段選びなさい."]];

CListF[mm_]:=Module[{Cn,row,col,rk,rkm,MA,i,j,tL={},js,ct,fs,ret={}},
{Cn,{row,col}}=QtList[[mm]];fs="正解です.";rkm=Min[row,col];
While[Length[tL]≤Cn,
rk=RandomInteger[rkm-1]+1;
If[Length[tL]<2 && col>rk,
js=Sort[RandomSample[Range[2,col],rk]],
js=Sort[RandomSample[Range[col],rk]]];
tL=Append[tL,js];tL=DeleteDuplicates[tL]];
For[ct=1,ct≤Length[tL],ct++,
js=tL[[ct]];rk=Length[js];
MA=Table[Table[Rnz[3],col],row];
For[i=1,i≤rk,i++,
If[js[[i]]>1,For[j=1,j<js[[i]],j++,MA[[i,j]]=0]]];
If[rk<row,For[i=rk+1,i≤row,i++,For[j=1,j≤col,j++,MA[[i,j]]=0]]];
(* Print[js]; *)
(* Print["Rank="<>TOT[rk]];Print[MatrixForm[MA]]; *)
ret=Append[ret,{"\\("<>TOT[MA]<>"\\)",fs}]];
Return[RandomSample[ret,Cn]]];

CListF[3]

ICListF[mm_]:=Module[{Cn,row,col,rk,rkm,MA,MA1,i,j,tL={},js,ct,fs,fs1,fs2,fs3,fsL={},ret={}},
{Cn,{row,col}}=QtList[[mm]];rkm=Min[row,col];
fs1="階段の段の「高さ」は2段以上であってはいけません.";
fs2="階段の「段」は上から順に右側に進んでいかなければなりません.";
fs3=fs1<>fs2;
While[Length[tL]<SELNUM,fs="";
js=RandomChoice[Range[0,col],row];
If[js≠Sort[js],fs=fs<>fs2];
For[ct=1,ct<row,ct++,If[js[[ct]]==js[[ct+1]],fs=fs<>fs1]];
If[fs≠"",tL=Append[tL,{js,fs}]];
tL=DeleteDuplicates[tL]];
(* Return[tL]]; *)
 For[ct=1,ct≤Length[tL],ct++,
js=tL[[ct,1]];fs=tL[[ct,2]];
MA=Table[Table[Rnz[3],col],row];
For[i=1,i≤row,i++,
If[js[[i]]>0,For[j=1,j<=js[[i]],j++,MA[[i,j]]=0]]];
 ret=Append[ret,{"\\("<>TOT[MA]<>"\\)",fs}]
(* ret=Append[ret,{MatrixForm[MA],fs}]; *)
];
Return[RandomSample[ret,SELNUM-Cn]]];

QtList[[5]]

ICListF[10]

For[ct=1,ct≤NUM,ct++,QtextF[ct];CListF[ct]] (* Error check *)

GenerateMCxml[DFname[],Category<>Title<>"(MC1)",Title,NUM,Single,QtextF,CListF,ICListF]

ICListF[3]

#->Nothing &/@ Permutations[{1,2,3},{2}]

MA={{3,5},{7,9}}

MA1=MA;MA1[[{1,2},1]]={{1},{0}};

MA1

RowCat[{IdentityMatrix[2],MA}]

(* failed code *)
While[Length[tL]<2,
rk=RandomInteger[rkm-1]+1;
js=Sort[RandomChoice[Range[col],rk]];
If[rk>1,While[js==DeleteDuplicates[js],js=Sort[RandomChoice[Range[col],rk]]]];
tL=Append[tL,{js,fs1}];tL=DeleteDuplicates[tL]];
While[Length[tL]<4,
rk=RandomInteger[rkm-1]+1;
js=RandomSample[Range[col],rk];
If[rk>1,While[js==Sort[js],js=RandomSample[Range[col],rk]]];
tL=Append[tL,{js,fs2}];tL=DeleteDuplicates[tL]];
While[Length[tL]<6,
rk=RandomInteger[rkm-1]+1;
js=RandomChoice[Range[col],rk];
If[rk>1,While[js==Sort[DeleteDuplicates[js]],
js=RandomChoice[Range[col],rk]]];
tL=Append[tL,{js,fs3}];tL=DeleteDuplicates[tL]]; *)