home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / permute < prev    next >
Text File  |  2020-01-01  |  1KB  |  46 lines

  1. #!/usr/local/bin/kermit +
  2. #
  3. # With pleasure I implemented the string permutation in C-Kermit attached
  4. # here.  What C-Kermit can do is limited only by one's own imagination.
  5. #
  6. # Dat Thuc Nguyen
  7. # 16 January 2004
  8. #
  9. # \%1 = String to be permuted
  10. # \%2 = Length of current segment
  11. #
  12. define permute {
  13.     (let n \flength(\%1) cnt 0)
  14.     local recur
  15.     define recur {
  16.         if == \%2 n {
  17.             (++ cnt)
  18.             echo \flpad(\m(cnt),5). \%1
  19.         } else {
  20.             local i
  21.             for i \%2 n 1 {
  22.                 if > i \%2 {
  23.                     asg \%1 \fsubstr(\%1,1,\%2-1)\fsubstr(\%1,i,1)-
  24. \fsubstr(\%1,\%2+1,i-\%2-1)\fsubstr(\%1,\%2,1)\fsubstr(\%1,i+1,\flen(\%1))
  25.                 }
  26.                 recur {\%1} \feval(\%2+1)
  27.             }
  28.         }
  29.     }
  30.     recur {\%1} 1
  31.     echo
  32. }
  33.  
  34. echo Testing...
  35.  
  36. permute a                               # One letter
  37. permute ab                              # Two letters
  38. permute abc                             # Three
  39. permute abcd                            # Four
  40.  
  41. echo To try it yourself type:
  42. echo
  43. echo {  permute "word(s) of your choice"}
  44. echo
  45. END
  46.