'Can we put condition on REST API
I was giving hackerrank test where I got this problem.
Problem was to find number of football matches that are draw. i.e data[index]['team1goals']==data[index]['team2goals']
Here is an API you can play with it: https://jsonmock.hackerrank.com/api/football_matches?year=2011&page=1
This is what I tried:
import requests
year = 2011
draw = 0
r = requests.get('https://jsonmock.hackerrank.com/api/football_matches?year='+str(year)+'&page=1').json()
total_pages = r['total_pages']
per_page = r['per_page']
for page in range(1, total_pages+1):
r = requests.get('https://jsonmock.hackerrank.com/api/football_matches?year='+str(year)+'&page='+str(page)).json()
try:
for i in range(0, per_page):
if int(r['data'][i]['team1goals']) == int(r['data'][i]['team2goals']):
draw += 1
except:
pass
print(draw) #516
It is giving me correct answer. Since the data was big, it is facing time complexity which I don't want
Is it possible, Can we modify the REST API with condition like this:
https://jsonmock.hackerrank.com/api/football_matches?year=2011&team1goals==team2goals&page=1
OR
https://jsonmock.hackerrank.com/api/football_matches?year=2011&team1goals-gt-lt&team2goals&page=1
Solution 1:[1]
If the API allows these many calls, you can use a multiprocessing.pool.Pool function and iterate through each page parallelly to reduce time. This should work:
import requests
from functools import partial
from multiprocessing.pool import Pool
def loop(page,year,r,per_page):
r = requests.get('https://jsonmock.hackerrank.com/api/football_matches?year='+str(year)+'&page='+str(page)).json()
try:
for i in range(0, per_page):
if int(r['data'][i]['team1goals']) == int(r['data'][i]['team2goals']):
increase = 1
else:
increase = 0
except:
increase = 0
return increase
if __name__ == "__main__":
year = 2011
draw = []
r = requests.get('https://jsonmock.hackerrank.com/api/football_matches?year='+str(year)+'&page=1').json()
total_pages = r['total_pages']
per_page = r['per_page']
pages = range(1, total_pages+1)
pool = Pool()
f = pool.map(partial(loop,year=year,r=r,per_page=per_page),pages)
draw += f
final = 0
for x in draw:
x = int(x)
final += x
print(final) #516
Solution 2:[2]
You should use multithreading and make multiple requests in parallel.
Solution 3:[3]
You can do it other way.
def getNumDraws(year):
counter = 0
for z in range(0, 10):
# 10 - maximum goals, it's in the description of this task
link = f"https://jsonmock.hackerrank.com/api/football_matches?year={year}&team1goals={z}&team2goals={z}"
r = requests.get(link)
counter = counter + int(r.json()['total'])
return counter
Steps:
- Get
['total']from link where you add&team1goals={variable}&team2goals={variable} - Add
['total']number to your counter. - Repeat 10 times. 10 times - because it's from task description that you can assume that there are 10 maximum goals scored.
So you call an api only 10 times
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Prateek Jain |
| Solution 2 | Leri Gogsadze |
| Solution 3 | Miguel Conde |
