-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayabsurdity.py
29 lines (28 loc) · 1.15 KB
/
arrayabsurdity.py
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
"""
ARRAY ABSURDITY
CHALLENGE DESCRIPTION: Imagine we have an immutable array of size N which we know to be filled with integers ranging
from 0 to N-2, inclusive. Suppose we know that the array contains exactly one duplicated entry and that duplicate
appears exactly twice. Find the duplicated entry. (For bonus points, ensure your solution has constant space and time
proportional to N)
INPUT SAMPLE: Your program should accept as its first argument a path to a filename. Each line in this file is one test
case. Ignore all empty lines. Each line begins with a positive integer(N) i.e. the size of the array, then a semicolon
followed by a comma separated list of positive numbers ranging from 0 to N-2, inclusive. i.e eg.
5;0,1,2,3,0
20;0,1,10,3,2,4,5,7,6,8,11,9,15,12,13,4,16,18,17,14
OUTPUT SAMPLE: Print out the duplicated entry, each one on a new line eg
0
4
"""
import sys
test_cases = open(sys.argv[1], 'r')
for test in test_cases:
if test == "":
break
items = test.split(";")
size = items[0]
numbers = items[1].strip().split(",")
for i in numbers:
if numbers.count(i) == 2:
print i
break
test_cases.close()