Avatar of SasanAhmadi

SasanAhmadi's solution

to Robot Name in the C# Track

Published at Oct 08 2019 · 0 comments
Instructions
Test suite
Solution

Manage robot factory settings.

When robots come off the factory floor, they have no name.

The first time you boot them up, a random name is generated in the format of two uppercase letters followed by three digits, such as RX837 or BC811.

Every once in a while we need to reset a robot to its factory settings, which means that their name gets wiped. The next time you ask, it will respond with a new random name.

The names must be random: they should not follow a predictable sequence. Random names means a risk of collisions. Your solution must ensure that every existing robot has a unique name.

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit RobotName.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Source

A debugging session with Paul Blackwell at gSchool. http://gschool.it

RobotNameTest.cs

using System.Collections.Generic;
using Xunit;

public class RobotNameTest
{
    private readonly Robot robot = new Robot();

    [Fact]
    public void Robot_has_a_name()
    {
        Assert.Matches(@"^[A-Z]{2}\d{3}$", robot.Name);
    }

    [Fact(Skip = "Remove to run test")]
    public void Name_is_the_same_each_time()
    {
        Assert.Equal(robot.Name, robot.Name);
    }

    [Fact(Skip = "Remove to run test")]
    public void Different_robots_have_different_names()
    {
        var robot2 = new Robot();
        Assert.NotEqual(robot2.Name, robot.Name);
    }

    [Fact(Skip = "Remove to run test")]
    public void Can_reset_the_name()
    {
        var originalName = robot.Name;
        robot.Reset();
        Assert.NotEqual(originalName, robot.Name);
    }

    [Fact(Skip = "Remove to run test")]
    public void After_reset_the_name_is_valid()
    {
        robot.Reset();
        Assert.Matches(@"^[A-Z]{2}\d{3}$", robot.Name);
    }

    [Fact(Skip = "Remove to run test")]
    public void Robot_names_are_unique()
    {
        var names = new HashSet<string>();
        for (int i = 0; i < 10_000; i++) {
            var robot = new Robot();
            Assert.True(names.Add(robot.Name));
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;

public class Robot
{
    private static List<string> names = new List<string>();

    private char[] alloedchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
    private Random rand = new Random();

    public Robot()
    {
        Generatename();
    }
    private void Generatename()
    {
        string output = "";
        StringBuilder builder = new StringBuilder();
        do
        {
            builder.Clear();
            builder.Append(alloedchars[rand.Next(alloedchars.Length - 1)]);
            builder.Append(alloedchars[rand.Next(alloedchars.Length - 1)]);
            builder.Append(rand.Next(1000).ToString("000"));
            output = builder.ToString();
        } while (names.Contains(output));
        names.Add(output);
        Name = output;
    }

    public string Name { get; set; }

    public void Reset()
    {
        Generatename();
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?