A function that outputs false if there is no overlap between Python numbers

Asked 2 years ago, Updated 2 years ago, 13 views

How do I construct a function that reads random numbers and outputs false if there is a duplicate between them?

python

2022-09-20 19:41

2 Answers

>>> 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


2022-09-20 19:41

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


2022-09-20 19:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.