+ 1

Basketball Players

Python for data science first challenge. Code appears to be giving the correct output but isn't passing the test case, which is hidden. Anyone have an idea why? Am i misinterpreting the task? https://code.sololearn.com/c15RTp4cpm28/?ref=app

1st Feb 2022, 5:36 PM
Martin Kerr
14 Réponses
+ 2
The task ask us to use the formula introduced in the course with n as a denominator. stdev() calculates the standard deviation with n-1 https://code.sololearn.com/WU5Fss4TVVz0/?ref=app
1st Feb 2022, 6:41 PM
Lisa
Lisa - avatar
+ 4
When calculating variance with a function, make sure you calculate the type of variance that you really want n: "population variance" n - 1: "sample variance" Usually you will want to do the later of the 2
1st Feb 2022, 6:54 PM
Lisa
Lisa - avatar
+ 4
p is for population, I suppose. pstdev() is different from stdev()
1st Feb 2022, 7:01 PM
Lisa
Lisa - avatar
+ 2
Paul K Sadler You can look at the docs and see what statistics.pstdev() yields as result ;)
1st Feb 2022, 6:48 PM
Lisa
Lisa - avatar
+ 1
looking at the code, it seems correct to me. I used a while loop and counted, but it's just a different way of doing the same thing. count = 0 i = 0 while i < numPlayers: if(players[i] >= theMean - standardD and players[i] <= theMean + standardD): count +=1 i+=1 print(count) The only other difference I see is that I calculated mean and standard deviation because I hadn't got to the library functions yet. The library functions surely calculate them correctly, so that's not it. I even ran my code again, because I did it months ago, just in case something had changed. it still passes. Next I ran my code outside of the challenge and did the same with yours. yours gets 7 and min, which passes, gets 6. Next I checked the list data to be sure they matched and they do. So I added prints to see our mean and standard deviation were the same. Themean matched at 189.2 but the standard deviation was different! Whaaaat? Yours is 11.1335332... and mine is 10.55219...idk.
1st Feb 2022, 5:47 PM
Paul K Sadler
Paul K Sadler - avatar
+ 1
Thanks, I've just tried print(6) which passes, so clearly that's what it's looking for. I'll have to look more closely to find out why mine is arriving at 7. Thank you!
1st Feb 2022, 6:35 PM
Martin Kerr
+ 1
Brilliant, thank you both! I had no idea of pstdev or indeed how it differed from stdev. That solves the problem but I shall go back and try to solve by coding the raw mathematics as it intended rather than importing statistics.
1st Feb 2022, 8:20 PM
Martin Kerr
0
So now I am wondering if how I calculate standard deviation is slightly off, because surely the function is correct and that's why mine passed. It also makes me wonder if our method is slightly off because our tests to include players is the same.. https://code.sololearn.com/cFE63ReNqfIy/?ref=app
1st Feb 2022, 6:24 PM
Paul K Sadler
Paul K Sadler - avatar
0
Yeah, me too, I must be calculating the variance or standard deviation wrong
1st Feb 2022, 6:36 PM
Paul K Sadler
Paul K Sadler - avatar
0
Lisa So can you pass n to the function Martin used? Spec: You need to calculate and output how many players are in the range of one standard deviation from the mean. 👌
1st Feb 2022, 6:46 PM
Paul K Sadler
Paul K Sadler - avatar
0
Lisa sorry, I was being lazy, I am doing this and working on something else at the same time 🙃😂 is pstdev a typo, or is that a different function?
1st Feb 2022, 6:49 PM
Paul K Sadler
Paul K Sadler - avatar
0
Martin I think Lisa has zeroed in on the issue.
1st Feb 2022, 6:53 PM
Paul K Sadler
Paul K Sadler - avatar
0
Batl
2nd Feb 2022, 6:25 PM
Jahongir Normurotov 16 Years
0
players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] #new list array to get number of players in mean-std and mean+std list=[] #mean mean=sum(players)/len(players) #variance x=[(i**2) for i in players] var=(1/len(players))*(sum(x))-mean**2 #Standard Deviation std=var**0.5 x,y=(int(mean-std),int(mean+std)) for i in players : if i>x and i<y : list.append(i) #len print(len(list))
8th Sep 2022, 8:31 PM
ALI Moussa Kadjalla
ALI Moussa Kadjalla - avatar