Avatar of artemkorsakov

artemkorsakov's solution

to Proverb in the C# Track

Published at Feb 25 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

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

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;

public static class Proverb
{
    public static string[] Recite(string[] subjects)
    {
        if (subjects.Length == 0)
        {
            return Array.Empty<string>();
        }
        var result = new string[subjects.Length];
        result[subjects.Length - 1] = GetLastRow(subjects[0]);
        for (var i = 0; i < subjects.Length - 1; i++)
        {
            result[i] = GetNotLastRow(subjects[i], subjects[i+1]);
        }
        return result;
    }

    private static string GetNotLastRow(string subject, string nextSubject)
    {
        return $"For want of a {subject} the {nextSubject} was lost.";
    }

    private static string GetLastRow(string subject)
    {
        return $"And all for the want of a {subject}.";
    }
}

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?