├── README.md ├── soln1.py └── soln2.py /README.md: -------------------------------------------------------------------------------- 1 | # Hackerrank-Rest-API 2 | Python solution for Hackerrank REST API Certification (Intermediate) 3 | -------------------------------------------------------------------------------- /soln1.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | 10 | 11 | # 12 | # Complete the 'getTotalGoals' function below. 13 | # 14 | # The function is expected to return an INTEGER. 15 | # The function accepts following parameters: 16 | # 1. STRING team 17 | # 2. INTEGER year 18 | # 19 | 20 | import requests 21 | import json 22 | 23 | def getTotalGoals(team, year): 24 | url1 = "https://jsonmock.hackerrank.com/api/football_matches?year=" + str(year) + "&team1=" + team 25 | url2 = "https://jsonmock.hackerrank.com/api/football_matches?year=" + str(year) + "&team2=" + team 26 | response1 = requests.get(url1) 27 | result1 = json.loads(response1.content) 28 | response2 = requests.get(url2) 29 | result2 = json.loads(response2.content) 30 | # print(result1) 31 | # print(result2) 32 | curr_1 = 1 33 | total_page_url_1 = result1['total_pages'] 34 | curr_2 = 1 35 | total_page_url_2 = result2['total_pages'] 36 | 37 | total = 0 38 | while curr_1 <= total_page_url_1: 39 | url1 = "https://jsonmock.hackerrank.com/api/football_matches?year={0}&team1={1}&page={2}".format(year,team,curr_1) 40 | response1 = requests.get(url1) 41 | result1 = json.loads(response1.content) 42 | for i in result1['data']: 43 | if i['team1'].upper() == team.upper(): 44 | total += int(i['team1goals']) 45 | curr_1 += 1 46 | # print(total) 47 | 48 | while curr_2 <= total_page_url_2: 49 | url2 = "https://jsonmock.hackerrank.com/api/football_matches?year={0}&team2={1}&page={2}".format(year,team,curr_2) 50 | response2 = requests.get(url2) 51 | result2 = json.loads(response2.content) 52 | for i in result2['data']: 53 | if i['team2'].upper() == team.upper(): 54 | total += int(i['team2goals']) 55 | curr_2 += 1 56 | return total 57 | 58 | 59 | # while current_page_url_1 <= total_page_url_1: 60 | # toal 61 | if __name__ == '__main__': -------------------------------------------------------------------------------- /soln2.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | 10 | 11 | # 12 | # Complete the 'getNumDraws' function below. 13 | # 14 | # The function is expected to return an INTEGER. 15 | # The function accepts INTEGER year as parameter. 16 | # 17 | import requests 18 | import json 19 | 20 | def getNumDraws(year): 21 | url1 = "https://jsonmock.hackerrank.com/api/football_matches?year=" + str(year) 22 | response1 = requests.get(url1) 23 | result1 = json.loads(response1.content) 24 | curr_1 = 1 25 | total_page_url_1 = result1['total_pages'] 26 | total = 0 27 | for i in range(0,12): 28 | url1 = "https://jsonmock.hackerrank.com/api/football_matches?year={0}&team1goals={1}&team2goals={1}".format(year,i,i) 29 | response1 = requests.get(url1) 30 | result1 = json.loads(response1.content) 31 | print(result1['total']) 32 | total += result1['total'] 33 | return total 34 | if __name__ == '__main__': --------------------------------------------------------------------------------