PROG0344 - Doubles

Which numbers appear multiple times in a given list?

Assignment

  1. Write a function double that takes a list of numbers as an argument, and prints the element of the list that occurs twice. The function must print the value None if no number occurs more than once in the list. There is one number at the most that appears twice.
  2. Write a function doubles that takes a list of numbers as an argument, and prints two collections. The first collection contains all elements that only occur once in the list and the second collection contains all elements that occur more than once in the list.

Example

>>> double([1, 2, 3, 4, 2])
2
>>> double([1, 2, 3, 4])
>>> double([1, 2, 3, 4, 5, 6, 100, -234, 15, 0, -20000, 15])
15

>>> doubles([1, 2, 3, 4, 2])
({1, 3, 4}, {2})
>>> doubles([2, 8, 8, 6, 10, -20, -4, -2, -4])
({2, 6, 10, -20, -2}, {8, -4})
>>> doubles([1, 3, 5, 7, 2, 4, 6])
({1, 2, 3, 4, 5, 6, 7}, set())

Welke getallen komen meerdere keren voor in een gegeven lijst?

Opgave

  1. Schrijf een functie dubbel die een lijst van getallen als argument neemt, en het element van de lijst teruggeeft dat tweemaal voorkomt. De functie moet de waarde None teruggeven als er geen enkel getal meer dan één keer voorkomt in de lijst. Er is hoogstens één getal dat tweemaal voorkomt.
  2. Schrijf een functie dubbels die een lijst van getallen als argument neemt, en twee verzamelingen teruggeeft. De eerste verzameling bevat alle elementen die slechts eenmaal voorkomen in de lijst en de tweede verzameling bevat alle elementen die meer dan eenmaal voorkomen in de lijst.

Voorbeeld

>>> dubbel([1, 2, 3, 4, 2])
2
>>> dubbel([1, 2, 3, 4])
>>> dubbel([1, 2, 3, 4, 5, 6, 100, -234, 15, 0, -20000, 15])
15

>>> dubbels([1, 2, 3, 4, 2])
({1, 3, 4}, {2})
>>> dubbels([2, 8, 8, 6, 10, -20, -4, -2, -4])
({2, 6, 10, -20, -2}, {8, -4})
>>> dubbels([1, 3, 5, 7, 2, 4, 6])
({1, 2, 3, 4, 5, 6, 7}, set())

Added by:Peter Dawyndt
Date:2013-03-26
Time limit:10s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:PY_NBC
Resource:None

© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.