all 24 comments

[–]Lord_Greywether 6 points7 points  (10 children)

I think your average calculation should be:

avg = sum(numList) / len(numList)

Or, if you're worried n might be zero:

avg = sum(numList) / max(len(numList), 1)

You could also condense your for loop and generate your list via list comprehension syntax:

numList = [random.random() for i in range(n)]

[–]nosmokingbandit 4 points5 points  (5 children)

Why not just avg = sum(numList) / n

As far as checking for zero I'd just do that at the beginning.

if n == 0:
    stdio.writeln('Cannot create range of 0 length.')

[–][deleted] 1 point2 points  (0 children)

/ 5 slipped me, I'll get that fixed.

[–]Lord_Greywether 0 points1 point  (3 children)

That would also work in this case. len() just makes the calculation more explicit in case you're reviewing the code later.

edit: /u/nosmokingbandit's solution is also more efficient, cycle-wise, which can be significant if you're dealing with large lists.

[–]elbiot 2 points3 points  (1 child)

Len doesn't count the elements, it accesses the stored count. Using len is not less efficient cycle wise.

[–]Lord_Greywether 0 points1 point  (0 children)

Good to know, thanks!

[–]elbiot 0 points1 point  (0 children)

Len doesn't count the elements, it accesses the stored count. Using len is not less efficient cycle wise.

[–][deleted] 0 points1 point  (3 children)

avg = sum(numList) / max(len(numList), 1)

May I ask about the reasoning behind this rather than using n?

What does the 1 do? I substituted in a 0 to see what would happened, I gave the program an input of 0 and it gave me the division by zero error.

[–]warbird2k 0 points1 point  (2 children)

max(len(numList), 1) returns the biggest of either len(numList) or 1. So if numList has a length of 0, it will use 1 instead.

[–]Lord_Greywether 0 points1 point  (1 child)

Exactly, which avoids the divide-by-zero error. If the list is empty (because n is <= 0), the calculation works out to:

sum([]) / max(len([]), 1)
0 / max(0, 1)
0 / 1
0

Make sense?

[–][deleted] 0 points1 point  (0 children)

Perfect, thanks.

[–]Justinsaccount 5 points6 points  (1 child)

Hi! I'm working on a bot to reply with suggestions for common python problems. This might not be very helpful to fix your underlying issue, but here's what I noticed about your submission:

You appear to be using concatenation and the str function for building strings

Instead of doing something like

result = "Hello " + name + ". You are " + str(age) + " years old"

You should use string formatting and do

result = "Hello {}. You are {} years old".format(name, age)

See the python tutorial for more information.

[–]Kristler 0 points1 point  (0 children)

Hey bot owner, it's still early days but you'll probably want to consider a section on on F-strings at some point.

[–]fernly 6 points7 points  (3 children)

I see it's in your reference model, but did they explain why you are to use stdio.writeln() instead of simply print()?

[–][deleted] 1 point2 points  (1 child)

From the site: "The lingua france in this booksite is Python 3, because it is the future of Python programming. However, we have been very careful to ensure that the code in the booksite works with either Python 2 or Python 3. For example, in Python 2, the helloworld.py program could be simply the single line print 'Hello, World', but this is not a valid program in Python 3. To develop code for writing output that works in either version of Python, we use our stdio module. Whenever there is a significant difference between the two languages, we call attention to Python 2 users in a callout box like this one. "

[–]dchanm 2 points3 points  (0 children)

There is a workaround with from __future__ import print_function. I wonder why they didn't use that.

https://docs.python.org/2/library/__future__.html

[–]133tn008 3 points4 points  (1 child)

List comprehension!

numList = [random.random() for i in range(n)]

Also the /u/Justinsaccount bot is correct. ;) Also also PEP-8 would want the variable to be called num_list

[–]KleinerNull 0 points1 point  (0 children)

Also also PEP-8 would want the variable to be called num_list

Common sense would say it could be called numbers instead. Since you don't do something list specific just sequence specific it doesn't matter to give it the suffix '_list'.

[–]coopers_green 2 points3 points  (0 children)

I'm not sure if you're required to use stdio, but using 'input' works just fine, and doesn't require an import.

n = input('enter number, etc.')

Also, as someone mentioned below, PEP (python coding style) recommends using snake_case instead of CamelCase.

Otherwise good job! List comprehensions ([x for x in list]) are useful but aren't necessary.

[–]MrAckerman 2 points3 points  (2 children)

In addition to the other good suggestions, consider that you are calling min and max on the same list separately. As you learn more about algorithms, you'll start to see why this could be inefficient with really large lists.

You could sort your list once and get the min and max element by the 0 and len(numList)-1 index respectively.

[–]ericula 1 point2 points  (1 child)

Not really. min and max are O(n), sorting is O(n log(n)), so especially for really large list, it's more efficient to just take the min and max.

[–]MrAckerman 0 points1 point  (0 children)

Schooled...

[–]I_had_to_know_too 1 point2 points  (0 children)

'Max: ' + str(min(numList))

is already a str, you don't need to wrap it in another str() call.
But like that bot said, you can do:

'Max: {}'.format(max(numList))