Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
46
Task/Perfect-shuffle/Dyalect/perfect-shuffle.dyalect
Normal file
46
Task/Perfect-shuffle/Dyalect/perfect-shuffle.dyalect
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
func shuffle(arr) {
|
||||
if arr.Length() % 2 != 0 {
|
||||
throw @InvalidValue(arr.Length())
|
||||
}
|
||||
var half = arr.Length() / 2
|
||||
var result = Array.Empty(arr.Length())
|
||||
var (t, l, r) = (0, 0, half)
|
||||
|
||||
while l < half {
|
||||
result[t] = arr[l]
|
||||
result[t+1] = arr[r]
|
||||
l += 1
|
||||
r += 1
|
||||
t += 2
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
func arrayEqual(xs, ys) {
|
||||
if xs.Length() != ys.Length() {
|
||||
return false
|
||||
}
|
||||
for i in xs.Indices() {
|
||||
if xs[i] != ys[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func shuffleThrough(original) {
|
||||
var copy = original.Clone()
|
||||
|
||||
while true {
|
||||
copy = shuffle(copy)
|
||||
yield copy
|
||||
if arrayEqual(original, copy) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for input in yields { 8, 24, 52, 100, 1020, 1024, 10000} {
|
||||
var numbers = [1..input]
|
||||
print("\(input) cards: \(shuffleThrough(numbers).Length())")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue