settingsAccountsettings
Menusettings

Q: Sudoku Results - C# Task

+3 votes

Minka likes to play Sudoku. She downloaded on her phone an application so she can play anytime, anywhere, but mostly when she is in the tram going to work. She also likes statistics and wants to know her average time per game. As a young programmer your task is to modify the Sudoku app to calculate the average time per game. After each game you should get the time and when she presses the “Quit” button the application should display the average time in seconds.

Also, when the game ends, it should give a score based on the average time. The rules for the score are as follows:

  • When the average time is lower than 720 seconds - give a Gold Star
  • When the average time is between 720 and 1440 seconds – give a Silver Star
  • When the average time is higher than 1440 seconds – give a Bronze Star

Write a console application to test your modification. You’ll get the result for each game from the console, each on a new line in the format “[mm]:[ss]” until you receive the command “Quit”. Convert the sum of all results to seconds, find the average seconds and print the corresponding star and the count of the games in the following format:

  • {Gold, Silver or Bronze} Star
  • “Games – {count of games} \ Average seconds – {average seconds}”.

Input

The input data should be read from the console. On each new line you will be given a string with the time in minutes and seconds until you receive the command “Quit”.

Output

The output should be printed on the console. It should consist of exactly 2 lines: First with received star and second with the count of the games and the average score in seconds rounded up to the nearest integer in the following format:

  • {Gold, Silver or Bronze} Star
  • “Games – {count of games} \ Average seconds – {average seconds}”.

Constraints

  • The time will always be in the format “[mm]:[ss]” with leading zeros if it’s necessary.
  • The input lines will be in the range: [1… 100].
  • The minutes will be a valid integer in the range: [00… 99].
  • The seconds will be a valid integer in the range: [00… 59].
  • Allowed working time for your program: 0.25 seconds.
  • Allowed memory: 16 MB.

Examples

sudoku results c#

sudoku results c# task

asked in C# category by user Jolie Ann
edited by user golearnweb

1 Answer

+2 votes
 
Best answer

To get the minutes and the seconds you can use Substrings see here: https://www.dotnetperls.com/substring :-) See the lines 14 and 15 - I've made more visible.

Otherwise the logic is straightforward...

Also! Make sure to use double - if you use decimal for average - the program won't properly work!

using System;

class SudokuResults
{
    static void Main()
    {
        int gamesPlayed = 0;
        int totalSeconds = 0;

        string gameTime = Console.ReadLine();
        while (gameTime != "Quit")
        {
            gamesPlayed++;
            int min = int.Parse(gameTime.Substring(0, 2));
            int sec = int.Parse(gameTime.Substring(3, 2));
            totalSeconds += 60 * min + sec;
            gameTime = Console.ReadLine();
        }

        double average = (double)totalSeconds / gamesPlayed;

        if (average < 720)
        {
            Console.WriteLine("Gold Star");
            Console.WriteLine("Games - {0} \\ Average seconds - {1}", gamesPlayed, Math.Ceiling(average));
        }
        else if (average >= 720 && average <= 1440)
        {
            Console.WriteLine("Silver Star");
            Console.WriteLine("Games - {0} \\ Average seconds - {1}", gamesPlayed, Math.Ceiling(average));
        }
        else
        {
            Console.WriteLine("Bronze Star");
            Console.WriteLine("Games - {0} \\ Average seconds - {1}", gamesPlayed, Math.Ceiling(average));
        }
    }
}
answered by user richard8502
edited by user golearnweb
Thanks! Nice explanation!
...