본문 바로가기
알고리즘

[HackerRank] RestAPI Certificate

by kigo23 2023. 10. 16.
반응형
'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();
}