Kotlinlearncs.online LogoJava
Return to List

Solve: Game CSV Sum

Created By: Geoffrey Challen
/ Version: 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 player with the most total points. For the game above, the winner is Chuchu, since he scored 76 points to Xyz's 65. 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. If both player score the same number of points, 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 bored animals.) Third, recall that you can use String.toInt to convert a String to an Int. Fourth, the only two players you need to handle are Xyz and Chuchu. Finally, if the String passed is null or contains no lines, you should return null.

Related Lessons

Stuck? You may find these lessons helpful: