Anonymous user
Find the missing permutation: Difference between revisions
→{{header|Racket}}: Naive method (using new builtin), and better style
(Replaced the two D entries with one, updated) |
(→{{header|Racket}}: Naive method (using new builtin), and better style) |
||
Line 1,155:
=={{header|Racket}}==
<lang racket>
#lang racket
(define almost-all
'([A B C D] [C A B D] [A C D B] [D A C B] [B C D A] [A C B D] [A D C B]
[C D A B] [D A B C] [B C A D] [C A D B] [C D B A] [C B A D] [A
[A D B C] [B D C A] [D C B A] [B A C D] [B A D C] [B D A C] [C
[D B C A] [D C A B]))
#| For permutations of any set of characters. |#▼
(for/hash ([(c i) (in-indexed (first almost-all))])▼
(values c i)))▼
(define size (hash-count character-enumeration))▼
;; Obvious method:
(for/first ([p (in-permutations (car almost-all))]
#:unless (member p almost-all))
p)
;; -> '(D B A C)
#| Illustrating approach mentioned in the task description.▼
For each position, character with odd parity at that position. |#▼
(require data/bit-vector)▼
(define charmap
(for/list ([position size])▼
(define parities (make-bit-vector size false))▼
▲(require data/bit-vector)
(for ([permutation almost-all])▼
(bit-vector-set! parities character-index▼
#:when (bit-vector-ref parities i))▼
c)))▼
#;"DBAC"</lang>▼
(define n (hash-ref charmap (list-ref permutation i)))
;; -> '(D B A C)
=={{header|REXX}}==
|