-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathShuffle.kt
35 lines (30 loc) · 894 Bytes
/
Shuffle.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Created by gazollajunior on 03/04/16.
*/
import java.util.Random
fun <T:Comparable<T>>shuffle(items:MutableList<T>):List<T>{
val rg : Random = Random()
for (i in 0..items.size - 1) {
val randomPosition = rg.nextInt(items.size)
val tmp : T = items[i]
items[i] = items[randomPosition]
items[randomPosition] = tmp
}
return items
}
/* extension version */
fun <T> Iterable<T>.shuffle(): List<T> {
val list = this.toMutableList().apply { }
Collections.shuffle(list)
return list
}
fun main(args: Array<String>) {
println("\nOrdered list:")
val ordered = listOf<String>("Adam", "Clark", "John", "Tim", "Zack")
println(ordered)
println("\nshuffled list:")
val shuffled = shuffle(ordered as MutableList<String>)
print(shuffled)
val orderedB = listOf(1, 2, 3, 4, 5)
print(orderedB.shuffle())
}