-
Notifications
You must be signed in to change notification settings - Fork 0
/
permutations-ii.rkt
42 lines (37 loc) · 971 Bytes
/
permutations-ii.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(define (succs state)
(define cnt (car state))
(define perm (cdr state))
(map
(λ (k)
(cons
(if (= (hash-ref cnt k) 1)
(hash-remove cnt k)
(hash-update cnt k sub1))
(cons k perm)))
(hash-keys cnt)))
(define (end? state)
(hash-empty? (car state)))
(define (search states)
(cond
[(null? states) '()]
[else
(define state (car states))
(if (end? state)
(cons state (search (cdr states)))
(search (append (succs state) (cdr states))))]))
(define (count xs)
(foldl
(λ (x cnt)
(if
(hash-has-key? cnt x)
(hash-update cnt x add1)
(hash-set cnt x 1)))
(make-immutable-hash)
xs))
(define (extract-results states)
(map cdr states))
(define/contract (permute-unique nums)
(-> (listof exact-integer?) (listof (listof exact-integer?)))
(define cnt (count nums))
(define init-states (list (cons cnt '())))
(extract-results (search init-states)))