Problem Solving/📘HackerRank
[HackerRank] Total Goals by a Team (Python)
seungwookim
2021. 11. 23. 16:27
📖 문제
https://www.hackerrank.com/skills-verification/rest_api_intermediate
💻 코드
import requests
def getHomeTotalPages(team, year):
response = requests.get(
"https://jsonmock.hackerrank.com/api/football_matches?year=%d&team1=%s&page=1" % (year, team))
return response.json()["total_pages"]
def getHomeTotalGoals(team, year):
total_goals = 0
total_pages = getHomeTotalPages(team, year)
for page in range(1, total_pages + 1):
response = requests.get(
"https://jsonmock.hackerrank.com/api/football_matches?year=%d&team1=%s&page=%d" % (year, team, page))
for play in response.json()["data"]:
total_goals += int(play["team1goals"])
return (total_goals)
def getAwayTotalPages(team, year):
response = requests.get(
"https://jsonmock.hackerrank.com/api/football_matches?year=%d&team2=%s&page=1" % (year, team))
return response.json()["total_pages"]
def getAwayTotalGoals(team, year):
total_goals = 0
total_pages = getAwayTotalPages(team, year)
for page in range(1, total_pages + 1):
response = requests.get(
"https://jsonmock.hackerrank.com/api/football_matches?year=%d&team2=%s&page=%d" % (year, team, page))
for play in response.json()["data"]:
total_goals += int(play["team2goals"])
return (total_goals)
def getTotalGoals(team, year):
return getHomeTotalGoals(team, year) + getAwayTotalGoals(team, year)