We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
# Enter your code here. Read input from STDIN. Print output to STDOUTset_nums=set([1,2,3])# calculate the standard deviation (uncorrected)defstd(set_nums):total=0xbar=sum(list(set_nums))/len(set_nums)fornuminlist(set_nums):total+=(num-xbar)**2# return the answer rounded to three decimal points (chosen to return correct answer)returnround((total/len(set_nums))**(1/2),3)target_stdev=std(set_nums)curr_stdev=0#currentstdev# strategy: brute force. check each number 5 to 0, incremented by 0.01# reason: we are looking for MAX number so start at 5# using this list comprehension because can't use range for floatsforiin[x/100forxinrange(500,0,-1)]:# we are adding and removing from a set so we want to avoid adding duplicates to set# otherwise in the remove step, we'd remove original elements of set_numsifiin[1,2,3]:continueset_nums.add(i)curr_stdev=std(set_nums)set_nums.remove(i)ifcurr_stdev==target_stdev:print(i)#printthenumber`i`sinceit'llbethemaxandbreakoutofloopbreak
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 1: Standard Deviation Puzzles #1
You are viewing a single comment's thread. Return to all comments →
Python solution: