beyondgrader.com Logo
DemoBrowseAboutTeamLogin

Game CSV Max First

Geoffrey Challen // 2020.9.0

Chuchu and Xyz are playing a game. They are keeping score by adding records to a String, like this:

Xyz, 10
Chuchu,5
Xyz, 20
Xyz, 4
Chuchu, 30
Xyz, 31
Chuchu, 31
Chuchu, 10

The winner of the game is the first player to score the maximum number of points scored. (You should not add the points.) Points are always positive. For the game above, the winner is Xyz, since she scored 31 points before Chuchu also scored 31. 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 either the winner: either Xyz or Chuchu.

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 excited animals.) Third, recall that you can use String.toInt to convert a String to an Int. Finally, if the String passed is null or contains no lines, you should return null.