Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1 +1,2 @@
|
|||
Implement the [[wp:Knuth shuffle|Knuth shuffle]] (a.k.a. the Fisher-Yates shuffle) for an integer array (or, if possible, an array of any type). The Knuth shuffle is used to create a random permutation of an array.
|
||||
Implement the [[wp:Knuth shuffle|Knuth shuffle]] (a.k.a. the Fisher-Yates shuffle) for an integer array (or, if possible, an array of any type).
|
||||
The Knuth shuffle is used to create a random permutation of an array.
|
||||
|
|
|
|||
46
Task/Knuth-shuffle/Eiffel/knuth-shuffle.e
Normal file
46
Task/Knuth-shuffle/Eiffel/knuth-shuffle.e
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
class
|
||||
APPLICATION
|
||||
inherit
|
||||
ARGUMENTS
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
do
|
||||
test:= <<1,2,3,4,5,6,7>>
|
||||
io.put_string ("Initial: ")
|
||||
across test as t loop io.put_string (t.item.out + " ") end
|
||||
create testresult.make_empty
|
||||
testresult:= shuffle (test)
|
||||
io.put_string ("%NShuffled: ")
|
||||
across testresult as t loop io.put_string (t.item.out + " ") end
|
||||
|
||||
end
|
||||
|
||||
test: ARRAY[INTEGER]
|
||||
testresult: ARRAY[INTEGER]
|
||||
|
||||
shuffle(ar:ARRAY[INTEGER]): ARRAY[INTEGER]
|
||||
local
|
||||
i,j:INTEGER
|
||||
ith: INTEGER
|
||||
random: V_RANDOM
|
||||
do
|
||||
create random
|
||||
from
|
||||
i:=ar.count
|
||||
until
|
||||
i=2
|
||||
loop
|
||||
j:=random.bounded_item (1, i)
|
||||
ith:= ar[i]
|
||||
ar[i]:= ar[j]
|
||||
ar[j]:= ith
|
||||
random.forth
|
||||
i:=i-1
|
||||
end
|
||||
Result:= ar
|
||||
end
|
||||
end
|
||||
2
Task/Knuth-shuffle/Frink/knuth-shuffle.frink
Normal file
2
Task/Knuth-shuffle/Frink/knuth-shuffle.frink
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
a = [1,2,3]
|
||||
a.shuffle[]
|
||||
|
|
@ -1,19 +1,19 @@
|
|||
# Return the list L after applying Knuth shuffle. GAP also has the function Shuffle, which does the same.
|
||||
ShuffleAlt := function(a)
|
||||
local i, j, n, t;
|
||||
n := Length(a);
|
||||
for i in [n, n - 1 .. 2] do
|
||||
j := Random(1, i);
|
||||
t := a[i];
|
||||
a[i] := a[j];
|
||||
a[j] := t;
|
||||
od;
|
||||
return a;
|
||||
local i, j, n, t;
|
||||
n := Length(a);
|
||||
for i in [n, n - 1 .. 2] do
|
||||
j := Random(1, i);
|
||||
t := a[i];
|
||||
a[i] := a[j];
|
||||
a[j] := t;
|
||||
od;
|
||||
return a;
|
||||
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]));
|
||||
PermShuffle := n -> PermList(ShuffleAlt([1 .. n]));
|
||||
|
||||
ShuffleAlt([1 .. 10]);
|
||||
# [ 4, 7, 1, 5, 8, 2, 6, 9, 10, 3 ]
|
||||
|
|
|
|||
22
Task/Knuth-shuffle/Objective-C/knuth-shuffle.m
Normal file
22
Task/Knuth-shuffle/Objective-C/knuth-shuffle.m
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface NSMutableArray (KnuthShuffle)
|
||||
- (void)knuthShuffle;
|
||||
@end
|
||||
@implementation NSMutableArray (KnuthShuffle)
|
||||
- (void)knuthShuffle {
|
||||
for (NSUInteger i = self.count-1; i > 0; i--) {
|
||||
NSUInteger j = arc4random_uniform(i+1);
|
||||
[self exchangeObjectAtIndex:i withObjectAtIndex:j];
|
||||
}
|
||||
}
|
||||
@end
|
||||
|
||||
int main() {
|
||||
@autoreleasepool {
|
||||
NSMutableArray *x = [NSMutableArray arrayWithObjects:@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, nil];
|
||||
[x knuthShuffle];
|
||||
NSLog(@"%@", x);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -15,7 +15,6 @@ function shuffle {
|
|||
done
|
||||
}
|
||||
|
||||
|
||||
# Test program.
|
||||
set -A array 11 22 33 44 55 66 77 88 99 110
|
||||
shuffle
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue