In: Computer Science
Game CSV Sum with Map
Chuchu and Xyz are playing a game. But this time they've invited their friends!
They've written down their scores in a String like this:
Xyz, 10
Chuchu,5
Sheldon,6
Xyz, 20
Lulu,8
Xyz, 4
The winner of the game is the player with the most total points. For the game above, the winner is Xyz, since she scored 34 points to Chuchu's 5, Sheldon's 6, and Lulu's 8. Note that zero is a valid number of points to have scored and should not be treated separately.
Write a function called winner that determines the winner of the game. It accepts a single String argument containing CSV records as shown above and should return a String containing the winner. If any two players earn the same score you should return "Tie".
A few notes. First, you'll want to use both split and trim, two of our familiar functions for working with String data. Second, while each line will contain a record, there may be extra whitespace surrounding each record. (The data was entered by happy animals.) Third, recall that you can use Integer.parseInt to convert a String to an int. Fourth, if the String passed is null or contains no lines, you should return null.
Finally, unlike the previous version of this problem Chuchu and Xyz are not the only players! They have many friends, some with strange names. So the participants will be different from game to game.
This is a great problem to solve using a map. Here's a solution approach:
You may need to examine the map documentation. But as a helpful note, the keySet Map function will return all of the keys in the map, which you can iterate over:
for (String player : map.keySet()) {
// Etc
}
The solution to this in java is as follows:-
Code-
import java.io.*;
import java.util.*;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Game{
public static String winner(String records){
//Checking is records are
null
if(records.isNull())
return "";
String
[]recordList=records.split("\n"); //Creating a list of
records
//For storing the records as a
map
Map<String, Integer> map =
new HashMap<String, Integer>();
//Iterate over the record list and
put them in a map
for(String
record:recordList){
String[]
data=record.split(",");
String
name=data[0].trim();
int
points=Integer.parseInt(data[1].trim());
if
(map.containsKey(name))
{
map.put(name, map.get(name) + points);
}
else
{
map.put(name, points);
}
}
//Find the winner based on
points
String winner="";
int maxpoints=-1;
//Find the maximum points of a
player in the map
for (Map.Entry<String,
Integer> entry : map.entrySet())
{
if(entry.getValue()>maxpoints){
maxpoints=entry.getValue();
winner=entry.getKey();
}
}
//Find the number of
winners with maximum score
int
countOfWinners=0;
for
(Map.Entry<String, Integer> entry : map.entrySet())
{
if(entry.getValue()==maxpoints){
countOfWinners++;
}
}
if(countOfWinners==1)
//If there is one winner
return
winner;
else
//If there are multiple winners
return
"Tie";
}
public static void main(String argc[]) throws
IOException{
//Reading contents of the csv
file
String content = new
String(Files.readAllBytes(Paths.get("list.csv")));
System.out.println("Winner of the
game: "+winner(content));
}
}
Code Screenshots-
Outputs-
Feel free to comment for any issues, do rate the answer positively