Avatar of AmSmart

AmSmart's solution

to Proverb in the C# Track

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

For want of a horseshoe nail, a kingdom was lost, or so the saying goes.

Given a list of inputs, generate the relevant proverb. For example, given the list ["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"], you will output the full text of this proverbial rhyme:

For want of a nail the shoe was lost.
For want of a shoe the horse was lost.
For want of a horse the rider was lost.
For want of a rider the message was lost.
For want of a message the battle was lost.
For want of a battle the kingdom was lost.
And all for the want of a nail.

Note that the list of inputs may vary; your solution should be able to handle lists of arbitrary length and content. No line of the output text should be a static, unchanging string; all should vary according to the input given.

HINTS

Try to capture the structure of the song in your code, where you build up the song by composing its parts.

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 Proverb.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

Wikipedia http://en.wikipedia.org/wiki/For_Want_of_a_Nail

ProverbTest.cs

// This file was auto-generated based on version 1.1.0 of the canonical data.

using System;
using Xunit;

public class ProverbTest
{
    [Fact]
    public void Zero_pieces()
    {
        var strings = Array.Empty<string>();
        var expected = Array.Empty<string>();
        Assert.Equal(expected, Proverb.Recite(strings));
    }

    [Fact(Skip = "Remove to run test")]
    public void One_piece()
    {
        var strings = new[]
        {
            "nail"
        };
        var expected = new[]
        {
            "And all for the want of a nail."
        };
        Assert.Equal(expected, Proverb.Recite(strings));
    }

    [Fact(Skip = "Remove to run test")]
    public void Two_pieces()
    {
        var strings = new[]
        {
            "nail",
            "shoe"
        };
        var expected = new[]
        {
            "For want of a nail the shoe was lost.",
            "And all for the want of a nail."
        };
        Assert.Equal(expected, Proverb.Recite(strings));
    }

    [Fact(Skip = "Remove to run test")]
    public void Three_pieces()
    {
        var strings = new[]
        {
            "nail",
            "shoe",
            "horse"
        };
        var expected = new[]
        {
            "For want of a nail the shoe was lost.",
            "For want of a shoe the horse was lost.",
            "And all for the want of a nail."
        };
        Assert.Equal(expected, Proverb.Recite(strings));
    }

    [Fact(Skip = "Remove to run test")]
    public void Full_proverb()
    {
        var strings = new[]
        {
            "nail",
            "shoe",
            "horse",
            "rider",
            "message",
            "battle",
            "kingdom"
        };
        var expected = new[]
        {
            "For want of a nail the shoe was lost.",
            "For want of a shoe the horse was lost.",
            "For want of a horse the rider was lost.",
            "For want of a rider the message was lost.",
            "For want of a message the battle was lost.",
            "For want of a battle the kingdom was lost.",
            "And all for the want of a nail."
        };
        Assert.Equal(expected, Proverb.Recite(strings));
    }

    [Fact(Skip = "Remove to run test")]
    public void Four_pieces_modernized()
    {
        var strings = new[]
        {
            "pin",
            "gun",
            "soldier",
            "battle"
        };
        var expected = new[]
        {
            "For want of a pin the gun was lost.",
            "For want of a gun the soldier was lost.",
            "For want of a soldier the battle was lost.",
            "And all for the want of a pin."
        };
        Assert.Equal(expected, Proverb.Recite(strings));
    }
}
using System;
using System.Collections.Generic;

public static class Proverb
{
    public static string[] Recite(string[] subjects)
    {
        List<string> output = new List<string>();
        if(subjects.Length == 0)
        {
            return new string[]{};
        }
        
        if(subjects.Length == 1)
        {
            return new string[]{$"And all for the want of a {subjects[0]}."};
        }
        
        for(int i = 0; i < subjects.Length-1; i++)
        {
            output.Add(
            $"For want of a {subjects[i]} the {subjects[i+1]} was lost."
                );
        }

        output.Add($"And all for the want of a {subjects[0]}.");
        return output.ToArray();
    }
}

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?