Skip to content

Instantly share code, notes, and snippets.

@ABHISHEK-KEDAR-21
Created February 2, 2021 10:01
Show Gist options
  • Save ABHISHEK-KEDAR-21/152bddc6b3213986cd8989890ce0e528 to your computer and use it in GitHub Desktop.
Save ABHISHEK-KEDAR-21/152bddc6b3213986cd8989890ce0e528 to your computer and use it in GitHub Desktop.
REST API HackerRank
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'getTotalGoals' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. STRING team
# 2. INTEGER year
#
import requests
import json
def getTotalGoals(team, year):
# Write your code here
res=0
r1=requests.get(f'https://jsonmock.hackerrank.com/api/football_matches?year={year}&team1={team}&page=1&per_page=100')
if r1.status_code==200:
#print(r1.content)
c1=r1.content
d1=json.loads(c1)
t1_total_pages=d1['total_pages']
#print(t1_total_pages)
t1=d1['data']
for i in t1:
res+=int(i['team1goals'])
#print(d1)
if int(t1_total_pages)!=1:
for j in range(1,int(t1_total_pages)):
j=j+1
#print(j)
r1=requests.get(f'https://jsonmock.hackerrank.com/api/football_matches?year={year}&team1={team}&page={j}')
if r1.status_code==200:
#print(r1.content)
c1=r1.content
d1=json.loads(c1)
t1_total_pages=d1['total_pages']
#print(t1_total_pages)
t1=d1['data']
for i in t1:
res+=int(i['team1goals'])
r2=requests.get(f'https://jsonmock.hackerrank.com/api/football_matches?year={year}&team2={team}&page=1')
if r2.status_code==200:
#print(r1.content)
c2=r2.content
d2=json.loads(c2)
t2_total_pages=d2['total_pages']
#print(t2_total_pages)
t2=d2['data']
for i in t2:
res+=int(i['team2goals'])
#print(d2)
if int(t2_total_pages)!=1:
for j in range(1,int(t2_total_pages)):
j=j+1
#print(j)
r2=requests.get(f'https://jsonmock.hackerrank.com/api/football_matches?year={year}&team2={team}&page={j}')
if r2.status_code==200:
#print(r1.content)
c2=r2.content
d2=json.loads(c2)
t2_total_pages=d2['total_pages']
#print(t2_total_pages)
t2=d2['data']
for i in t2:
res+=int(i['team2goals'])
#print(d2)
return res
if __name__ == '__main__':
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'getNumDraws' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER year as parameter.
#
import requests
import json
def getNumDraws(year):
# Write your code here
res=0
for g in range(10):
goals=g
r=requests.get(f'https://jsonmock.hackerrank.com/api/football_matches?year={year}&page=1&team1goals={goals}&team2goals={goals}')
if r.status_code==200:
c=r.content
d=json.loads(c)
total_pages=int(d['total_pages'])
#print('goal total pages',total_pages)
data=d['data']
for i in data:
if i['team1goals']==i['team2goals']:
res+=1
#print(d)
if total_pages>1:
for j in range(1,total_pages):
j=j+1
r=requests.get(f'https://jsonmock.hackerrank.com/api/football_matches?year={year}&page={j}&&team1goals={goals}&team2goals={goals}')
if r.status_code==200:
c=r.content
d=json.loads(c)
total_pages=int(d['total_pages'])
# print('')
data=d['data']
for i in data:
if i['team1goals']==i['team2goals']:
res+=1
return res
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment