You are viewing a single comment's thread. Return to all comments →
head = float(raw_input())
mylist = [float(i) for i in raw_input().split()]
zero = sum(j == 0 for j in mylist)
ng = sum(j < 0 for j in mylist)
ps = sum(j > 0 for j in mylist)
print round(ps/head, 3)
print round(ng/head, 3)
print round(zero/head, 3)
nice idea! not wasting memory and more importantly time in creating a list. Thanks for sharing!
When i read it correctly, she walks through 'mylist' 3 times. If she would go through each element once and do the compareision in that loop, she would walk through the list only once.
But because she also reads the input in a list and than splits it, she basicly is creating a temp list which isn't necessary. In java you can say (when using a Scanner) nextInt and don't even need a list at all.
As far as complexity goes, repeating an iteration 3 times is the same as doing it once so it's not the end of the world. Especially with the constraint that N <100.
The code idea is correct but we don't need to perform three O(n) operations for same stuff. The time complexity will remain O(n) but if taken for large array length, there will be slight difference in time in microseconds probably compared to that with one or two O(n) time complexity program.
We mostly don't care for space complexity (just for single list), thus, we may use it.
REDUCE COMPLEXITY TO WHATEVER YOU CAN!!
can you please explain this?
Nice ! But I think its better to use format than round since round will not display zeros after decimal.
can you plzz explain this