Avatar of Mikael128

Mikael128's solution

to Series in the C# Track

Published at Sep 30 2019 · 0 comments
Instructions
Test suite
Solution

Given a string of digits, output all the contiguous substrings of length n in that string in the order that they appear.

For example, the string "49142" has the following 3-digit series:

  • "491"
  • "914"
  • "142"

And the following 4-digit series:

  • "4914"
  • "9142"

And if you ask for a 6-digit series from a 5-digit string, you deserve whatever you get.

Note that these series are only required to occupy adjacent positions in the input; the digits need not be numerically consecutive.

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 Series.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 subset of the Problem 8 at Project Euler http://projecteuler.net/problem=8

SeriesTest.cs

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

using System;
using Xunit;

public class SeriesTest
{
    [Fact]
    public void Slices_of_one_from_one()
    {
        var expected = new[] { "1" };
        Assert.Equal(expected, Series.Slices("1", 1));
    }

    [Fact(Skip = "Remove to run test")]
    public void Slices_of_one_from_two()
    {
        var expected = new[] { "1", "2" };
        Assert.Equal(expected, Series.Slices("12", 1));
    }

    [Fact(Skip = "Remove to run test")]
    public void Slices_of_two()
    {
        var expected = new[] { "35" };
        Assert.Equal(expected, Series.Slices("35", 2));
    }

    [Fact(Skip = "Remove to run test")]
    public void Slices_of_two_overlap()
    {
        var expected = new[] { "91", "14", "42" };
        Assert.Equal(expected, Series.Slices("9142", 2));
    }

    [Fact(Skip = "Remove to run test")]
    public void Slices_can_include_duplicates()
    {
        var expected = new[] { "777", "777", "777", "777" };
        Assert.Equal(expected, Series.Slices("777777", 3));
    }

    [Fact(Skip = "Remove to run test")]
    public void Slices_of_a_long_series()
    {
        var expected = new[] { "91849", "18493", "84939", "49390", "93904", "39042", "90424", "04243" };
        Assert.Equal(expected, Series.Slices("918493904243", 5));
    }

    [Fact(Skip = "Remove to run test")]
    public void Slice_length_is_too_large()
    {
        Assert.Throws<ArgumentException>(() => Series.Slices("12345", 6));
    }

    [Fact(Skip = "Remove to run test")]
    public void Slice_length_cannot_be_zero()
    {
        Assert.Throws<ArgumentException>(() => Series.Slices("12345", 0));
    }

    [Fact(Skip = "Remove to run test")]
    public void Slice_length_cannot_be_negative()
    {
        Assert.Throws<ArgumentException>(() => Series.Slices("123", -1));
    }

    [Fact(Skip = "Remove to run test")]
    public void Empty_series_is_invalid()
    {
        Assert.Throws<ArgumentException>(() => Series.Slices("", 1));
    }
}
using System;

public static class Series
{
    public static string[] Slices(string numbers, int sliceLength)
    {
        if (numbers == "" || sliceLength > numbers.Length || sliceLength <= 0)
            throw new ArgumentException();

        var output = new string[numbers.Length - sliceLength + 1];

        for (int i = 0; i < output.Length; i++)
            output[i] = numbers.Substring(i, sliceLength);        
        return output;
    }
}

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?