Avatar of zxhcho

zxhcho's solution

to Raindrops in the C# Track

Published at Oct 21 2019 · 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

RaindropsTest.cs

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

using Xunit;

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

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

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

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

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

    [Fact(Skip = "Remove to run 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 to run test")]
    public void The_sound_for_9_is_pling_as_it_has_a_factor_3()
    {
        Assert.Equal("Pling", Raindrops.Convert(9));
    }

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

    [Fact(Skip = "Remove to run 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 to run 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 to run 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 to run test")]
    public void The_sound_for_25_is_plang_as_it_has_a_factor_5()
    {
        Assert.Equal("Plang", Raindrops.Convert(25));
    }

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

    [Fact(Skip = "Remove to run 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 to run test")]
    public void The_sound_for_49_is_plong_as_it_has_a_factor_7()
    {
        Assert.Equal("Plong", Raindrops.Convert(49));
    }

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

    [Fact(Skip = "Remove to run 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 to run 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)
    {
        string ans = "";
        if (number % 3 == 0)
            ans += "Pling";
        if (number % 5 == 0)
            ans += "Plang";
        if (number % 7 == 0)
            ans += "Plong";
        return ans == "" ? number.ToString() : ans;
    }
}

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?