-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREST_API_patiens_medical_record.py
More file actions
61 lines (46 loc) · 1.42 KB
/
REST_API_patiens_medical_record.py
File metadata and controls
61 lines (46 loc) · 1.42 KB
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
55
56
57
58
59
60
61
#!/bin/python3
import math
import os
import random
import re
import sys
import requests
import json
#
# Complete the 'getAverageTemperatureForUser' function below.
#
# URL for cut and paste
# https://jsonmock.hackerrank.com/api/medical_records?userId=<userId>&page=<page>
#
# The function is expected to return a String value.
# The function accepts a userId argumnent (Integer).
# In the case of an empty array result, return value '0'
#
def getAverageTemperatureForUser(userId):
base_url = "https://jsonmock.hackerrank.com/api/medical_records"
page = 1
temperatures = []
while True:
url = f"{base_url}?userId={userId}&page={page}"
res = requests.get(url)
data = res.json()
# print(data)
records = data.get('data', [])
for record in records:
temp = record.get('vitals', {}).get('bodyTemperature')
if temp is not None:
temperatures.append(temp)
if page >= data.get('total_pages', 0):
break
page += 1
if not temperatures:
return '0'
avg_temp = sum(temperatures) / len(temperatures)
return f"{avg_temp:.1f}"
if __name__ == '__main__':
# fptr = open(os.environ['OUTPUT_PATH'], 'w')
# userId = int(input().strip())
# result = getAverageTemperatureForUser(userId)
# fptr.write(result + '\n')
# fptr.close()
print(getAverageTemperatureForUser(1))