Skip to content

Commit 0d91e90

Browse files
authored
Create Random Difference
1 parent 8d60fe3 commit 0d91e90

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

interview_query/Random Difference

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Write a function to generate M samples from a random normal distribution of size N.
2+
3+
Then return the average difference between the 5th and 6th lowest values.
4+
5+
Note: N should always be greater than 6.
6+
7+
Bonus: How would you verify your function returning the correct results?
8+
9+
Example:
10+
11+
Input:
12+
13+
N = 10
14+
M = 20
15+
Output:
16+
17+
random_differences(M, N) -> 0.5
18+
------------------------------------------------------------------------------------------------------------------------
19+
def random_differences(M, N):
20+
import numpy as np
21+
result = [0 for _ in range(M)]
22+
23+
for i in range(M):
24+
normal_tmp = np.random.normal(0, 1, N).tolist()
25+
result[i] = sorted(normal_tmp)[5] - sorted(normal_tmp)[4]
26+
27+
return np.mean(result)
28+
29+
30+
--------------------------------------------------------------------------
31+
32+
import numpy as np
33+
34+
35+
def calculate(N,M):
36+
37+
res = [0]*M
38+
for i in range(M):
39+
temp = np.random.normal(0,1,N).tolist()
40+
temp = sorted(temp)
41+
42+
res[i] = temp[5]-temp[6]
43+
44+
res = np.mean(res)
45+
print(res)
46+
return res
47+
48+
49+
calculate(10,20)

0 commit comments

Comments
 (0)