Please solve the Python problem. It's hard

Asked 2 years ago, Updated 2 years ago, 302 views

The company regularly conducts medical checkups for employees every year to measure blood pressure, and the blood pressure levels include systolic blood pressure (high) and diastolic blood pressure (low). The company measures two blood pressure values for all employees and calculates and outputs the mean and median values of all employees' systolic and diastolic blood pressure.

The center value of a numeric string is the value that the numbers are centered in order. For example, 3,7,1,9,5 has a median of 5 The median values of 5, 8, 2, and 6 are the average values of 5 and 6. (Round from decimal point unconditionally).

Help complete the split - range() function to complete the median calculation for both blood pressure in the employee. (Note: This program does not use sorting to find the median.)

Input format:

n: Number of employees ha1, …, han: systolic blood pressure in the staff hb1, …, hbn: diastolic blood pressure in staff

Output type: ha_avg : Employee average systolic blood pressure hb_avg : Employee average diastolic blood pressure ha_md : median systolic blood pressure in the employee hb_md : median diastolic blood pressure of the employee

Conditions def split_range (pivot_position, mid, left, right): Function

pivot_position: split point position

mid: intermediate position

left: align left end

right: align right end

This function gradually narrows the search based on the result of calling the split function (pivot_position).

def split_range(pivot_position,mid,left,right):

Sample Materials:

1.

input

5

139 93 121 142 116

68 89 58 67 83

print

122

73

121

68

2.

input

4

125 132 130 108

96 67 82 96

print

123

85

127

89

3.

input

3

149 147 119

77 68 70

print

138

71

147

70

4.

input

10

108 140 141 132 140 100 116 116 94 90

89 55 84 77 79 91 74 72 85 99

print

117

80

116

81

5.

input

1

134

55

print

134

55

134

55


def average(data, n):
avg =0
for x in data:
avg += x
return avg//n
def split(data, left, right):
pivot = data[left]     
pivot_position = left
for i in range(left+1,right+1):
if data[i] < pivot:
pivot_position += 1
data[i], data[pivot_position] = data[pivot_position], data[i]
data[left], data[pivot_position] = data[pivot_position], data[left]
return pivot_position
def split_range(pivot_position, mid, left, right):


???????????????????????????????????????????????????????????????????????????????


def half(data, n):
value = data[n//2]
for i in range(n//2,n):
if data[i] < value:
value = data[i]
return value
def median(data, n):
left = 0
right = n-1
mid = (left+right)//2
while(True):
pivot_position = split(data, left, right)
if pivot_position == mid:
break
else:
left,right = split_range(pivot_position, mid, left, right)
if n%2 == 1:
return data[mid]
else:
return (data[mid] + half(data, n))//2
num = int(input())
systolic = list(map(int,input().split()))
diastolic = list(map(int,input().split()))
sy_average = average(systolic, num)
dia_average = average(diastolic, num)
sy_median = median(systolic, num)
dia_median = median(diastolic, num)
print(sy_average, dia_average, sy_median, dia_median,sep='\n')

What code should I write in the middle question mark? crying Please answer the question If you can, please interpret it Thank you as always!

python programming

2022-10-20 00:01

0 Answers

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.