A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
4
Task/Knuth-shuffle/Factor/knuth-shuffle.factor
Normal file
4
Task/Knuth-shuffle/Factor/knuth-shuffle.factor
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
: randomize ( seq -- seq )
|
||||
dup length [ dup 1 > ]
|
||||
[ [ iota random ] [ 1 - ] bi [ pick exchange ] keep ]
|
||||
while drop ;
|
||||
22
Task/Knuth-shuffle/Fantom/knuth-shuffle.fantom
Normal file
22
Task/Knuth-shuffle/Fantom/knuth-shuffle.fantom
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
class Main
|
||||
{
|
||||
static Void knuthShuffle (List array)
|
||||
{
|
||||
((array.size-1)..1).each |Int i|
|
||||
{
|
||||
r := Int.random(0..i)
|
||||
array.swap (i, r)
|
||||
}
|
||||
}
|
||||
|
||||
public static Void main ()
|
||||
{
|
||||
List a := [1,2,3,4,5]
|
||||
knuthShuffle (a)
|
||||
echo (a)
|
||||
|
||||
List b := ["apples", "oranges", "pears", "bananas"]
|
||||
knuthShuffle (b)
|
||||
echo (b)
|
||||
}
|
||||
}
|
||||
25
Task/Knuth-shuffle/GAP/knuth-shuffle.gap
Normal file
25
Task/Knuth-shuffle/GAP/knuth-shuffle.gap
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
# Return the list L after applying Knuth shuffle.
|
||||
Shuffle := function(L)
|
||||
local i, j, n;
|
||||
n := Length(L);
|
||||
for i in [n, n-1 .. 1] do
|
||||
j := Random(1, i);
|
||||
x := L[i];
|
||||
L[i] := L[j];
|
||||
L[j] := x;
|
||||
od;
|
||||
return L;
|
||||
end;
|
||||
|
||||
# Return a ''Permutation'' object (a permutation of 1..n).
|
||||
# They are printed in gap, in cycle decomposition form.
|
||||
PermShuffle := n -> PermListList([1 .. n], Shuffle([1 .. n]));
|
||||
|
||||
Shuffle([1..10]);
|
||||
# [ 4, 7, 1, 5, 8, 2, 6, 9, 10, 3 ]
|
||||
PermShuffle(10);
|
||||
# (1,9)(2,3,6,4,5,10,8,7)
|
||||
|
||||
# One may also call the built-in random generator on the symmetric group :
|
||||
Random(SymmetricGroup(10));
|
||||
(1,8,2,5,9,6)(3,4,10,7)
|
||||
10
Task/Knuth-shuffle/Groovy/knuth-shuffle-1.groovy
Normal file
10
Task/Knuth-shuffle/Groovy/knuth-shuffle-1.groovy
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
def shuffle = { list ->
|
||||
if (list == null || list.empty) return list
|
||||
def r = new Random()
|
||||
def n = list.size()
|
||||
(n..1).each { i ->
|
||||
def j = r.nextInt(i)
|
||||
list[[i-1, j]] = list[[j, i-1]]
|
||||
}
|
||||
list
|
||||
}
|
||||
5
Task/Knuth-shuffle/Groovy/knuth-shuffle-2.groovy
Normal file
5
Task/Knuth-shuffle/Groovy/knuth-shuffle-2.groovy
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def list = [] + (0..20)
|
||||
println list
|
||||
println shuffle(list)
|
||||
println shuffle(list)
|
||||
println shuffle(list)
|
||||
14
Task/Knuth-shuffle/Icon/knuth-shuffle-1.icon
Normal file
14
Task/Knuth-shuffle/Icon/knuth-shuffle-1.icon
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
procedure main()
|
||||
show(shuffle([3,1,4,1,5,9,2,6,3]))
|
||||
show(shuffle("this is a string"))
|
||||
end
|
||||
|
||||
procedure shuffle(A)
|
||||
every A[i := *A to 1 by -1] :=: A[?i]
|
||||
return A
|
||||
end
|
||||
|
||||
procedure show(A)
|
||||
every writes(!A," ")
|
||||
write()
|
||||
end
|
||||
3
Task/Knuth-shuffle/Icon/knuth-shuffle-2.icon
Normal file
3
Task/Knuth-shuffle/Icon/knuth-shuffle-2.icon
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
procedure shuffle(A)
|
||||
every !A :=: ?A
|
||||
end
|
||||
10
Task/Knuth-shuffle/Inform-6/knuth-shuffle.inf
Normal file
10
Task/Knuth-shuffle/Inform-6/knuth-shuffle.inf
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
[ shuffle a n i j tmp;
|
||||
for(i = n - 1: i > 0: i--)
|
||||
{
|
||||
j = random(i + 1) - 1;
|
||||
|
||||
tmp = a->j;
|
||||
a->j = a->i;
|
||||
a->i = tmp;
|
||||
}
|
||||
];
|
||||
1
Task/Knuth-shuffle/J/knuth-shuffle-1.j
Normal file
1
Task/Knuth-shuffle/J/knuth-shuffle-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
KS=:{~ (2&{.@[ {`(|.@[)`]} ])/@(,~(,.?@>:))@i.@#
|
||||
12
Task/Knuth-shuffle/J/knuth-shuffle-10.j
Normal file
12
Task/Knuth-shuffle/J/knuth-shuffle-10.j
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
]M=: /:~(1 2 3,:2 3 4),(11 2 3,: 0 11 2),(1 1 1,:1 0),:1 1 1,:1 0 1
|
||||
1 1 1
|
||||
1 0 0
|
||||
|
||||
1 1 1
|
||||
1 0 1
|
||||
|
||||
1 2 3
|
||||
2 3 4
|
||||
|
||||
11 2 3
|
||||
0 11 2
|
||||
12
Task/Knuth-shuffle/J/knuth-shuffle-11.j
Normal file
12
Task/Knuth-shuffle/J/knuth-shuffle-11.j
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
KS M
|
||||
11 2 3
|
||||
0 11 2
|
||||
|
||||
1 1 1
|
||||
1 0 1
|
||||
|
||||
1 1 1
|
||||
1 0 0
|
||||
|
||||
1 2 3
|
||||
2 3 4
|
||||
4
Task/Knuth-shuffle/J/knuth-shuffle-12.j
Normal file
4
Task/Knuth-shuffle/J/knuth-shuffle-12.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
]L=:'aA';'bbB';'cC%$';'dD@'
|
||||
+--+---+----+---+
|
||||
|aA|bbB|cC%$|dD@|
|
||||
+--+---+----+---+
|
||||
4
Task/Knuth-shuffle/J/knuth-shuffle-13.j
Normal file
4
Task/Knuth-shuffle/J/knuth-shuffle-13.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
KS L
|
||||
+--+----+---+---+
|
||||
|aA|cC%$|dD@|bbB|
|
||||
+--+----+---+---+
|
||||
1
Task/Knuth-shuffle/J/knuth-shuffle-14.j
Normal file
1
Task/Knuth-shuffle/J/knuth-shuffle-14.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
({~?~@#)
|
||||
20
Task/Knuth-shuffle/J/knuth-shuffle-15.j
Normal file
20
Task/Knuth-shuffle/J/knuth-shuffle-15.j
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
({~?~@#) A
|
||||
8 7 13 6 10 11 5 9 12
|
||||
|
||||
({~?~@#) M
|
||||
1 1 1
|
||||
1 0 1
|
||||
|
||||
1 2 3
|
||||
2 3 4
|
||||
|
||||
11 2 3
|
||||
0 11 2
|
||||
|
||||
1 1 1
|
||||
1 0 0
|
||||
|
||||
({~?~@#) L
|
||||
+----+---+--+---+
|
||||
|cC%$|bbB|aA|dD@|
|
||||
+----+---+--+---+
|
||||
3
Task/Knuth-shuffle/J/knuth-shuffle-2.j
Normal file
3
Task/Knuth-shuffle/J/knuth-shuffle-2.j
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
process J
|
||||
|
||||
fold swap transform array <==> f / g y
|
||||
8
Task/Knuth-shuffle/J/knuth-shuffle-3.j
Normal file
8
Task/Knuth-shuffle/J/knuth-shuffle-3.j
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(,~(,.?@>:))@i.@# 1+i.6
|
||||
0 0 0 0 0 0
|
||||
1 1 0 0 0 0
|
||||
2 0 0 0 0 0
|
||||
3 2 0 0 0 0
|
||||
4 3 0 0 0 0
|
||||
5 0 0 0 0 0
|
||||
0 1 2 3 4 5
|
||||
1
Task/Knuth-shuffle/J/knuth-shuffle-4.j
Normal file
1
Task/Knuth-shuffle/J/knuth-shuffle-4.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
2&{.@[ {`(|.@[)`]} ]
|
||||
1
Task/Knuth-shuffle/J/knuth-shuffle-5.j
Normal file
1
Task/Knuth-shuffle/J/knuth-shuffle-5.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
input { ~ shuffled indexes
|
||||
4
Task/Knuth-shuffle/J/knuth-shuffle-6.j
Normal file
4
Task/Knuth-shuffle/J/knuth-shuffle-6.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(|.@; ;&~./@(,. ?@>:)@i.@#)'abcde'
|
||||
+---+-+---+---+-+-----+
|
||||
|4 2|3|2 1|1 0|0|abcde|
|
||||
+---+-+---+---+-+-----+
|
||||
1
Task/Knuth-shuffle/J/knuth-shuffle-7.j
Normal file
1
Task/Knuth-shuffle/J/knuth-shuffle-7.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
KS=: [: > (<@C. >)/@(|.@; ;&~./@(,. ?@>:)@i.@#)
|
||||
2
Task/Knuth-shuffle/J/knuth-shuffle-8.j
Normal file
2
Task/Knuth-shuffle/J/knuth-shuffle-8.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
]A=: 5+i.9
|
||||
5 6 7 8 9 10 11 12 13
|
||||
2
Task/Knuth-shuffle/J/knuth-shuffle-9.j
Normal file
2
Task/Knuth-shuffle/J/knuth-shuffle-9.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
KS A
|
||||
13 10 7 5 11 9 8 6 12
|
||||
17
Task/Knuth-shuffle/Joy/knuth-shuffle-1.joy
Normal file
17
Task/Knuth-shuffle/Joy/knuth-shuffle-1.joy
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
DEFINE knuth-shuffle ==
|
||||
|
||||
(* Take the size of the array (without destroying it) *)
|
||||
dup dup size
|
||||
|
||||
(* Generate a list of as many random numbers *)
|
||||
[rand] [rem] enconcat map
|
||||
|
||||
(* Zip the two lists *)
|
||||
swap zip
|
||||
|
||||
(* Sort according to the new index number *)
|
||||
[small] [] [uncons unswonsd [first >] split [swons] dip2]
|
||||
[enconcat] binrec
|
||||
|
||||
(* Delete the new index number *)
|
||||
[second] map.
|
||||
3
Task/Knuth-shuffle/Joy/knuth-shuffle-2.joy
Normal file
3
Task/Knuth-shuffle/Joy/knuth-shuffle-2.joy
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
(* Sorted array of 21 integers *)
|
||||
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
|
||||
knuth-shuffle.
|
||||
22
Task/Knuth-shuffle/Liberty-BASIC/knuth-shuffle.liberty
Normal file
22
Task/Knuth-shuffle/Liberty-BASIC/knuth-shuffle.liberty
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
'Declared the UpperBound to prevent confusion with lots of 9's floating around....
|
||||
UpperBound = 9
|
||||
Dim array(UpperBound)
|
||||
|
||||
For i = 0 To UpperBound
|
||||
array(i) = Int(Rnd(1) * 10)
|
||||
Print array(i)
|
||||
Next i
|
||||
|
||||
For i = 0 To UpperBound
|
||||
'set a random value because we will need to use the same value twice
|
||||
randval = Int(Rnd(1) * (UpperBound - i))
|
||||
temp1 = array(randval)
|
||||
temp2 = array((UpperBound - i))
|
||||
array(randval) = temp2
|
||||
array((UpperBound - i)) = temp1
|
||||
Next i
|
||||
|
||||
Print
|
||||
For i = 0 To UpperBound
|
||||
Print array(i)
|
||||
Next i
|
||||
12
Task/Knuth-shuffle/Logo/knuth-shuffle-1.logo
Normal file
12
Task/Knuth-shuffle/Logo/knuth-shuffle-1.logo
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
to swap :i :j :a
|
||||
localmake "t item :i :a
|
||||
setitem :i :a item :j :a
|
||||
setitem :j :a :t
|
||||
end
|
||||
to shuffle :a
|
||||
for [i [count :a] 2] [swap 1 + random :i :i :a]
|
||||
end
|
||||
|
||||
make "a {1 2 3 4 5 6 7 8 9 10}
|
||||
shuffle :a
|
||||
show :a
|
||||
37
Task/Knuth-shuffle/Logo/knuth-shuffle-2.logo
Normal file
37
Task/Knuth-shuffle/Logo/knuth-shuffle-2.logo
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
to slice :lst :start :finish
|
||||
local "res
|
||||
make "res []
|
||||
for "i [:start :finish 1] [
|
||||
make "j item :i :lst
|
||||
make "res se :res :j
|
||||
]
|
||||
op :res
|
||||
end
|
||||
|
||||
to setitem :n :lst :val
|
||||
local "lhs
|
||||
local "rhs
|
||||
make "lhs slice :lst 1 :n-1
|
||||
make "rhs slice :lst :n+1 count :lst
|
||||
op (se :lhs :val :rhs)
|
||||
end
|
||||
|
||||
to swap :i :j :a
|
||||
local "t
|
||||
make "t item :i :a
|
||||
make "a setitem :i :a item :j :a
|
||||
make "a setitem :j :a :t
|
||||
op :a
|
||||
end
|
||||
|
||||
to shuffle :a
|
||||
for "i [count :a 2]
|
||||
[
|
||||
make "a swap 1 + random :i :i :a
|
||||
]
|
||||
op :a
|
||||
end
|
||||
|
||||
make "a ( list 1 2 3 4 5 6 7 8 9 10 )
|
||||
make "a shuffle :a
|
||||
show :a
|
||||
27
Task/Knuth-shuffle/M4/knuth-shuffle.m4
Normal file
27
Task/Knuth-shuffle/M4/knuth-shuffle.m4
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
divert(-1)
|
||||
define(`randSeed',141592653)
|
||||
define(`rand_t',`eval(randSeed^(randSeed>>13))')
|
||||
define(`random',
|
||||
`define(`randSeed',eval((rand_t^(rand_t<<18))&0x7fffffff))randSeed')
|
||||
define(`for',
|
||||
`ifelse($#,0,``$0'',
|
||||
`ifelse(eval($2<=$3),1,
|
||||
`pushdef(`$1',$2)$4`'popdef(`$1')$0(`$1',incr($2),$3,`$4')')')')
|
||||
define(`set',`define(`$1[$2]',`$3')')
|
||||
define(`get',`defn($1[$2])')
|
||||
define(`new',`set($1,size,0)')
|
||||
define(`deck',
|
||||
`new($1)for(`x',1,$2,
|
||||
`set(`$1',x,x)')`'set(`$1',size,$2)')
|
||||
define(`show',
|
||||
`for(`x',1,get($1,size),`get($1,x)`'ifelse(x,get($1,size),`',`, ')')')
|
||||
define(`swap',`set($1,$2,get($1,$4))`'set($1,$4,$3)')
|
||||
define(`shuffle',
|
||||
`define(`s',get($1,size))`'for(`x',1,decr(s),
|
||||
`swap($1,x,get($1,x),eval(x+random%(s-x+1)))')')
|
||||
divert
|
||||
|
||||
deck(`b',52)
|
||||
show(`b')
|
||||
shuffle(`b')
|
||||
show(`b')
|
||||
84
Task/Knuth-shuffle/MUMPS/knuth-shuffle.mumps
Normal file
84
Task/Knuth-shuffle/MUMPS/knuth-shuffle.mumps
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
Shuffle(items,separator) New ii,item,list,n
|
||||
Set list="",n=0
|
||||
Set ii="" For Set ii=$Order(items(ii)) Quit:ii="" Do
|
||||
. Set n=n+1,list(n)=items(ii),list=list_$Char(n)
|
||||
. Quit
|
||||
For Quit:list="" Do
|
||||
. Set n=$Random($Length(list))+1
|
||||
. Set item=list($ASCII(list,n))
|
||||
. Set $Extract(list,n)=""
|
||||
. Write item,separator
|
||||
. Quit
|
||||
Quit
|
||||
CardDeck New card,ii,suite
|
||||
Set ii=0
|
||||
For suite="Spades","Hearts","Clubs","Diamonds" Do
|
||||
. For card=2:1:10,"Jack","Queen","King","Ace" Do
|
||||
. . Set ii=ii+1,items(ii)=card_" of "_suite
|
||||
. . Quit
|
||||
. Quit
|
||||
Quit
|
||||
|
||||
Kill items
|
||||
Set items(91)="Red"
|
||||
Set items(82)="White"
|
||||
Set items(73)="Blue"
|
||||
Set items(64)="Yellow"
|
||||
Set items(55)="Green"
|
||||
Do Shuffle(.items," ") ; Red Yellow White Green Blue
|
||||
Do Shuffle(.items," ") ; Red Blue Yellow White Green
|
||||
Do Shuffle(.items," ") ; Green Blue Yellow White Red
|
||||
|
||||
Kill items Do CardDeck,Shuffle(.items,$Char(13,10))
|
||||
Queen of Hearts
|
||||
9 of Diamonds
|
||||
10 of Hearts
|
||||
King of Hearts
|
||||
7 of Diamonds
|
||||
9 of Clubs
|
||||
6 of Diamonds
|
||||
8 of Diamonds
|
||||
Jack of Spades
|
||||
Ace of Hearts
|
||||
Queen of Diamonds
|
||||
9 of Hearts
|
||||
2 of Hearts
|
||||
King of Clubs
|
||||
10 of Spades
|
||||
7 of Clubs
|
||||
6 of Clubs
|
||||
3 of Diamonds
|
||||
3 of Spades
|
||||
Queen of Clubs
|
||||
Ace of Spades
|
||||
4 of Hearts
|
||||
Ace of Diamonds
|
||||
7 of Spades
|
||||
Ace of Clubs
|
||||
King of Spades
|
||||
10 of Diamonds
|
||||
Jack of Diamonds
|
||||
8 of Clubs
|
||||
4 of Spades
|
||||
Jack of Hearts
|
||||
10 of Clubs
|
||||
4 of Diamonds
|
||||
3 of Hearts
|
||||
2 of Diamonds
|
||||
5 of Hearts
|
||||
Jack of Clubs
|
||||
2 of Clubs
|
||||
5 of Diamonds
|
||||
6 of Hearts
|
||||
4 of Clubs
|
||||
9 of Spades
|
||||
3 of Clubs
|
||||
5 of Spades
|
||||
6 of Spades
|
||||
7 of Hearts
|
||||
8 of Spades
|
||||
8 of Hearts
|
||||
2 of Spades
|
||||
Queen of Spades
|
||||
King of Diamonds
|
||||
5 of Clubs
|
||||
|
|
@ -0,0 +1 @@
|
|||
RandomSample[{1, 2, 3, 4, 5, 6}]
|
||||
10
Task/Knuth-shuffle/Mathematica/knuth-shuffle-2.mathematica
Normal file
10
Task/Knuth-shuffle/Mathematica/knuth-shuffle-2.mathematica
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
Shuffle[input_List /; Length[input] >= 1] :=
|
||||
Module[{indices = {}, allindices = Range[Length[input]]},
|
||||
Do[
|
||||
AppendTo[indices,
|
||||
Complement[allindices, indices][[RandomInteger[{1, i}]]]];
|
||||
,
|
||||
{i, Length[input], 1, -1}
|
||||
];
|
||||
input[[indices]]
|
||||
]
|
||||
|
|
@ -0,0 +1 @@
|
|||
Shuffle[{1, 2, 3, 4, 5, 6}]
|
||||
2
Task/Knuth-shuffle/Maxima/knuth-shuffle.maxima
Normal file
2
Task/Knuth-shuffle/Maxima/knuth-shuffle.maxima
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
/* Maxima has an implementation of Knuth shuffle */
|
||||
random_permutation([a, b, c]);
|
||||
29
Task/Knuth-shuffle/Modula-3/knuth-shuffle.mod3
Normal file
29
Task/Knuth-shuffle/Modula-3/knuth-shuffle.mod3
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
MODULE Shuffle EXPORTS Main;
|
||||
|
||||
IMPORT IO, Fmt, Random;
|
||||
|
||||
VAR a := ARRAY [0..9] OF INTEGER {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||
|
||||
PROCEDURE Shuffle(VAR a: ARRAY OF INTEGER) =
|
||||
VAR temp: INTEGER;
|
||||
n: INTEGER := NUMBER(a);
|
||||
BEGIN
|
||||
WITH rand = NEW(Random.Default).init() DO
|
||||
WHILE n > 1 DO
|
||||
WITH k = rand.integer(0, n - 1) DO
|
||||
DEC(n);
|
||||
temp := a[n];
|
||||
a[n] := a[k];
|
||||
a[k] := temp;
|
||||
END;
|
||||
END;
|
||||
END;
|
||||
END Shuffle;
|
||||
|
||||
BEGIN
|
||||
Shuffle(a);
|
||||
FOR i := FIRST(a) TO LAST(a) DO
|
||||
IO.Put(Fmt.Int(a[i]) & " ");
|
||||
END;
|
||||
IO.Put("\n");
|
||||
END Shuffle.
|
||||
Loading…
Add table
Add a link
Reference in a new issue