0
House prices
import numpy as np data = np.array([150000, 125000, 320000, 540000, 200000, 120000, 160000, 230000, 280000, 290000, 300000, 500000, 420000, 100000, 150000, 280000]) """ You are given an array that represents house prices. Calculate and output the percentage of houses that are within one standard deviation from the mean. """ ##### """ To calculate the percentage, divide the number of houses that satisfy the condition by the total number of houses, and multiply the result by 100. """ https://code.sololearn.com/cDH9mSBk50jU/?ref=app https://code.sololearn.com/cDH9mSBk50jU/?ref=app
5 Réponses
+ 5
You must calculate mean and standard deviation (std).
mean + std and mean - std are the borders of the prices within one std. Count all prices that are in, and make a percentage of it
+ 4
What's the point to have test case result hidden? How one supposed to debug the code?
+ 3
import numpy as np
data = np.array([150000, 125000,320000,540000, 200000, 120000, 160000, 230000, 280000, 290000, 300000, 500000, 420000, 100000, 150000, 280000])
#Calculating tha mean
mean = sum(data)/len(data)
#Calculating Standard Deviation
st_dev = np.std(data)
#Stablishing the first standard deviation
mean1 = mean + st_dev
mean2 = mean - st_dev
#List with the values of the first STDEV
y = []
for x in data:
if (x >= mean2 and x <= mean1):
y.append(x)
#calculating the percentange
z = round((sum(y)/sum(data))*100, 2)
print("{}%".format(z))
+ 1
Thank you 😊