In: Computer Science
1 ABA, 3
2 DCD, 6
3 YMC, 10
4 DCD, 8
5 POP, 4
Create a public class with a public static method called winner. It accepts a single String argument containing CSV records like above and should return a String containing the winner. If two players earn the maximum score you should return "Tie". The winner is the player with the maximum cumulative points, in this case DCD with 14.
/* Explanation Of the Code-
I have taken input in the form of CSV file as mentioned in the question that all records are given
in CSV format. converted all the records into a single string separated by commas so that it can be
passed as a single string argument to winner method. after that input was taken into hashmap in which
name of the person will be the key and key value will be the cumulative score pf that particular person
and at last returning tie if there is tie between two or more players else returning the winner.
*/
/*Sample test case for better Explanation
1 ABA,3
2 DCD,6
3 YMC,14
4 DCD,8
5 POP,4
6 PAB,22
Now when first entry is made then win variable will contain the name ABA after 2nd entry DCD will
be replaced as the winner after that YMC and in 4ht input there will be a tie between DCD and YMC so
win variable will be updated to Tie but during 6ht entry as score of PAB is greater than 14 i.e. 22 so
win will now contain the name PAB as the winner and it will be returned.
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
public class Game{
public static String winner(String records)
{
//seperate records
String rec_list[] = records.split("\n");
// containing score of each unique individual
HashMap<String,Integer> scores = new HashMap<String,Integer>();
// for each record store in HashMap and calculate maxScorer
int maxScore = 0;
String win = "";
for(String rec: rec_list)
{
// first record is empty, because of the way the string is passed.
if (rec==""){
continue;
}
// Split record with delimeter "," into {rowNo,name,score}
String[] temp;
String delimiter = ",";
temp = rec.split(delimiter);
int rowNo = Integer.parseInt(temp[0]);
String name = temp[1];
int sc = Integer.parseInt(temp[2]);
// Update score if already present
if (scores.get(name)!=null){
scores.put(name, scores.get(name) + sc);
}
else{
scores.put(name, sc);
}
// Check for Tie for every record
if (scores.get(name)==maxScore)
{
maxScore = scores.get(name);
win = "Tie";
}
//Update winner
if (scores.get(name)>maxScore)
{
maxScore = scores.get(name);
win = name;
}
}
// return winner and score from HashMap
return win +" "+ scores.get(win);
}
public static void main(String args[])
{
// master string for storing CSV record with delimeter "\n".
StringBuilder input = new StringBuilder();
//Read CSV File
String line = "";
try
{
BufferedReader br = new BufferedReader(new FileReader("CSVDemo.csv"));
while ((line = br.readLine()) != null)
{
input.append("\n"+line);
}
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
//Pass to winner function
String ans = winner(input.toString());
//Print the winner
System.out.println(ans);
}
}