-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1_interquartile_range.py
53 lines (43 loc) · 1.36 KB
/
day1_interquartile_range.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
#!/bin/python3
import math
import os
import random
import re
import sys
"""
Task:
The interquartile range of an array is the difference between its first (Q1) and third (Q3) quartiles (i.e., Q3-Q1).
Given an array, values, of n integers and an array, freqs, representing the respective frequencies of values's elements,
construct a data set, S, where each values[i] occurs at frequency freqs[i]. Then calculate and print S's interquartile range,
rounded to a scale of 1 decimal place (i.e., 12.3 format).
"""
#
# Complete the 'interQuartile' function below.
#
# The function accepts following parameters:
# 1. INTEGER_ARRAY values
# 2. INTEGER_ARRAY freqs
#
def median(arr : list, n) -> int:
if n % 2 == 0:
return (arr[int(n/2)-1]+arr[int(n/2)])/2
return arr[int((n-1)/2)]
def interQuartile(values, freqs):
S = []
for i in range(len(values)):
# appending
for time_appending in range(freqs[i]):
S.append(values[i])
if len(S) % 2 == 0:
mid = len(S)//2
else:
mid = (len(S)//2)+1
S.sort()
med_iQ1 = median(S[:mid], len(S[:mid]))
med_iQ3 = median(S[mid:], len(S[mid:]))
print(round(float(med_iQ3-med_iQ1), 1))
if __name__ == '__main__':
n = int(input().strip())
val = list(map(int, input().rstrip().split()))
freq = list(map(int, input().rstrip().split()))
interQuartile(val, freq)