[SOLVED] Basketball players problem | Sololearn: Learn to code for FREE!

+2

[SOLVED] Basketball players problem

import statistics players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] statistics.stdev(players) # std is 11.1 , mean is 189.2 So, I need to output how many players are between (189.2 - 11.1) to (189.2 + 11.1)

3/16/2021 12:05:57 PM

Kristo Kallfa

24 Answers

New Answer

+26

The correct answer is: players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] mean = sum(players)/len(players) stdvar = (sum((v-mean)**2 for v in players)/len(players))**0.5 low, high = mean-stdvar,mean+stdvar count = len([v for v in players if low < v < high]) print(count)

+3

All it was wrriten was just :”players = [180,172,178,185,190,195,192,200,210,190] My code is : import statistics players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] mean = (180 + 172 + 178 + 185 + 190 + 195 + 192 + 200 + 210 + 190) / 10 statistics.stdev(players) So, I know the mean and the std, all I have to do now is to check how many players are in that range... like I have to use smth like for players in range... idk...any idea?

+3

yes, you must loop over the array and count how many values matches the conditions ;) finally, just output the count...

+3

players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] import numpy as np mean = np.mean(players) standard_deviation = np.std(players) variance = np.var(players) result = 0 for i in players: if i >= mean-standard_deviation and i <= mean+standard_deviation: result += 1 print(result)

+1

Coalemus because your for loop should throw an error, as you're trying to iterate in a range of float (decimal) values, while it expect intergers (whole numbers)... anyway, you should post your own question to be helped further ^^

0

well, you're right... where is your coding attempt? we need to see what you've tried so far to be able to help ^^

0

Thank You!

0

Hello, in searching for values to get the desired output, I can't seem to find out why this code isn't working. import statistics players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] mean = (180 + 172 + 178 + 185 + 190 + 195 + 192 + 200 + 210 + 190) / 10 std = statistics.stdev(players) mean = statistics.mean(players) for players in range(mean-std, mean+std): print(players)

0

Oh my bad, thanks anyway!

0

kensy Nicolle Gutierrez Flores kindly stop spamming others threads, and rather post your own question in a brand new thread, by following the Q&A guidelines (accurate and short title, useful tags and explanation of your specific problem in the description field with your code attempt provided)...

0

The given code includes a list of heights for various basketball players. You need to calculate and output how many players are in the range of one standard deviation from the mean. players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] mean=sum(players)/len(players) var=sum((x-mean)**2 for x in players) std=var**0.5 for players in range(int(mean-std),int(mean+std)): print(players) what might be the issue with this code??

0

mine says indent 2

0

import math players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] sum = 0 for nums in players: sum+=nums mean = sum / len(players) new_list = [] for i in players: new_list.append(mean-i) new_sum = 0 for k in new_list: new_sum+=k**2 variance = new_sum/len(players) deviation = math.sqrt(variance) #print(variance) #print(deviation) #print(round(variance)) #print(round(deviation)) range1 = mean - deviation range2 = mean + deviation num_players = [] for j in players: if j in range(round(range1),round(range2)): num_players.append(j) else: pass print(len(num_players)) This is my answer you can check it out

0

my answer: import statistics players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] std_dev = int(statistics.stdev(players)) mean = int(statistics.mean(players)) lower_height = mean - std_dev upper_height = mean + std_dev standard_height = [] for p in players: if p > lower_height and p < upper_height: standard_height.append(p) print(len(standard_height)) the lower bound and upper bound shouldnt be included in the range

0

players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] mean = sum(players) / len(players) standard_deviation = (sum(((sum(players) / len(players)) - p)**2 for p in players) / len(players))**0.5 print(len([p for p in players if (mean - standard_deviation) <= p <= (mean + standard_deviation)]))

0

players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] mean = sum(players) / len(players) std = (sum([(i - mean) ** 2 for i in players]) / len(players)) ** 0.5 print(len(list(filter(lambda x: mean - std < x < mean + std, players))))

0

My way: players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] sum =0 count = 0 count1 = 0 sum_sqr = 0 for i in players: sum += i count += 1 mean = sum / count for i in players: sqr = (mean-i)**2 sum_sqr += sqr var = sum_sqr / count std = var**(1/2) for i in players: raz1 = mean - std raz2 = mean + std if (i > raz1 and i < raz2): count1 += 1 print(int(count1))

0

# Simple and for beginners and maths lovers players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] x = 0 y = 0 z = 0 for p in players: y += p m = y/len(players) for p in players : z = z + (p - m)**2 s = (z/len(players))**(1/2) for p in players : if p > m - s and p < m + s : x += 1 print(x)

0

Doing it without the statistics module, I solved it with: from math import sqrt data = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] mean = sum(data) / len(data) pre_variance = list(map(lambda number: (number - mean) * (number - mean), data)) variance = sum(pre_variance) / len(pre_variance) std = sqrt(variance) result = list(filter(lambda number: number > (mean - std) and number < (mean + std), data)) print(len(result))

0

players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] mean = sum(players)/10 diff = [] for i in players: difference = mean - i diff.append(difference) i += 1 sq_diff = [] for j in diff: square = j * j sq_diff.append(square) j = j+1 mean2 = sum(sq_diff)/10 stdev = mean2**(1/2) minimum = int(mean - stdev) maximum = int(mean + stdev) result = [] for l in range(minimum, maximum): if l in players: result.append(l) l = l+1 print(len(result)) simple and understandable code