Sort by

recency

|

24 Discussions

|

  • + 0 comments

    the exact answer is the maximum root of simple quadratic equation 9*N^2 -36*N+28=0

    abtained after a few algebraic substitutions and expansions

  • + 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
    
  • + 0 comments

    I got 2.94 if I run the code in VScode, but I got Runtime error when I submitted it. What was the error?

    import numpy as np
    
    A = [1, 2, 3]
    stdA = np.std(A)
    
    mn = float('inf')
    N = 0
    for i in np.arange(0, 4.01, 0.01):
        B = [1, 2, 3, i]
        stdB = np.std(B)
        if (abs(stdA - stdB) <= mn):
            mn = abs(stdA - stdB)
            N = i
    
    print(N)
    
  • + 0 comments

    I get 2.98, but the answer is wrong.

    Althoug std of [1.0, 2.0, 3.0, 2.98] is the same as std of [1,2,3], which is 0.82 (rounded to 2 decimal places, as decribed)

    What am I missing?

    import numpy as np
    
    arr = [1.0, 2.0, 3.0]
    
    std = round(np.std(arr),2)
    
    print('Standard Deviation of ', arr, 'is', std)
    print()
    
    num = 0.0
    for i in range(1,100001):
      num = round(num + 0.01, 2)
      arr.append(num)
      res_std = round(np.std(arr), 2)
      if res_std == std:
        res = num
      arr.pop()
    print(res)
    
  • + 0 comments
    n = np.array([1, 2, 3])
    n.std() --> 0.82
    n2 = np.random.normal(loc = 2, scale = 0.82, size = 4)
    n2.max() --> 2.97
    n2 = np.array([1, 2, 3, 2.97])
    n2.std() --> 0.82