new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
22
Task/Knuth-shuffle/Smalltalk/knuth-shuffle-1.st
Normal file
22
Task/Knuth-shuffle/Smalltalk/knuth-shuffle-1.st
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
"The selector swap:with: is documented, but it seems not
|
||||
implemented (GNU Smalltalk version 3.0.4); so here it is an implementation"
|
||||
SequenceableCollection extend [
|
||||
swap: i with: j [
|
||||
|t|
|
||||
t := self at: i.
|
||||
self at: i put: (self at: j).
|
||||
self at: j put: t.
|
||||
]
|
||||
].
|
||||
|
||||
Object subclass: Shuffler [
|
||||
Shuffler class >> Knuth: aSequenceableCollection [
|
||||
|n k|
|
||||
n := aSequenceableCollection size.
|
||||
[ n > 1 ] whileTrue: [
|
||||
k := Random between: 1 and: n.
|
||||
aSequenceableCollection swap: n with: k.
|
||||
n := n - 1
|
||||
]
|
||||
]
|
||||
].
|
||||
6
Task/Knuth-shuffle/Smalltalk/knuth-shuffle-2.st
Normal file
6
Task/Knuth-shuffle/Smalltalk/knuth-shuffle-2.st
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
"Test"
|
||||
|c|
|
||||
c := OrderedCollection new.
|
||||
c addAll: #( 1 2 3 4 5 6 7 8 9 ).
|
||||
Shuffler Knuth: c.
|
||||
c display.
|
||||
Loading…
Add table
Add a link
Reference in a new issue