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.
Try to capture the structure of the song in your code, where you build up the song by composing its parts.
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
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.
// 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)
{
var proverb = new List<string>();
for(int i = 0; i < subjects.Length; i++)
{
if(i == subjects.Length -1)
proverb.Add($"And all for the want of a {subjects[0]}.");
else
proverb.Add($"For want of a {subjects[i]} the {subjects[i+1]} was lost.");
}
return proverb.ToArray();
}
}
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.
Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments