D
D
Dauren S2020-05-26 15:07:07
Python
Dauren S, 2020-05-26 15:07:07

What's wrong with the algorithm?

Good afternoon, the tester shows supposedly incorrect. What's wrong?
Task:
The program receives a large number of integers as input. All numbers except one have a pair, and there may be several identical pairs. Find a number without a pair.

def test(a): 
  for index,n in enumerate(a): 
    if(a.count(n)%2!=0): 
      return n 
a=[1,1,2,2,3,3,4,4,5,5,6,6,7,7,1]
print(test(a))


Time limit 1 second
Memory limit 64Mb
Input standard input or input-201.txt
Output standard output or input-201.a.txt Is
there something wrong with the input?
My time is 44ms
Memory 3.96Mb

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Ukolov, 2020-05-26
@dauren101

the tester shows supposedly incorrect
Most likely, you are not on time. Now your algorithm is very suboptimal - in the worst case, you do n 2 iterations over the array (count must also somehow calculate the values). Since the text of the problem specifically specifies a large number of numbers, it is understood that the algorithm will be as non-greedy as possible in terms of resources.
You can save time by wasting some memory and get by with two iterations - in the first, count the number of each number (through an increment in a special dictionary, not count), and in the second, find an odd number.

K
Konstantin, 2020-05-26
@Norraxx

- `enumerate(a)` creates a copy of the field
- `arr` is not used at all
- `test` function may be isolated from `a`
- `index` is not used

G
galaxy, 2020-05-26
@galaxy

A classic problem for algorithms in a vacuum.

spoiler
Числа можно просто поксорить

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question