Question

In: Computer Science

Game CSV Sum with Map Chuchu and Xyz are playing a game. But this time they've...

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:

  1. Parse the CSV and combine all the scores into a map, totaling the score for each player as you go
  2. Loop over the map to compute the maximum

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

}

Solutions

Expert Solution

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


Related Solutions

The average time spent playing one competitive game in Counter Strike is μ = 50 minutes...
The average time spent playing one competitive game in Counter Strike is μ = 50 minutes and standard deviation σ = 7 minutes. I didn’t believe it takes that long so I went ahead and played 100 competitive games for my sample and had an average time of 51 minutes. Use α = 0.05. Can I conclude that the average time spent playing competitive games in Counter Strike is any different that 50 minutes? You can use either Critical Value...
Provide an example of a strictly competitive game that is not a zero-sum game.
Provide an example of a strictly competitive game that is not a zero-sum game.
Al and Bo are playing a game. There is a pot of money in front of...
Al and Bo are playing a game. There is a pot of money in front of them, and they each will take turn to decide if they will take the money or pass. If either player decides to take the money, the game is over, and the other player will have nothing. If he chooses to pass, the game will continue and the pot of money will grow, but it will be the other player’s turn to decide if he...
What are the penalties for not playing the "doing being ordinary" game?
What are the penalties for not playing the "doing being ordinary" game?
Question 1 - Game Theory (7 Marks) Alice and Bob are playing a game of simultaneous...
Question 1 - Game Theory Alice and Bob are playing a game of simultaneous moves where they are deciding which concert they will attend on Saturday night. Two musicians are performing that night locally, Artist 1 and Artist 2. Alice prefers to go to Artist 1 with Bob rather than Artist 2 with Bob, and she prefers going to Artist 2 with Bob rather than going to Artist 2 alone. Moreover, she prefers going to Artist 1 alone rather than...
Tanya is playing PokemonGo, and searching for a Snorlax. The game is programmed such that pokemon...
Tanya is playing PokemonGo, and searching for a Snorlax. The game is programmed such that pokemon are generated randomly, and there is a 7% chance that a Snorlax will appear. Tanya will search Mill Creek park until she captures 473 random pokemon. Consider the proportion in her sample that will be Snorlaxes. As we have learned, the sample proportion is a random quantity. What type of random variable can we use to approximate the sample proportion? Type the name of...
The Rams are playing the Aggies in the last conference game of the season. The Rams...
The Rams are playing the Aggies in the last conference game of the season. The Rams are trailing the Aggies 21 to 14 with 7 seconds left in the game, when they score a touchdown. Still trailing 21 to 20, the Rams can either go for two points and win or go for one point to send the game into overtime. The conference championship will be determined by the outcome of this game. If the Rams win, they will go...
Consider the missile allocation problem (MAP) with discretized time. (a) For MAP, an extreme case may...
Consider the missile allocation problem (MAP) with discretized time. (a) For MAP, an extreme case may be to maximize the probability of shooting down only those ASMs targeting the high value ships, ignoring the rest. How can you modify MAP model to accomplish that situation? (b) The probability of no leaker may be a very small figure, when there is a large number of attacking ASMs. For such cases, modify the objective function for maximizing the expected number of ASMs...
Consider a following zero-sum game of two players a) Reduce the initial game to a 2x2...
Consider a following zero-sum game of two players a) Reduce the initial game to a 2x2 game. Eliminate only strictly dominated strategies. In the obtained 2x2 game name Player 1’s (Row player’s) strategies “Up” and “Down” and Player 2’s (Column player’s) strategies “Left” and “Right”. b) Find all Nash equilibria of the 2x2 game (both in pure and mixed strategies) ALL ANSWERS MUST BE EXPLAINED. 2 0 1 -1 1 0 1 2 3 1 2 0
Consider a following zero-sum game of two players a) Reduce the initial game to a 2x2...
Consider a following zero-sum game of two players a) Reduce the initial game to a 2x2 game. Eliminate only strictly dominated strategies. In the obtained 2x2 game name Player 1’s (Row player’s) strategies “Up” and “Down” and Player 2’s (Column player’s) strategies “Left” and “Right”. b) Find all Nash equilibria of the 2x2 game (both in pure and mixed strategies) ALL ANSWERS MUST BE EXPLAINED. 2          0          1          -1 1          0          1          2 3...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT