Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,19 @@
PROGRAM KNUTH_SHUFFLE
CONST CARDS%=52
DIM PACK%[CARDS%]
BEGIN
RANDOMIZE(TIMER)
FOR I%=1 TO CARDS% DO
PACK%[I%]=I%
END FOR
FOR N%=CARDS% TO 2 STEP -1 DO
SWAP(PACK%[N%],PACK%[1+INT(N%*RND(1))])
END FOR
FOR I%=1 TO CARDS% DO
PRINT(PACK%[I%];)
END FOR
PRINT
END PROGRAM

View file

@ -0,0 +1,29 @@
Remark- The native '''shuffle''' function implementation in EchoLisp has been replaced by this one.
Thx Rosetta Code.
(lib 'list) ;; for list-permute
;; use "inside-out" algorithm, no swapping needed.
;; returns a random permutation vector of [0 .. n-1]
(define (rpv n (j))
(define v (make-vector n))
(for [(i n)]
(set! j (random (1+ i)))
(when (!= i j) (vector-set! v i [v j]))
(vector-set! v j i))
v)
;; apply to any kind of list
(define (k-shuffle list)
(list-permute list (vector->list (rpv (length list)))))
;; out
(k-shuffle (iota 17))
→ (16 7 11 10 0 9 15 12 13 8 4 2 14 3 6 5 1)
(k-shuffle
'(adrien 🎸 alexandre 🚂 antoine 🍼 ben 📚 georges 📷 julie 🎥 marine 🐼 nathalie 🍕 ))
→ (marine alexandre 🎥 julie 🎸 ben 🍼 nathalie 📚 georges 🚂 antoine adrien 🐼 📷 🍕)
(shuffle ;; native
'(adrien 🎸 alexandre 🚂 antoine 🍼 ben 📚 georges 📷 julie 🎥 marine 🐼 nathalie 🍕 ))
→ (antoine 🎥 🚂 marine adrien nathalie 🍼 🍕 ben 🐼 julie 📷 📚 🎸 alexandre georges)

View file

@ -0,0 +1,77 @@
' version 22-10-2016
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
' sort from lower bound to the highter bound
' array's can have subscript range from -2147483648 to +2147483647
Sub knuth_down(a() As Long)
Dim As Long lb = LBound(a)
Dim As ULong n = UBound(a) - lb +1
Dim As ULong i, j
Randomize Timer
For i = n -1 To 1 Step -1
j =Fix(Rnd * (i +1)) ' 0 <= j <= i
Swap a(lb + i), a(lb + j)
Next
End Sub
Sub knuth_up(a() As Long)
Dim As Long lb = LBound(a)
Dim As ULong n = UBound(a) - lb +1
Dim As ULong i, j
Randomize Timer
For i = 0 To n -2
j = Fix(Rnd * (n - i) + i) ' 0 <= j < n-i, + i ==> i <= j < n
Swap a(lb + i), a(lb + j)
Next
End Sub
' ------=< MAIN >=------
Dim As Long i
Dim As Long array(1 To 52), array2(-7 To 7)
For i = 1 To 52 : array(i) = i : Next
Print "Starting array"
For i = 1 To 52
Print Using " ###";array(i);
Next : Print : Print
knuth_down(array())
Print "After Knuth shuffle downwards"
For i = 1 To 52
Print Using " ###";array(i);
Next : Print : Print
For i = LBound(array2) To UBound(array2)
array2(i) = i - LBound(array2) + 1
Next
Print "Starting array, first index <> 0 "
For i = LBound(array2) To UBound(array2)
Print Using " ##";array2(i);
Next : Print : Print
knuth_up(array2())
Print "After Knuth shuffle upwards"
For i = LBound(array2) To UBound(array2)
Print Using " ##";array2(i);
Next : Print : Print
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

View file

@ -0,0 +1,9 @@
def shuffle( a ) =
res = array( a )
n = a.length()
for i <- 0:n
r = rnd( i:n )
res(i), res(r) = res(r), res(i)
res.toList()

View file

@ -0,0 +1,20 @@
define staticarray->swap(p1::integer,p2::integer) => {
fail_if(
#p1 < 1 or #p2 < 1 or
#p1 > .size or #p2 > .size,
'invalid parameters'
)
#p1 == #p2
? return
local(tmp) = .get(#p2)
.get(#p2) = .get(#p1)
.get(#p1) = #tmp
}
define staticarray->knuthShuffle => {
loop(-from=.size, -to=2, -by=-1) => {
.swap(math_random(1, loop_count), loop_count)
}
}
(1 to 10)->asStaticArray->knuthShuffle&asString

View file

@ -0,0 +1,11 @@
import math
randomize()
proc shuffle[T](x: var seq[T]) =
for i in countdown(x.high, 0):
let j = random(i + 1)
swap(x[i], x[j])
var x = @[0,1,2,3,4,5,6,7,8,9]
shuffle(x)
echo x

View file

@ -0,0 +1,5 @@
Indexable method: shuffle
| s i l |
self asListBuffer ->l
self size dup ->s 1- loop: i [ s i - rand i + i l swapValues ]
l dup freeze ;

View file

@ -0,0 +1,8 @@
sequence cards = tagset(52)
puts(1,"Before: ") ?cards
for i=52 to 1 by -1 do
integer r = rand(i)
{cards[r],cards[i]} = {cards[i],cards[r]}
end for
puts(1,"After: ") ?cards
puts(1,"Sorted: ") ?sort(cards)

View file

@ -0,0 +1,16 @@
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
shuffle(a)
for n = 1 to len(a)
see "" + a[n] + " "
next
func shuffle t
n = len(t)
while n > 1
k = random(n-1)+1
temp = t[n]
t[n] = t[k]
t[k] = temp
n = n - 1
end
return t

View file

@ -0,0 +1,11 @@
func shuffle(a) {
{ |n|
var k = (n+1 -> irand);
k == n || (a[k, n] = a[n, k]);
} * a.end;
return a;
}
say shuffle(@(1..10));

View file

@ -0,0 +1,20 @@
import func Darwin.arc4random_uniform
extension Array {
func shuffle() -> Array {
var result = self; result.shuffleInPlace(); return result
}
mutating func shuffleInPlace() {
for i in 1 ..< count { swap(&self[i], &self[Int(arc4random_uniform(UInt32(i+1)))]) }
}
}
// Swift 2.0:
print([1, 2, 3, 4, 5, 6, 7, 8, 9, 10].shuffle())
// Swift 1.x:
//println([1, 2, 3, 4, 5, 6, 7, 8, 9, 10].shuffle())

View file

@ -0,0 +1,31 @@
import func Darwin.arc4random_uniform
func shuffleInPlace<T: MutableCollectionType where T.Index: RandomAccessIndexType>(inout collection: T) {
let i0 = collection.startIndex
for i in i0.successor() ..< collection.endIndex {
let j = i0.advancedBy(numericCast(
arc4random_uniform(numericCast(
i0.distanceTo()
)+1)
))
swap(&collection[i], &collection[j])
}
}
func shuffle<T: MutableCollectionType where T.Index: RandomAccessIndexType>(collection: T) -> T {
var result = collection
shuffleInPlace(&result)
return result
}
// Swift 2.0:
print(shuffle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
// Swift 1.x:
//println(shuffle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))

View file

@ -0,0 +1,50 @@
import func Darwin.arc4random_uniform
// Define a protocol for shuffling:
protocol Shufflable {
@warn_unused_result (mutable_variant="shuffleInPlace")
func shuffle() -> Self
mutating func shuffleInPlace()
}
// Provide a generalized implementation of the shuffling protocol for any mutable collection with random-access index:
extension Shufflable where Self: MutableCollectionType, Self.Index: RandomAccessIndexType {
func shuffle() -> Self {
var result = self
result.shuffleInPlace()
return result
}
mutating func shuffleInPlace() {
let i0 = startIndex
for i in i0+1 ..< endIndex {
let j = i0.advancedBy(numericCast(
arc4random_uniform(numericCast(
i0.distanceTo(i)
)+1)
))
swap(&self[i], &self[j])
}
}
}
// Declare Array's conformance to Shufflable:
extension Array: Shufflable
{ /* Implementation provided by Shufflable protocol extension */ }
print([1, 2, 3, 4, 5, 6, 7, 8, 9, 10].shuffle())