###################################################################### ##SBNYT.txt: Save this file as SBNYT.txt # ## To use it, stay in the # ##same directory, get into Maple (by typing: maple ) # ##and then type: read SBNYT.txt # ##Then follow the instructions given there # ## # ##Written by Doron Zeilberger, Rutgers University , # #zeilberg at math dot rutgers dot edu # ###################################################################### #Created: April 22, 2015 #This version: May 16, 2015 print(`First Created: April 22, 2015`): print(`This version: May 16, 2015. `): print(` This is SBSNYT.txt `): print(`A Maple package to solve (and make) Spelling Bee Puzzles, by Frank Longo that appear`): print(`in the New York Times magazine. For example, of April 19, 2015. `): print(`Written by Doron Zeilberger `): print(`It requires the data file, milim.txt`): print(`available from `): print(` http://www.math.rutgers.edu/~zeilberg/tokhniot/milim.txt .`): print(``): print(`Please report bugs to zeilberg at math dot rutgers dot edu`): print(``): print(`The most current version of this package and paper`): print(` are available from`): print(`http://www.math.rutgers.edu/~zeilberg/ .`): print(`---------------------------------------`): print(`For a list of the Supporting procedures type ezra1();, for help with`): print(`a specific procedure, type ezra(procedure_name); .`): print(``): print(`---------------------------------------`): print(`---------------------------------------`): print(`For a list of the MAIN procedures type ezra();, for help with`): print(`a specific procedure, type ezra(procedure_name); .`): print(``): print(`---------------------------------------`): with(combinat): ezra1:=proc() if args=NULL then print(` The supporting procedures are: PuzzleBook1, PuzzleBookNew1, RandPuzzle1, RandPuzzle3, RandPuzzle4 `): print(``): else ezra(args): fi: end: ezra:=proc() if args=NULL then print(`The main procedures are: DrawPuzzle, PSOLVE, PuzzleBook, , PuzleBook4, PuzzleBookNew1, RandPuzzle, SOLVE, SOLVEn `): print(` `): elif nops([args])=1 and op(1,[args])=DrawPuzzle then print(`DrawPuzzle(P): draws a puzzle nicely where P=[S,S1] and S is a set of seven letters and S1 is a subset of cardinality 1`): print(`Try:`): print(`DrawPuzzle([{a,b,c,d,e,f,g},{c}]);`): elif nops([args])=1 and op(1,[args])=PuzzleBook then print(`PuzzleBook(K1,K2,START,MIN,DAT1,DAT2,KAMA,HOWMANY): makes a list of puzzles,`): print(`followed by their list of solutions`): print(`Try:`): print(`PuzzleBook(7,1,5,20, ANGLITP,ANGLIT,100,10);`): elif nops([args])=1 and op(1,[args])=PuzzleBook1 then print(`PuzzleBook1(K1,K2,START,MIN,DAT1,DAT2,KAMA,HOWMANY): makes a list of puzzles,`): print(`followed by their list of solutions`): print(`Try:`): print(`PuzzleBook1(7,1,5,20, ANGLITP,ANGLIT,100,10);`): elif nops([args])=1 and op(1,[args])=PuzzleBookNew1 then print(`PuzzleBookNew1(K1,R,Kama,START,MIN,DAT1,DAT2,HowMany): makes a preliminary puzzle book`): print(`followed by their list of solutions`): print(`Try: `): print(`PuzzleBookNew1(7,5, 100, 5, 20, ANGLITP,ANGLIT,10); `): elif nops([args])=1 and op(1,[args])=PuzzleBook4 then print(`PuzzleBook4(K1,R,Kama,START,MIN,MAX,DAT1,DAT2,HOWMANY`): print(`A Collection of HOWMANY puzzles. Try:`) : print(`PuzzleBook4(7,5, 100, 5, 20, 40,ANGLITP,ANGLIT,3) ; `): elif nops([args])=1 and op(1,[args])=PSOLVEn then print(`PSOLVEn(S,DAT1,DAT2,START,S1): Like SOLVEn(S,DAT1,DAT2,START,S1) , but returns a pair of lists, the first`): print(`containing all the letters, the second all the other ones. `): print(`PSOLVEn(S,DAT1,DAT2,START,S1): solves a Spelling Bee style puzzle by Frank Longo that appears in the New York Times magazine`): print(`at least in April 2015. `): print(`The input is a set of letters , S, two huge data sets, DAT1, DAT2, that lists `): print(`and all i-letter starts and all the words in the language of a given length respectively`): print(` that for English are called, in the auxiliary data set, milim, ANGLITP and ANGLIT `): print(`of length at least START,, that contain the letters of the ses S1.`): print(`In the New York Times magazine the size of S is 7, and the size of S1 is 1, and S1 is a subset of S.`): print(`It also inputs a positive integer, START, and the output all words of length >=START using the `): print(`letters in S (possibly several times), and containing the subset S1. `): print(`To Solve the April 19, 2015 puzzle, type:`): print(`PSOLVEn({n,e,i,p,r,w,z},ANGLITP,ANGLIT,5,{n}); `): print(``): elif nops([args])=1 and op(1,[args])=RandPuzzle1 then print(`Terse version of RandPuzzle`): print(`RandPuzzle1(K1,K2,MIN,DAT1,DAT2,KAMA): Tries to makes a random puzzle with a set of letters of size K1, the subset that`): print(`must be included of size K2. It tries KAMA times. Try:`): print(`RandPuzzle1(7,1,5,20, ANGLITP,ANGLIT,100);`): elif nops([args])=1 and op(1,[args])=RandPuzzle3 then print(`RandPuzzle3(K1,R,Kama,START,MIN,DAT1,DAT2): makes a random puzzle with a set of letters of size K1, the subset that`): print(`must be included of size K2. It must have at least MIN solutions, and at least one of the solutions should use`): print(`all letters, and it must have K1+R letters. One of the letters must be such that all solutions contain it`): print(`Try:`): print(`RandPuzzle3(7,4,100,5,15, ANGLITP,ANGLIT);`): elif nops([args])=1 and op(1,[args])=RandPuzzle4 then print(`RandPuzzle4(K1,R,Kama,START,MIN,MAX,DAT1,DAT2): makes a random puzzle with a set of letters of size K1, the subset that`): print(`must be included of size K2. It must have at least MIN solutions, and at least one of the solutions should use`): print(`all letters, and it must have K1+R letters. One of the letters must be such that all solutions contain it`): print(`there should be at most MAX solutions`): print(`Try:`): print(`RandPuzzle4(7,5, 100, 5, 20, 40,ANGLITP,ANGLIT,40);`): elif nops([args])=1 and op(1,[args])=RandPuzzle then print(`RandPuzzle(K1,K2,MIN,DAT1,DAT2,KAMA): Tries to makes a random puzzle with a set of letters of size K1, the subset that`): print(`must be included of size K2. It tries KAMA times. Try:`): print(`RandPuzzle(7,1,5,20, ANGLITP,ANGLIT,100):`): elif nops([args])=1 and op(1,[args])=SOLVE then print(`Like PSOLVE(S,DAT1,DAT2,START,S1), but the output is in Maple format, as lists. Try:`): print(`SOLVE({n,e,i,p,r,w,z},ANGLITP,ANGLIT,5,{n});`): print(``): elif nops([args])=1 and op(1,[args])=SOLVEn then print(`Like SOLVE(S,DAT1,DAT2,START,S1), but the output consists of two lists. The first one using all`): print(`the letters of S, the second the remining ones. Try: `): print(`SOLVEn({n,e,i,p,r,w,z},ANGLITP,ANGLIT,5,{n});`): print(``): else print(`There is no ezra for`,args): fi: end: ALF:=[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]: #Spelling-Bee puzzle from New York Times Magazine ez:=proc(): print(` Vecs(n,k,K), hadpes(w), SB1([n,e,i,p,r,w,z],5,2,Anglit); SB([n,e,i,p,r,w,z],5,2,Anglit); BreakUp(S); ` ) : print(`PrefixList(S), Ptor1({a,c,d,e},DAT,3); `): print(`Ptor({a,c,d,e},ANGLITP,ANGLIT, 3);`): print(`SOLVE( {n,e,i,p,r,w,z},ANGLITP,ANGLIT,5,{n})`): print(`PSOLVE( {n,e,i,p,r,w,z},ANGLITP,ANGLIT,5,{n})`): end: read `milim.txt`: with(combinat): #Vecs(n,k,K): the number of vectors of non-negative integers in {0,..,K), of length k that add-up to n Vecs:=proc(n,k,K) local gu,n1, mu,mu1: option remember: if k=0 then if n=0 then RETURN({[]}): else RETURN({}): fi: fi: gu:={}: for n1 from 0 to min(n,K) do mu:=Vecs(n-n1,k-1,K): gu:=gu union {seq([op(mu1),n1],mu1 in mu)}: od: gu: end: #SB1(L,k,S): inputs a list of letters L, a positive integer k #SB1(L,k,S) #end: #prints the word w hadpes:=proc(w) if nops(w)=1 then sprintf("%-1.1s",op(w)); elif nops(w)=2 then fsprintf("%-1.1s%-1.1s",op(w)); elif nops(w)=3 then sprintf("%-1.1s%-1.1s%-1.1s",op(w)); elif nops(w)=4 then sprintf("%-1.1s%-1.1s%-1.1s%-1.1s",op(w)); elif nops(w)=5 then sprintf("%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s",op(w)); elif nops(w)=6 then sprintf("%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s",op(w)); elif nops(w)=7 then sprintf("%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s",op(w)); elif nops(w)=8 then sprintf("%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s",op(w)); elif nops(w)=9 then sprintf("%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s",op(w)); elif nops(w)=10 then sprintf("%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s",op(w)); elif nops(w)=11 then sprintf("%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s",op(w)); elif nops(w)=12 then sprintf("%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s",op(w)); elif nops(w)=13 then sprintf("%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s",op(w)); elif nops(w)=14 then sprintf("%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s",op(w)); elif nops(w)=15 then sprintf("%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s%-1.1s",op(w)); else print(w): fi: end: #SB1(L,n,K,S): inputs a list of letters L, a positive integer n, a positive integer K #and a list of words S #outputs all the n-letter words with letters drawn from L using the first letter at #least once, and where every letter shows up at most K times. Try #SB1([n,e,i,p,r,w,z],5,2,Anglit); SB1:=proc(L,n,K,S) local mu,gu,mu1,lu,i1: mu:=Vecs(n-1,nops(L),K): mu:={seq([mu1[1]+1,op(2..nops(mu1),mu1)],mu1 in mu)}: gu:={}: for mu1 in mu do lu:=permute([seq(L[i1]$mu1[i1],i1=1..nops(L))]): gu:=gu union convert(lu,set) intersect(convert(S,set)): od: gu: end: #SB(L,n,K,S): inputs a list of letters L, a positive integer n, a positive integer K #and a list of words S #outputs the pair (list of length 2) #whose first entry is the set all the words with at least n letters, with letters drawn from L using the first letter at #least once, and where every letter shows up at most K times. The second entry is the set of words of #nops(L) letters where every letter shows up #Try #SB([n,e,i,p,r,w,z],5,2,Anglit); SB:=proc(L,n,K,S) local n1,gu1,gu2: gu1:={seq(op(SB1(L,n1,K,S)),n1=n..nops(L))}: gu2:=convert(S,set) intersect convert(permute(L),set): [gu1,gu2]: end: #BreakUp(S): Inputs a list S and outputs a list of lists, let's call it L, such that #L[i] is the list of words of length i, try: #BreakUp([[1],[2,5],[3]]): BreakUp:=proc(S) local i1,MA,T: MA:=max(seq(nops(S[i1]),i1=1..nops(S))): for i1 from 1 to MA do T[i1]:={}: od: for i1 from 1 to nops(S) do T[nops(S[i1])]:=[op(T[nops(S[i1])]),S[i1]]: od: [seq(T[i1],i1=1..MA)]: end: #PrefixList(S): inputs a set S, and outputs a list of sets whose i-th item are all the length-i prefixes of the words in S PrefixList:=proc(S) local T,MA,NISH,W,i1: MA:=max(seq(nops(S[i1]),i1=1..nops(S))): for i1 from 1 to MA do T[i1]:={}: od: NISH:=S: for i1 from 1 to MA do T[i1]:={seq([op(1..i1,W)],W in NISH)}: NISH:=NISH minus T[i1] intersect S: od: [seq(T[i1],i1=1..MA)]: end: #Ptor1(S,DAT,K): all the words in the alphabet S with k letters that are beginnings of English words with k letters #Try: #Ptor1({a,c,d,e},DAT,3); Ptor1:=proc(S,DAT,K) local GU,W,S1: option remember: if not (K>=0 and K<=nops(DAT)) then RETURN(FAIL): fi: if K=0 then RETURN({[]}): fi: GU:=Ptor1(S,DAT, K-1): {seq(seq([op(W),S1],S1 in S),W in GU)} intersect DAT[K]: end: #Ptor(S,DAT1,DAT2,K): #all the words in the alphabet S with k letters that are English words #Try: #Ptor({a,c,d,e},ANGLITP,ANGLIT, 3); Ptor:=proc(S,DAT1,DAT2,K) Ptor1(S,DAT1,K) intersect convert(DAT2[K],set): end: #SOLVE(S,DAT1,DAT2,START,S1): solves a spelling bee puzzle, to make words using the data sets DAT1, DAT2 #of length at least START,, that contain the letters of S1. #Try: #SOLVE({n,e,i,p,r,w,z},ANGLITP,ANGLIT,5,{n}) SOLVE:=proc(S,DAT1,DAT2,START,S1) local GU1,GU,K: GU1:=[]: for K from START while Ptor1(S,DAT1,K)<>{} do GU1:=[op(GU1),op(Ptor(S,DAT1,DAT2,K))]: od: GU:=[]: for K from 1 to nops(GU1) do if S1 minus convert(GU1[K],set) ={} then GU:=[op(GU),GU1[K]]: fi: od: GU: end: #PSOLVEold(S,DAT1,DAT2,START,S1): prints the solution of a Spelling Bee NYT puzzle, to make words using the data sets DAT1, DAT2 #of length at least START,, that contain the letters of S1. #Try: #PSOLVEold({n,e,i,p,r,w,z},ANGLITP,ANGLIT,5,{n}) PSOLVEold:=proc(S,DAT1,DAT2,START,S1) local GU,A: GU:=SOLVE(S,DAT1,DAT2,START,S1): for A from 1 to nops(GU) do print(hadpes(GU[A])); od: end: #PSOLVE(S,DAT1,DAT2,START,S1): prints the solution of a Spelling Bee NYT puzzle, to make words using the data sets DAT1, DAT2 #of length at least START,, that contain the letters of S1. #Try: #PSOLVE({n,e,i,p,r,w,z},ANGLITP,ANGLIT,5,{n}) PSOLVE:=proc(S,DAT1,DAT2,START,S1) local GU,A: GU:=SOLVE(S,DAT1,DAT2,START,S1): sort([seq(hadpes(GU[A]),A=1..nops(GU))],lexorder): end: #PSOLVEn(S,DAT1,DAT2,START,S1): prints the solution of a Spelling Bee NYT puzzle, to make words using the data sets DAT1, DAT2 #of length at least START,, that contain the letters of S1. #Try: #PSOLVEn({n,e,i,p,r,w,z},ANGLITP,ANGLIT,5,{n}) PSOLVEn:=proc(S,DAT1,DAT2,START,S1) local GU,A: GU:=SOLVEn(S,DAT1,DAT2,START,S1): [sort([seq(hadpes(GU[1][A]),A=1..nops(GU[1]))],lexorder),sort([seq(hadpes(GU[2][A]),A=1..nops(GU[2]))],lexorder)]: end: #RandPuzzle11(K1,K2,START,MIN,DAT1,DAT2): makes a random puzzle with a set of letters of size K1, the subset that #must be included of size K2. It must have at least MIN solutions. Try: #RandPuzzle11(7,1,5,20, ANGLITP,ANGLIT); RandPuzzle11:=proc(K1,K2,START,MIN,DAT1,DAT2) local A,ALF,S,B,NISHAR,S1: ALF:=[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]: S:={}: for A from 1 to K1 do B:=rand(1..nops(ALF))(): S:=S union {ALF[B]}: ALF:=[op(1..B-1,ALF),op(B+1..nops(ALF),ALF)]: od: NISHAR:=S: S1:={}: for A from 1 to K2 do B:=rand(1..nops(NISHAR))(): S1:=S1 union {NISHAR[B]}: NISHAR:=NISHAR minus {NISHAR[B]}: od: if nops(SOLVE(S,DAT1,DAT2,START,S1))FAIL then RETURN(gu): fi: od: end: #RandPuzzle2(K1,K2,START,MIN,DAT1,DAT2,KAMA): makes a random puzzle with a set of letters of size K1, the subset that #must be included of size K2. It must have at least MIN solutions. It tries up to KAMA times #Try: #RandPuzzle2(7,1,5,20, ANGLITP,ANGLIT,100); RandPuzzle2:=proc(K1,K2,START,MIN,DAT1,DAT2,KAMA) local A,gu: for A from 1 to KAMA do gu:=RandPuzzle21(K1,K2,START,MIN,DAT1,DAT2): if gu<>FAIL then RETURN(gu): fi: od: end: #RandPuzzle(K1,K2,START,MIN,DAT1,DAT2,KAMA): makes a random puzzle with a set of letters of size K1, the subset that #must be included of size K2. It must have at least MIN solutions. It tries up to KAMA times #Try: #RandPuzzle(7,1,5,20, ANGLITP,ANGLIT,100); RandPuzzle:=proc(K1,K2,START,MIN,DAT1,DAT2,KAMA) local GU,S,S1: GU:=RandPuzzle1(K1,K2,START,MIN,DAT1,DAT2,KAMA): if GU=FAIL then RETURN(FAIL): fi: S:=GU[1]: S1:=GU[2]: print(`How many words of length at least`, START, `can you make by using the letters`): print(S): if nops(S1)>1 then print(`where you must use the letters of`, S1): elif nops(S1)=1 then print(`where you must use, at least once, the letter`, S1[1]): fi: print(`letters may be used several times. `): [S,S1]: end: #PuzzleBook1(K1,K2,START,MIN,DAT1,DAT2,KAMA,HOWMANY): makes a list of puzzles, #followed by their list of solutions #Try: #PuzzleBook1(7,1,5,20, ANGLITP,ANGLIT,100,10); PuzzleBook1:=proc(K1,K2,START,MIN,DAT1,DAT2,KAMA,HOWMANY) local A,P,GU1,GU2: GU1:=[]: GU2:=[]: for A from 1 to HOWMANY do P:=RandPuzzle2(K1,K2,START,MIN,DAT1,DAT2,KAMA): GU1:=[op(GU1),P[1]]: GU2:=[op(GU2),P[2]]: od: [GU1,GU2]: end: #PuzzleBook(K1,K2,START,MIN,DAT1,DAT2,KAMA,HOWMANY): makes a list of puzzles, #followed by their list of solutions #Try: #PuzzleBook(7,1,5,20, ANGLITP,ANGLIT,100,10); PuzzleBook:=proc(K1,K2,START,MIN,DAT1,DAT2,KAMA,HOWMANY) local GU,GU1,GU2,A: GU:=PuzzleBook1(K1,K2,START,MIN,DAT1,DAT2,KAMA,HOWMANY): GU1:=GU[1]: GU2:=GU[2]: print(HOWMANY, `Challenging Spelling Bee Puzzles `): print(``): print(`By Shalosh B. Ekhad `): print(``): print(`In the following puzzles, you are given a set of letters, together with a subset`): print(`and you have to make as many common words, of length at least`, START, `that must include the indicated letter(s)`): print(`You may use letters several times.`): print(``): for A from 1 to HOWMANY do print(`Puzzle Number`, A): print(``): print(`LETTERS:`, GU1[A][1], `MUST USE: `, GU1[A][2] ): print(``): od: print(``): print(``): print(``): print(``): print(``): print(`-----------------------------------------------------------------------------------`): print(``): print(`SOLUTIONS `): for A from 1 to HOWMANY do print(`Solution to Puzzle Number`, A): print(``): print(GU2[A]): print(``): od: end: #RandPuzzle3(K1,R,Kama,START,MIN,DAT1,DAT2): makes a random puzzle with a set of letters of size K1, the subset that #must be included of size K2. It must have at least MIN solutions, and at least one of the solutions should use #all letters, and it must have K1+R letters. One of the letters must be such that all solutions contain it #Try: #RandPuzzle3(7,5, 100, 5, 20, ANGLITP,ANGLIT); RandPuzzle3:=proc(K1,R,Kama,START,MIN,DAT1,DAT2) local i1,w1,r1,MIL,S,S1,KU: if K1+R>nops(DAT2) then RETURN(FAIL): fi: MIL:=DAT2[K1+R]; for i1 from 1 to min(nops(MIL)/2,Kama) do r1:=rand(1..nops(MIL))(): w1:=MIL[r1]: S:={op(w1)}: if nops(S)=K1 then S1:={S[rand(1..K1)()]}: KU:=SOLVE(S,DAT1,DAT2,START,S1): if nops(KU)>=MIN then RETURN([[S,S1] , PSOLVE(S,DAT1,DAT2,START,S1)]): fi: fi: od: FAIL: end: #PuzzleBookNew1(K1,R,Kama,START,MIN,DAT1,DAT2,HowMany): makes a preliminary puzzle book #followed by their list of solutions #Try: #PuzzleBookNew1(7,5, 100, 5, 20, ANGLITP,ANGLIT,10); PuzzleBookNew1:=proc(K1,K2,START,MIN,DAT1,DAT2,KAMA,HOWMANY) local A,P,GU1,GU2: GU1:=[]: GU2:=[]: for A from 1 to HOWMANY do P:=RandPuzzle3(K1,K2,START,MIN,DAT1,DAT2,KAMA): if not member(P[1],convert(GU1,set)) then GU1:=[op(GU1),P[1]]: GU2:=[op(GU2),P[2]]: fi: od: [GU1,GU2]: end: with(plots): ToCap:=proc(OT): if OT=a then A: elif OT=b then B: elif OT=c then C: elif OT=d then D: elif OT=e then E: elif OT=f then F: elif OT=g then G: elif OT=h then H: elif OT=i then I: elif OT=j then J: elif OT=k then K: elif OT=l then L: elif OT=m then M: elif OT=n then N: elif OT=o then O: elif OT=p then P: elif OT=q then Q: elif OT=r then R: elif OT=s then S: elif OT=t then T: elif OT=u then U: elif OT=v then V: elif OT=w then W: elif OT=x then X: elif OT=y then Y: elif OT=z then Z: else FAIL: fi: end: #DrawPt(A,NA): Given a point A, and a name NA #puts the label NA at A DrawPt:=proc(A,NA) local d: textplot([A[1],A[2],NA],axes=none,numpoints=3000):end: #DrawPuzzle(P): draws a puzzle nicely where P=[S,S1] and S is a set of seven letters and S1 is a subset of cardinality 1 #Try: #DrawPuzzle([{a,b,c,d,e,f,g},{c}]); DrawPuzzle:=proc(P) local S, Merkaz,gu,i1,X,Y,Z,K: S:=P[1] minus P[2]: S:=sort(convert(S,list)): K:=nops(S): Merkaz:=P[2][1]: gu:=DrawPt([0,0],ToCap(Merkaz)): gu:=gu,implicitplot(x^2+y^2-20^2,x=-20..20,y=-20..20,scaling=constrained): for i1 from 0 to K-1 do X[i1]:=evalf([10*cos(2*Pi*i1/K),10*sin(2*Pi*i1/K)]): Y[i1]:=evalf([20*cos(2*Pi*i1/K),20*sin(2*Pi*i1/K)]): Z[i1]:=evalf([15*cos(2*Pi*(i1+1/2)/K),15*sin(2*Pi*(i1+1/2)/K)]): od: for i1 from 0 to K-1 do gu:=gu,plot([X[i1],Y[i1]]): od: for i1 from 0 to K-2 do gu:=gu,plot([X[i1],X[i1+1]]): od: gu:=gu,plot([X[K-1],X[0]]): for i1 from 0 to K-1 do gu:=gu,DrawPt(Z[i1],ToCap(S[i1+1])) : od: display(gu): end: #SOLVEn(S,DAT1,DAT2,START,S1): solves a spelling bee puzzle, to make words using the data sets DAT1, DAT2 #of length at least START,, that contain the letters of S1. #Returns, two lists, the first one thos that use all the letters, the second one the remaining ones #Try: #SOLVEn({n,e,i,p,r,w,z},ANGLITP,ANGLIT,5,{n}) SOLVEn:=proc(S,DAT1,DAT2,START,S1) local GU1,GUA,GUB,K: GU1:=[]: for K from START while Ptor1(S,DAT1,K)<>{} do GU1:=[op(GU1),op(Ptor(S,DAT1,DAT2,K))]: od: GUA:=[]: GUB:=[]: for K from 1 to nops(GU1) do if S1 minus convert(GU1[K],set) ={} then if convert(GU1[K],set)=S then GUA:=[op(GUA),GU1[K]]: else GUB:=[op(GUB),GU1[K]]: fi: fi: od: [GUA,GUB]: end: #RandPuzzle4(K1,R,Kama,START,MIN,MAX,DAT1,DAT2): makes a random puzzle with a set of letters of size K1, the subset that #must be included of size K2. It must have at least MIN solutions, and at least one of the solutions should use #all letters, and it must have K1+R letters. One of the letters must be such that all solutions contain it #there should be at most MAX solutions #Try: #RandPuzzle4(7,5, 100, 5, 20, 40,ANGLITP,ANGLIT,40); RandPuzzle4:=proc(K1,R,Kama,START,MIN,MAX,DAT1,DAT2) local i1,w1,r1,MIL,S,S1,KU: if K1+R>nops(DAT2) then RETURN(FAIL): fi: MIL:=DAT2[K1+R]; for i1 from 1 to min(nops(MIL)/2,Kama) do r1:=rand(1..nops(MIL))(): w1:=MIL[r1]: S:={op(w1)}: if nops(S)=K1 then S1:={S[rand(1..K1)()]}: KU:=SOLVEn(S,DAT1,DAT2,START,S1): if nops(KU[2])>=MIN and nops(KU[2])<=MAX then RETURN([[S,S1] , PSOLVEn(S,DAT1,DAT2,START,S1)]): fi: fi: od: FAIL: end: #Try: #PuzzleBook4(7,5, 100, 5, 20, 40,ANGLITP,ANGLIT,3); PuzzleBook4:=proc(K1,R,Kama,START,MIN,MAX,DAT1,DAT2,HOWMANY) local GU,lu:: lu:=RandPuzzle4(K1,R,Kama,START,MIN,MAX,DAT1,DAT2): GU:={}: while nops(GU)<=HOWMANY do lu:=RandPuzzle4(K1,R,Kama,START,MIN,MAX,DAT1,DAT2): if lu<>FAIL then GU:= GU union {lu}: fi: od: GU: end: