File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change 13
13
(head _L)
14
14
(@ _L (random 0 (- (len _L) 1)))))))
15
15
16
+ # @brief Shuffle a given list
17
+ # @details The original list is not modified
18
+ # @param _L list to shuffle
19
+ # =begin
20
+ # (import std.Random)
21
+ # (let data [1 2 3 4 5])
22
+ # (let randomized (random:shuffle data))
23
+ # =end
24
+ # @author https://github.com/SuperFola
25
+ (let shuffle (fun (_L) {
26
+ (mut _output [])
27
+
28
+ (while (not (empty? _L)) {
29
+ (let _idx (random 0 (- (len _L) 1)))
30
+ (append! _output (@ _L _idx))
31
+ (pop! _L _idx) })
32
+
33
+ _output }))
34
+
Original file line number Diff line number Diff line change 1
1
(import std.Random)
2
+ (import std.List :forEach :find)
2
3
(import std.Testing)
3
4
4
5
(test:suite random {
18
19
(test:eq "1" (random:choice "111"))
19
20
(test:eq "1" (random:choice "111"))
20
21
(test:eq "1" (random:choice "111"))
21
- (test:eq "1" (random:choice "111")) })})
22
+ (test:eq "1" (random:choice "111"))
23
+
24
+ (test:eq [] (random:shuffle []))
25
+ (test:eq [1] (random:shuffle [1]))
26
+
27
+ (let data [0 1 2 3 4 5 6 7 8 9])
28
+ (let randomized (random:shuffle data))
29
+ (test:eq (len data) (len randomized))
30
+
31
+ (forEach
32
+ randomized
33
+ (fun (e)
34
+ (test:expect (!= (list:find data e) -1))))
35
+ })})
22
36
You can’t perform that action at this time.
0 commit comments