Skip to content

Instantly share code, notes, and snippets.

@AgungPambudi
Last active July 3, 2022 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AgungPambudi/c9008dc1af0bb3ea9544e336f8eddedc to your computer and use it in GitHub Desktop.
Save AgungPambudi/c9008dc1af0bb3ea9544e336f8eddedc to your computer and use it in GitHub Desktop.
"""
file: Football Competition Winner's Goals.py
language: python3
author: mail@agungpambudi.com Agung Pambudi
description: REST API: Football Competition Winner's Goals
"""
#!/bin/python3
#
# Complete the 'getWinnerTotalGoals' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. STRING competition
# 2. INTEGER year
#
import requests
import json
import os
def getWinnerTotalGoals(competition, year):
# Write your code here
goals = 0
url = 'https://jsonmock.hackerrank.com/api/football_competitions?name=' + competition + '&year=' + str(year)
response = requests.request('GET', url, headers={}, data={})
winner = json.loads(response.text.encode('utf8'))['data'][0]['winner']
for team in ['team1', 'team2']:
url = 'https://jsonmock.hackerrank.com/api/football_matches?competition=' + competition + '&year=' + str(year) + '&' + team + '=' + winner + '&page=1'
response = requests.request('GET', url, headers={}, data={})
total_pages = json.loads(response.text.encode('utf8'))['total_pages']
for i in range(1, total_pages + 1):
url = 'https://jsonmock.hackerrank.com/api/football_matches?competition=' + competition + '&year=' + str(year) + '&'+ team +'=' + winner + '&page=' + str(i)
response = requests.request('GET', url, headers={}, data={})
r = json.loads(response.text.encode('utf8'))
r_data = r['data']
for record in r_data:
goals += int(record[team + 'goals'])
return goals
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
competition = input()
year = int(input().strip())
result = getWinnerTotalGoals(competition, year)
fptr.write(str(result) + '\n')
fptr.close()
"""
file: Total Goals by a Team.py
language: python3
author: mail@agungpambudi.com Agung Pambudi
description: REST API: Total Goals by a Team
"""
#!/bin/python3
import math
import os
import random
import re
import sys
import requests
import json
#
# Complete the 'getTotalGoals' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. STRING team
# 2. INTEGER year
#
def getTotalGoals(team, year):
# Write your code here
first_url = "https://jsonmock.hackerrank.com/api/football_matches?year=" + str(year) + "&team1=" + team
second_url = "https://jsonmock.hackerrank.com/api/football_matches?year=" + str(year) + "&team2=" + team
first_response = requests.get(first_url)
second_response = requests.get(second_url)
first_result = json.loads(first_response.content)
second_result = json.loads(second_response.content)
first_position = 1
second_position = 1
total_page_url_1 = first_result['total_pages']
total_page_url_2 = second_result['total_pages']
total = 0
while first_position <= total_page_url_1:
first_url = "https://jsonmock.hackerrank.com/api/football_matches?year={0}&team1={1}&page={2}".format(year, team, first_position)
first_response = requests.get(first_url)
first_result = json.loads(first_response.content)
for i in first_result['data']:
if i['team1'].upper() == team.upper():
total += int(i['team1goals'])
first_position += 1
while second_position <= total_page_url_2:
second_url = "https://jsonmock.hackerrank.com/api/football_matches?year={0}&team2={1}&page={2}".format(year, team, second_position)
second_response = requests.get(second_url)
second_result = json.loads(second_response.content)
for i in second_result['data']:
if i['team2'].upper() == team.upper():
total += int(i['team2goals'])
second_position += 1
return total
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
team = input()
year = int(input().strip())
result = getTotalGoals(team, year)
fptr.write(str(result) + '\n')
fptr.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment