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

  1. From: maart@cs.vu.nl (Maarten Litmaath)
  2. Newsgroups: rec.puzzles,alt.sources
  3. Subject: Re: Balls
  4. Message-ID: <7302@star.cs.vu.nl>
  5. Date: 13 Aug 90 23:55:39 GMT
  6.  
  7. In article <1990Aug8.141722.26196@specialix.co.uk>,
  8.     jonb@specialix.co.uk (Jon Brawn) writes:
  9. )[Urm.... I do hope I don't need to be anyone special to post these things...]
  10.  
  11. You only gotta have BALLS!  :-)
  12.  
  13. )Imagine, if you will, that you posses four balls, (0, 1, 2, and 3)
  14. )Now, you can arrange these four balls in 24 permutations:
  15. )
  16. )        0 1 2 3,     1 0 2 3,    2 0 1 3,    3 0 1 2,
  17. )        0 1 3 2,     1 0 3 2,    2 0 3 1,    3 0 2 1,
  18. )        0 2 1 3,     1 2 0 3,    2 1 0 3,    3 1 0 2,
  19. )        0 2 3 1,     1 2 3 0,    2 1 3 0,    3 1 2 0,
  20. )        0 3 1 2,     1 3 0 2,    2 3 0 1,    3 2 0 1,
  21. )        0 3 2 1,     1 3 2 0,    2 3 1 0,    3 2 1 0.
  22. )
  23. )It is desired to have a function that yields an integer (0-23)
  24. )that uniquely identifies each of the above sequences. [...]
  25.  
  26. Included are 2 C functions `perm' and `perm_number', each accompanied by
  27. a small main program.  Compile them like this:
  28.  
  29.     cc -o perm perm.c
  30.     cc -o perm_number perm_number.c
  31.  
  32. Example session:
  33.  
  34.     $ perm 16 0123
  35.     2301
  36.     $ perm 479001599 ab,de+g=ij.l
  37.     l.ji=g+ed,ba
  38.     $ perm_number lkjihgfedcba abcdefghijkl
  39.     479001599
  40.  
  41. That's right!
  42. --
  43. : This is a shar archive.  Extract with sh, not csh.
  44. : This archive ends with exit, so do not worry about trailing junk.
  45. : --------------------------- cut here --------------------------
  46. PATH=/bin:/usr/bin:/usr/ucb
  47. echo Extracting 'perm.c'
  48. sed 's/^X//' > 'perm.c' << '+ END-OF-FILE ''perm.c'
  49. Xint    perm(n, nullperm, length, dest)
  50. Xlong    n;
  51. Xchar    *nullperm, *dest;
  52. Xint    length;
  53. X{
  54. X    long    f;
  55. X    int    d;
  56. X    char    *p, *q, *r, *malloc();
  57. X
  58. X    if (!(p = malloc(length)))
  59. X        return -1;
  60. X
  61. X    strncpy(p, nullperm, length);
  62. X
  63. X    for (d = --length, f = 1; d > 0; d--)
  64. X        f *= d;
  65. X
  66. X    while (length > 0) {
  67. X        d = n / f;
  68. X        *dest++ = p[d];
  69. X        if (!d)
  70. X            p++;
  71. X        else
  72. X            for (q = p + d, r = q + 1; *q++ = *r++; )
  73. X                ;
  74. X        n -= f * d;
  75. X        f /= length--;
  76. X    }
  77. X
  78. X    *dest++ = *p;
  79. X    *dest = '\0';
  80. X
  81. X    free(p);
  82. X
  83. X    return 0;
  84. X}
  85. X
  86. X
  87. Xmain(argc, argv)
  88. Xint    argc;
  89. Xchar    **argv;
  90. X{
  91. X    char    buf[32];
  92. X    long    atol();
  93. X
  94. X    if (argc > 2) {
  95. X        perm(atol(argv[1]), argv[2], strlen(argv[2]), buf);
  96. X        printf("%s\n", buf);
  97. X    }
  98. X}
  99. + END-OF-FILE perm.c
  100. chmod 'u=rw,g=r,o=r' 'perm.c'
  101. set `wc -c 'perm.c'`
  102. count=$1
  103. case $count in
  104. 644)    :;;
  105. *)    echo 'Bad character count in ''perm.c' >&2
  106.         echo 'Count should be 644' >&2
  107. esac
  108. echo Extracting 'perm_number.c'
  109. sed 's/^X//' > 'perm_number.c' << '+ END-OF-FILE ''perm_number.c'
  110. Xlong    perm_number(s, nullperm, n)
  111. Xchar    *s, *nullperm;
  112. Xint    n;
  113. X{
  114. X    long    number = 0;
  115. X    char    *q, *r, *c_index, *t, *malloc(), *index();
  116. X
  117. X    if (!(t = malloc(n)))
  118. X        return -1;
  119. X
  120. X    strncpy(t, nullperm, n);
  121. X
  122. X    while (--n > 0) {
  123. X        if (!(c_index = index(t, *s++))) {
  124. X            number = -1;
  125. X            break;
  126. X        }
  127. X        number += c_index - t;
  128. X        number *= n;
  129. X        if (c_index == t)
  130. X            t++;
  131. X        else
  132. X            for (q = c_index, r = q + 1; *q++ = *r++; )
  133. X                ;
  134. X    }
  135. X
  136. X    free(t);
  137. X
  138. X    return number;
  139. X}
  140. X
  141. X
  142. Xmain(argc, argv)
  143. Xint    argc;
  144. Xchar    **argv;
  145. X{
  146. X    if (argc > 2)
  147. X        printf("%ld\n",
  148. X            perm_number(argv[1], argv[2], strlen(argv[2])));
  149. X}
  150. + END-OF-FILE perm_number.c
  151. chmod 'u=rw,g=r,o=r' 'perm_number.c'
  152. set `wc -c 'perm_number.c'`
  153. count=$1
  154. case $count in
  155. 572)    :;;
  156. *)    echo 'Bad character count in ''perm_number.c' >&2
  157.         echo 'Count should be 572' >&2
  158. esac
  159. exit 0
  160. --
  161.    "UNIX was never designed to keep people from doing stupid things, because
  162.     that policy would also keep them from doing clever things."  (Doug Gwyn)
  163.