/* put the words to unscramble here */ data target; length target $6.; input target; datalines; NAGER RRREO ORRCDE LROANM ; /* create data sets of all permutations of 5 and 6 elements */ /** generate all permutations of n elements, in order **/ data perm5(drop=i); array a{5}; do i = 1 to 5; a[i]=i; end; /** initialize **/ do i = 1 to fact(5); call allperm(i, of a[*]); output; end; run; data perm6(drop=i); array a{6}; do i = 1 to 6; a[i]=i; end; /** initialize **/ do i = 1 to fact(6); call allperm(i, of a[*]); output; end; run; /* create data set from word list */ /* Download a wordlist such as OSPD from http://www.puzzlers.org/dokuwiki/doku.php?id=solving:wordlists:about:start*/ filename wordlist 'C:\Documents and Settings\frwick\My Documents\My Downloads\wordlist.txt'; data Dictionary; /** keep first 8 chars unless you need longer words **/ length word $8.; infile wordlist; input word $; run; proc iml; /** apply a given permutation to a string of characters **/ start PermuteChars( w, perm ); n = nleng(w); s = j(1, n, " "); /** allocate character array **/ do i = 1 to n; s[i] = substr(w, perm[i], 1); end; return(rowcatc(s)); finish; /** read word list; convert to uppercase **/ use Dictionary; read all var {Word}; close Dictionary; Word = upcase(Word); /** read permutations for 5- an 6-letter words **/ use perm5; read all var _NUM_ into p5; close perm5; use perm6; read all var _NUM_ into p6; close perm6; use target; read all var _CHAR_ into target; close target; free solution; do i = 1 to nrow(target); /** for each word **/ w = strip(target[i]); if length(w)=5 then p = p5; else p = p6; /** use permutation for N elements **/ idx = loc( length(Word) = length(w) ); list = Word[idx]; /** limit word list **/ foundWord = 0; /** how many permutations result in a word? **/ do n = 1 to nrow(p) while (foundWord<5); /** for each permutation **/ perm = p[n, ]; s = PermuteChars(w, perm); /** apply permutation **/ idx = loc(list = s); /** check if this is a word **/ if ncol(idx)>0 then do; /** remember solution **/ foundWord = foundWord + 1; solution = solution // (s + " is a solution for " + w); end; end; end; print solution;