home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1960 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  1.4 KB

  1. From: scotta@hpcuhd.HP.COM (Scott Anderson)
  2. Newsgroups: alt.sources
  3. Subject: Re: Permutation generator
  4. Message-ID: <108380001@hpcuhd.HP.COM>
  5. Date: 15 Oct 90 18:55:49 GMT
  6.  
  7.  
  8.     Here's one I did about 2 years ago for solving jumbles (in a pretty
  9. gross way - see leading comment in program).  It doesn't use malloc and
  10. uses pointers instead of indices.  The only minor disadvantage is that
  11. dupes aren't eliminated.  I find it somewhat amusing how similar the
  12. programs are though.
  13.  
  14.     Scott Anderson
  15.     An RTEsian and proud of it...        Hewlett-Packard
  16.                         Data Systems Operation
  17.     scotta@cup.hp.com                11000 Wolfe Rd.  MS 42UN
  18.     408-447-5219                Cupertino, CA  95014
  19. ________________________________________________________________________
  20.  
  21. /* This program produces permutations of all of the strings passed to it.
  22.  * To solve jumbles try:
  23.  *    perm xxxxx | sort -u | tee temp | spell | sort | comm -13 - temp
  24.  * where xxxxx is the scrambled word.
  25.  */
  26.  
  27. #define SWAP(a, b)    ch = *(a); *(a) = *(b); *(b) = ch;
  28.  
  29. void perm(whole, partial)
  30.     char *whole, *partial;
  31. {
  32.     register char *ptr, *next_partial, ch;
  33.  
  34.     if (!*partial) {
  35.     puts(whole);
  36.     return;
  37.     }
  38.  
  39.     ptr = partial;
  40.     next_partial = partial + 1;
  41.     while (*ptr) {
  42.     SWAP(partial, ptr);
  43.     perm(whole, next_partial);
  44.     SWAP(partial, ptr);
  45.     ptr++;
  46.     }
  47. }
  48.  
  49. main (argc, argv)
  50.     int argc;
  51.     char **argv;
  52. {
  53.     int i;
  54.  
  55.     for (i=1; i<argc; i++)
  56.     perm(argv[i], argv[i]);
  57. }
  58.