-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_1_Quartiles.py
54 lines (44 loc) · 1.38 KB
/
day_1_Quartiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'quartiles' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts INTEGER_ARRAY arr as parameter.
#
def quartiles(arr, level):
# Write your code here
#At level 0, It checks for the whole array to be sorted
arr = sorted(arr) if level==0 else arr
#To check length of the array is odd or even, If Odd - this flag will be TRUE.
odd = True if len(arr)%2!=0 else False
idx_q2 = ((len(arr)+1)/2)-1
res = []
level+=1
if level==2:
if odd:
return int(arr[int(idx_q2)])
else:
return int((arr[int(idx_q2)]+arr[int(math.ceil(idx_q2))])/2)
else:
if odd:
res.append(quartiles(arr[:int(idx_q2)], level))
res.append(arr[int(idx_q2)])
res.append(quartiles(arr[int(idx_q2+1):], level))
else:
res.append(quartiles(arr[:int(math.ceil(idx_q2))], level))
res.append(quartiles(arr, level))
res.append(quartiles(arr[int(math.ceil(idx_q2)):], level))
return res
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input().strip())
data = list(map(int, input().rstrip().split()))
res = quartiles(data, 0)
fptr.write('\n'.join(map(str, res)))
fptr.write('\n')
fptr.close()