How do I construct a function that reads random numbers and outputs false if there is a duplicate between them?
python
>>> numbers = [ random.randint(1, 20) for _ in range(5) ]
>>> numbers
[19, 9, 18, 5, 15]
>>> def has_duplicates(l):
... ... return len(l) != len(set(l))
...
>>> has_duplicates(numbers)
False
>>> numbers = [ random.randint(1, 20) for _ in range(5) ]
>>> numbers
[16, 9, 19, 9, 7]
>>> has_duplicates(numbers)
True
Try using scalar
import scala.util.Random
val numbers = for(_ <- 1 to 10) yield Random.nextInt(100)
=> numbers: scala.collection.immutable.IndexedSeq[Int] = Vector(37, 44, 39, 42, 4, 63, 74, 47, 73, 47)
def isDuplicate[A](numbers: Seq[A]):Boolean = numbers.length != numbers.distinct.length
isDuplicate(numbers)
=> res375: Boolean = true
© 2024 OneMinuteCode. All rights reserved.