ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# pmart's solution

## to Raindrops in the C# Track

Published at Oct 27 2020 · 0 comments
Instructions
Test suite
Solution

Convert a number to a string, the contents of which depend on the number's factors.

• If the number has 3 as a factor, output 'Pling'.
• If the number has 5 as a factor, output 'Plang'.
• If the number has 7 as a factor, output 'Plong'.
• If the number does not have 3, 5, or 7 as a factor, just pass the number's digits straight through.

## Examples

• 28's factors are 1, 2, 4, 7, 14, 28.
• In raindrop-speak, this would be a simple "Plong".
• 30's factors are 1, 2, 3, 5, 6, 10, 15, 30.
• In raindrop-speak, this would be a "PlingPlang".
• 34 has four factors: 1, 2, 17, and 34.
• In raindrop-speak, this would be "34".

## 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 Raindrops.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 variation on a famous interview question intended to weed out potential candidates. http://jumpstartlab.com

### RaindropsTests.cs

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

using Xunit;

public class RaindropsTests
{
[Fact]
public void The_sound_for_1_is_1()
{
Assert.Equal("1", Raindrops.Convert(1));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void The_sound_for_3_is_pling()
{
Assert.Equal("Pling", Raindrops.Convert(3));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void The_sound_for_5_is_plang()
{
Assert.Equal("Plang", Raindrops.Convert(5));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void The_sound_for_7_is_plong()
{
Assert.Equal("Plong", Raindrops.Convert(7));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void The_sound_for_6_is_pling_as_it_has_a_factor_3()
{
Assert.Equal("Pling", Raindrops.Convert(6));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Number_2_to_the_power_3_does_not_make_a_raindrop_sound_as_3_is_the_exponent_not_the_base()
{
Assert.Equal("8", Raindrops.Convert(8));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void The_sound_for_9_is_pling_as_it_has_a_factor_3()
{
Assert.Equal("Pling", Raindrops.Convert(9));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void The_sound_for_10_is_plang_as_it_has_a_factor_5()
{
Assert.Equal("Plang", Raindrops.Convert(10));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void The_sound_for_14_is_plong_as_it_has_a_factor_of_7()
{
Assert.Equal("Plong", Raindrops.Convert(14));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void The_sound_for_15_is_plingplang_as_it_has_factors_3_and_5()
{
Assert.Equal("PlingPlang", Raindrops.Convert(15));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void The_sound_for_21_is_plingplong_as_it_has_factors_3_and_7()
{
Assert.Equal("PlingPlong", Raindrops.Convert(21));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void The_sound_for_25_is_plang_as_it_has_a_factor_5()
{
Assert.Equal("Plang", Raindrops.Convert(25));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void The_sound_for_27_is_pling_as_it_has_a_factor_3()
{
Assert.Equal("Pling", Raindrops.Convert(27));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void The_sound_for_35_is_plangplong_as_it_has_factors_5_and_7()
{
Assert.Equal("PlangPlong", Raindrops.Convert(35));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void The_sound_for_49_is_plong_as_it_has_a_factor_7()
{
Assert.Equal("Plong", Raindrops.Convert(49));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void The_sound_for_52_is_52()
{
Assert.Equal("52", Raindrops.Convert(52));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void The_sound_for_105_is_plingplangplong_as_it_has_factors_3_5_and_7()
{
Assert.Equal("PlingPlangPlong", Raindrops.Convert(105));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void The_sound_for_3125_is_plang_as_it_has_a_factor_5()
{
Assert.Equal("Plang", Raindrops.Convert(3125));
}
}``````
``````using System;

public static class Raindrops
{
public static string Convert(int number)
{
int curnum = number;
string result = "";
bool spent3 = false, spent5 = false, spent7 = false;

do
{
int fact = NextFactor(curnum);
curnum /= fact;

switch (fact)
{
case 3:
if (!spent3)
{
result += "Pling";
spent3 = true;
}
break;
case 5:
if (!spent5)
{
result += "Plang";
spent5 = true;
}
break;
case 7:
if (!spent7)
{
result += "Plong";
spent7 = true;
}

break;
default:
break;
}
} while (curnum > 1);

if (string.IsNullOrEmpty(result))
{
result += number;
}
return result;
}

private static int NextFactor(int number)
{
for (int i = 2; i <= number; i++)
{
if ((number % i) == 0)
{
return i;
}
}

return number;
}
}``````

### 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?