Question

In: Computer Science

1 ABA, 3 2 DCD, 6 3 YMC, 10 4 DCD, 8 5 POP, 4 Create...

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.

Solutions

Expert Solution

/* 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);
        
    }


}

Related Solutions

tens Units 1 5 2 3 4 8 5 2 5 6 9 6 1 3...
tens Units 1 5 2 3 4 8 5 2 5 6 9 6 1 3 5 4 7 9 7 0 0 4 5 6 9 9 8 1 3 5 6 8 9 9 0 1 2 3 5 9 The table represent a random sample of 31 test scores taken from a large lecture class. Find the following [round to 2 decimal points X. XX] a) [2 pts] Find the 5 number summary [L, Q1, Q2, Q3,...
A = (1 −7 5 0 0 10 8 2 2 4 10 3 −4 8...
A = (1 −7 5 0 0 10 8 2 2 4 10 3 −4 8 −9 6) (1) Count the number of rows that contain negative components. (2) Obtain the inverse of A and count the number of columns that contain even number of positive components. (3) Assign column names (a,b,c,d) to the columns of A. (4) Transform the matrix A into a vector object a by stacking rows. (5) Replace the diagonal components of A with (0,0,2,3). Hint:...
3 6 4 8 1 10 2 9 11 12 15 22 3 6 7 5...
3 6 4 8 1 10 2 9 11 12 15 22 3 6 7 5 8 1 12 14 Each column represents a different treatment given to sick rats. Each cell is a different rat. Use statistical analysis and use post hoc testing using contrasts to find the best treatment. Treatment 1: vitamins Treatment 2: prescription pills Treatment 3: brain surgery Treatment 4: shock therapy Treatment 5: dietary changes
PROBLEM 5. A box contains 10 tickets labeled 1, 2, 3, 4, 5, 6, 7, 8,...
PROBLEM 5. A box contains 10 tickets labeled 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. Draw four tickets and find the probability that the largest number drawn is 8 if: (a) the draws are made with replacement. (b) the draws are made without replacement. PROBLEM 6. Suppose a bakery mixes up a batch of cookie dough for 1,000 cookies. If there are raisins in the dough, it's reasonable to assume raisins will independently have a .001 chance...
Year 1 2 3 4 5 6 7 8 9 10 A 1 1.01 1.0201 1.0303...
Year 1 2 3 4 5 6 7 8 9 10 A 1 1.01 1.0201 1.0303 1.0406 1.0510 1.0615 1.0721 1.0829 12.0305 B 1 .9900 .9801 .9703 .9606 .9510 .9415 .9321 .9227 10.0487 Assume a purchase price of $10 Million for both properties. (a) What is the expected total return (IRR) on a 10-year investment in each property? Use a financial calculator or equation solver for this. (b) If the 10% cap rate represents a fair market value for each...
2 10 6 10 10 4 5 4 5 8 a) Calculate the range. b) Calculate...
2 10 6 10 10 4 5 4 5 8 a) Calculate the range. b) Calculate the variance. (Round to two decimal places as needed.) c) Calculate the standard deviation. (Round to two decimal places as needed.) 13 28 30 17 24 9 44 21 a​)Determine the 20th percentile. b​)Determine the 30th percentile. c​)Determine the 70th percentile.
3, 7, 8, 5, 6, 4, 9, 10, 7, 8, 6, 5 Using the previous question...
3, 7, 8, 5, 6, 4, 9, 10, 7, 8, 6, 5 Using the previous question 's scores, If three points were added to every score in this distribution, what would be the new mean? If three points were added to every score in this distribution, what would be the new standard deviation. Remember, you have already calculated population standard deviation in a previous problem. This problem requires two answers.
x 2 8 5 9 4 3 9 6 7 8 y 3 6 5 7...
x 2 8 5 9 4 3 9 6 7 8 y 3 6 5 7 9 7 4 6 9 9 -5.48x + 0.17 5.48x + 0.17 -0.17x + 5.48 0.17x + 5.48
1.Consider the program: .data myArray: .word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10...
1.Consider the program: .data myArray: .word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 .text la $s0, myArray li $s1, 0 loop: sll $t0, $s1, 2 add $t0, $t0, $s0 lw $s2, 0($t0) lw $s3, 4($t0) add $s2, $s2, $s3 sw $s2, 0($t0) addi $s1, $s1, 1 slti $t1, $s1, 9 bne $t1, $zero, loop .end Explain what does this program do? How is the data bound from the .data segment to the base address register $s0? What...
Match No. 1 2 3 4 5 6 7 8 9 10 11 12 13 14...
Match No. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Player A 8 42 56 68 91 123 12 46 57 137 5 80 14 10 19 Player B 38 44 46 59 57 61 48 42 51 39 58 41 55 45 68 1. For the given data set representing the runs scored by two players in last 15 matches, conduct the following analysis: i. Which average you will use to summarize...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT