반응형
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
const axios = require('axios');
const api = 'https://jsonmock.hackerrank.com/api'
async function getTotalGoals(team, year) {
let goals = 0;
let totalPages;
let nowPage = 1;
// Get team1 goals
const team1Response = await axios.get(`${api}/football_matches?year=${year}&team1=${team}`);
totalPages = team1Response.data.total_pages;
while (nowPage <= totalPages) {
const response = await axios.get(`${api}/football_matches?year=${year}&team1=${team}&page=${nowPage}`);
goals += response.data.data.reduce((total, match) => total + parseInt(match.team1goals), 0);
nowPage++;
}
// Get team2 goals
nowPage = 1;
const team2Response = await axios.get(`${api}/football_matches?year=${year}&team2=${team}`);
totalPages = team2Response.data.total_pages;
while (nowPage <= totalPages) {
const response = await axios.get(`${api}/football_matches?year=${year}&team2=${team}&page=${nowPage}`);
goals += response.data.data.reduce((total, match) => total + parseInt(match.team2goals), 0);
nowPage++;
}
return goals;
}
async function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const team = readLine();
const year = parseInt(readLine().trim(), 10);
const result = await getTotalGoals(team, year);
ws.write(result + '\n');
ws.end();
}
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
const axios = require('axios');
const api = 'https://jsonmock.hackerrank.com/api'
async function getWinnerTotalGoals(competition, year) {
const result = await axios.get(`${api}/football_competitions?year=${year}&name=${competition}`);
const winner = result.data.data[0].winner;
let goals = 0;
let totalPages = 0;
let nowPage = 1;
const team1Response = await axios.get(`${api}/football_matches?year=${year}&team1=${winner}&competition=${competition}`);
totalPages = team1Response.data.total_pages;
while (nowPage <= totalPages) {
const response = await axios.get(`${api}/football_matches?year=${year}&team1=${winner}&competition=${competition}&page=${nowPage}`);
goals += response.data.data.reduce((total, match) => total + parseInt(match.team1goals), 0);
nowPage++;
}
nowPage = 1;
const team2Response = await axios.get(`${api}/football_matches?year=${year}&team2=${winner}&competition=${competition}`);
totalPages = team2Response.data.total_pages;
while (nowPage <= totalPages) {
const response = await axios.get(`${api}/football_matches?year=${year}&team2=${winner}&competition=${competition}&page=${nowPage}`);
goals += response.data.data.reduce((total, match) => total + parseInt(match.team2goals), 0);
nowPage++;
}
return goals;
}
async function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const competition = readLine();
const year = parseInt(readLine().trim(), 10);
const result = await getWinnerTotalGoals(competition, year);
ws.write(result + '\n');
ws.end();
}
'알고리즘' 카테고리의 다른 글
[프로그래머스] Lv.3 인사고과 (JS) (0) | 2023.07.01 |
---|---|
[백준] 14243 출근기록2 (C++) (0) | 2023.05.15 |
[프로그래머스] LV.5 상품을 구매한 회원 비율 구하기 (MySQL) (0) | 2023.04.21 |
[백준] 17298 오큰수 (C++) (0) | 2023.04.18 |
[프로그래머스] LV.3 숫자게임 (JAVA) (0) | 2023.04.18 |