• + 0 comments

    Python solution:

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    set_nums = set([1,2,3])
    
    # calculate the standard deviation (uncorrected)
    def std(set_nums):
        total = 0
        xbar = sum(list(set_nums)) / len(set_nums)
        for num in list(set_nums):
            total += (num - xbar)**2
        # return the answer rounded to three decimal points (chosen to return correct answer)
        return round((total / len(set_nums))**(1/2),3)
    
    
    target_stdev = std(set_nums) 
    curr_stdev = 0  # current stdev
    
    # 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 floats
    for i in [x / 100 for x in range(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_nums
        if i in [1,2,3]:
            continue
        set_nums.add(i)
        curr_stdev = std(set_nums)
        set_nums.remove(i)
        if curr_stdev == target_stdev:
            print(i)  # print the number `i` since it'll be the max and break out of loop
            break