简体   繁体   中英

Why do Python lists in my computer don't hold more than 693 numbers?

I was solving the Day 0 of 10 Days of Statistics problems in HackerRank the other day. My solution was pretty straightforward and it worked with almost all the test cases. The one my code fails in has 2500 numbers as input. I placed a few prints to see how this is happening and discovered my list for the numbers holds only the 693 values of all.

Here is the part to find median:

number_of_items = int(input().strip())
inputs = list(map(int, input().split(' ')))
inputs.sort()

if (number_of_items % 2) == 0:
    n = number_of_items // 2 - 1
    median = (inputs[n] + inputs[n+1])/2
else:
    n = number_of_items // 2
    median = inputs[n-1]
print(median)

Here is the full code: https://gist.github.com/mnzr/5a6f6c1c49d4dc0dbb940ed3ecba79ff

I have tried this code on an online editor and it worked, with an exception: the mode was way off from the actual number.

I thought Python lists can hold large amount of numbers! Why doesn't it work on my PC?

Edit: I have had a friend solve the problem on his own and relay me the results. Then told him to run my code and tell me what he sees. Here is his code: https://gist.github.com/sz-ashik440/192bc22b18da0292832e65997a6787a7 Here is what happened: 1: His code worked, he said. I ran it and it worked on my PC too! For the first time that is. 2: I gave him my code and run it myself again. I saw there were only 693 elements, he reported the same. 3: And perhaps the most surprising thing is, his own version has gave the same out of index error and an array with the size of 693! My friend's own code on his own PC now gives wrong answers.

Here is my system config:

  • Intel Core i5, 5th gen
  • 8GB of RAM, 1600 MHz bus speed
  • Ubuntu 16.04.1
  • Python 3.5.2

My friend is using Python 3.4.3 on Ubuntu 14.04.

I don't know why you are experiencing this limitation when running your code on your local machine, however, I strongly suspect that it is due to your input data - perhaps a stray new line character as suggested by Martijn Pieters .

The problem that you have with HackerRank is due to your mode calculation:

# mode
occurances = {n: inputs.count(n) for n in inputs}
mode = min(occurances)
print(mode)

This always selects the lowest value from the input, not the lowest of the most frequently occurring values. Here is one way to do that:

from collections import Counter

c = Counter(inputs)
max_count = c.most_common(1)[0][1]
print(sorted([x for x in c if c[x] == max_count])[0])

Note that there is also another problem, you need to round the mean and median values to 1 decimal place, but this error is not exposed by HackerRank's test data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM