0% found this document useful (0 votes)
100 views

Rand

The document defines classes to represent matches, match data, and competition information retrieved from APIs. It includes a getWinnerTotalGoals method that takes a competition and year as parameters. It makes API calls to get the winner and iterate through match pages to sum the total goals scored by the winner.

Uploaded by

Dj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

Rand

The document defines classes to represent matches, match data, and competition information retrieved from APIs. It includes a getWinnerTotalGoals method that takes a competition and year as parameters. It makes API calls to get the winner and iterate through match pages to sum the total goals scored by the winner.

Uploaded by

Dj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

using System.CodeDom.

Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;

using Newtonsoft.Json;

class Result
{

/*
* Complete the 'getWinnerTotalGoals' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. STRING competition
* 2. INTEGER year
*/

public class Match {


public string team1 {get; set;}
public string team2 {get; set;}
public int team1goals {get; set;}
public int team2goals {get; set;}
}

public class Data {


public int page {get; set;}
public int per_page {get; set;}
public int total {get; set;}
public int total_pages {get; set;}
public List<Match> data {get; set;}
}

public class matchInfo {


public string name {get; set;}
public string country {get; set;}
public string year {get; set;}
public string winner {get; set;}
public string runnerup {get; set;}
}
public static int getWinnerTotalGoals(string competition, int year)
{
string winner;
int pages = 0, store = 0;
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage res =
client.GetAsync($"https://jsonmock.hackerrank.com/api/football_competitions?
name={competition}&year={year}").Result)
{
using (HttpContent content = res.Content)
{
string r = content.ReadAsStringAsync().Result;
Console.WriteLine(r);
var answer = JsonConvert.DeserializeObject<matchInfo>(r);
winner = answer.winner;
}
}
}

using (HttpClient client = new HttpClient())


{
using (HttpResponseMessage res =
client.GetAsync($"https://jsonmock.hackerrank.com/api/football_matches?
competition={competition}&year={year}&team1={winner}&page={1}").Result)
{
using (HttpContent content = res.Content)
{
string r = content.ReadAsStringAsync().Result;

var answer = JsonConvert.DeserializeObject<Data>(r);


pages = answer.total_pages;

}
}
}

for(int i = 0; i < pages; i++) {

using (HttpClient client = new HttpClient())


{
using (HttpResponseMessage res =
client.GetAsync($"https://jsonmock.hackerrank.com/api/football_matches?
competition={competition}&year={year}&team1={winner}&page={i}").Result)
{
using (HttpContent content = res.Content)
{
string r = content.ReadAsStringAsync().Result;

var answer = JsonConvert.DeserializeObject<Data>(r);


store += answer.data.Sum((c)=> c.team1goals);

}
}
}

using (HttpClient client = new HttpClient())


{
using (HttpResponseMessage res =
client.GetAsync($"https://jsonmock.hackerrank.com/api/football_matches?
competition={competition}&year={year}&team2={winner}&page={i}").Result)
{
using (HttpContent content = res.Content)
{
string r = content.ReadAsStringAsync().Result;

var answer = JsonConvert.DeserializeObject<Data>(r);


store += answer.data.Sum((c)=> c.team2goals);

}
}
}

return store;
}

class Solution
{
public static void Main(string[] args)
{
TextWriter textWriter = new
StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

string competition = Console.ReadLine();

int year = Convert.ToInt32(Console.ReadLine().Trim());

int result = Result.getWinnerTotalGoals(competition, year);

textWriter.WriteLine(result);

textWriter.Flush();
textWriter.Close();
}
}

You might also like