# m3m0ry's solution

## to Series in the D Track

Published at Dec 26 2018 · 0 comments
Instructions
Test suite
Solution

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

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.

## Getting Started

Make sure you have read D page on exercism.io. This covers the basic information on setting up the development environment expected by the exercises.

## Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development. Create just enough structure by declaring namespaces, functions, classes, etc., to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, uncomment the next test by moving the following line past the next test.

``````static if (all_tests_enabled)
``````

This may result in compile errors as new constructs may be invoked that you haven't yet declared or defined. Again, fix the compile errors minimally to get a failing test, then change the code minimally to pass the test, refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard D facilities in preference to writing your own low-level algorithms or facilities by hand. DRefLanguage and DReference are references to the D language and D standard library.

## Source

A subset of the Problem 8 at Project Euler http://projecteuler.net/problem=8

## Submitting Incomplete Solutions

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

### series.d

``````module series;

import std.algorithm.comparison : equal;

unittest
{
import std.exception : assertThrown;

immutable int allTestsEnabled = 0;

// short_digits
{
const int[] expected = [0, 1, 2, 3, 4, 5];
const int[] actual = digits("012345");

assert(equal(actual, expected));
}

static if (allTestsEnabled)
{
// long_digits
{
const int[] expected = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const int[] actual = digits("0123456789");

assert(equal(actual, expected));
}

// keeps_the_digit_order_if_reversed
{
const int[] expected = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0];
const int[] actual = digits("9876543210");

assert(equal(actual, expected));
}

// keeps_arbitrary_digit_order
{
const int[] expected = [9, 3, 6, 9, 2, 3, 4, 6, 8];
const int[] actual = digits("936923468");

assert(equal(actual, expected));
}

// can_slice_by_1
{
const int[][] expected = [[0], [1], [2], [3], [4]];
const int[][] actual = slice("01234", 1);

assert(equal(actual, expected));
}

// can_slice_by_2
{
const int[][] expected = [[9, 8], [8, 2], [2, 7], [7, 3], [3, 4], [4, 6], [6, 3]];
const int[][] actual = slice("98273463", 2);

assert(equal(actual, expected));
}

// can_slice_by_3
{
const int[][] expected = [[0, 1, 2], [1, 2, 3], [2, 3, 4]];
const int[][] actual = slice("01234", 3);

assert(equal(actual, expected));
}

// can_slice_by_3_with_duplicate_digits
{
const int[][] expected = [[3, 1, 0], [1, 0, 0], [0, 0, 1]];
const int[][] actual = slice("31001", 3);

assert(equal(actual, expected));
}

// can_slice_by_4
{
const int[][] expected = [[3, 1, 0], [1, 0, 0], [0, 0, 1]];
const int[][] actual = slice("31001", 3);

assert(equal(actual, expected));
}

// can_slice_by_5
{
const int[][] expected = [[8, 1, 2, 2, 8]];
const int[][] actual = slice("81228", 5);

assert(equal(actual, expected));
}

// exception_if_not_enough_digits_to_slice
{
assertThrown(slice("01032987583", 12));
}

// exception_if_invalid_input
{
assertThrown(slice("01032i987583", 12));
}

// exception_if_invalid_input_2
{
assertThrown(digits("01032i987583"));
}

}

}``````
``````module series;

import std.algorithm.comparison : equal;
import std.string : isNumeric;
import std.exception : enforce;
import std.conv : to;
import std.array : split, array;
import std.algorithm.iteration : map;

int[] digits(string numbers){
enforce(isNumeric(numbers));

return map!(c => to!int(c))(numbers.split("")).array;
}

int[][] slice(string numbers, int size = 1){
enforce(size <= numbers.length);
enforce(isNumeric(numbers));

auto nSeries = numbers.length - size + 1;

int[][] slices;
auto digits = digits(numbers);

foreach(i; 0..nSeries)
{
slices ~= digits[i..i+size];
}

return slices;
}

unittest
{
import std.exception : assertThrown;

immutable int allTestsEnabled = 1;

// short_digits
{
const int[] expected = [0, 1, 2, 3, 4, 5];
const int[] actual = digits("012345");

assert(equal(actual, expected));
}

static if (allTestsEnabled){
// long_digits
{
const int[] expected = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const int[] actual = digits("0123456789");

assert(equal(actual, expected));
}

// keeps_the_digit_order_if_reversed
{
const int[] expected = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0];
const int[] actual = digits("9876543210");

assert(equal(actual, expected));
}

// keeps_arbitrary_digit_order
{
const int[] expected = [9, 3, 6, 9, 2, 3, 4, 6, 8];
const int[] actual = digits("936923468");

assert(equal(actual, expected));
}

// can_slice_by_1
{
const int[][] expected = [[0], [1], [2], [3], [4]];
const int[][] actual = slice("01234", 1);

assert(equal(actual, expected));
}

// can_slice_by_2
{
const int[][] expected = [[9, 8], [8, 2], [2, 7], [7, 3], [3, 4], [4, 6], [6, 3]];
const int[][] actual = slice("98273463", 2);

assert(equal(actual, expected));
}

// can_slice_by_3
{
const int[][] expected = [[0, 1, 2], [1, 2, 3], [2, 3, 4]];
const int[][] actual = slice("01234", 3);

assert(equal(actual, expected));
}

// can_slice_by_3_with_duplicate_digits
{
const int[][] expected = [[3, 1, 0], [1, 0, 0], [0, 0, 1]];
const int[][] actual = slice("31001", 3);

assert(equal(actual, expected));
}

// can_slice_by_4
{
const int[][] expected = [[3, 1, 0], [1, 0, 0], [0, 0, 1]];
const int[][] actual = slice("31001", 3);

assert(equal(actual, expected));
}

// can_slice_by_5
{
const int[][] expected = [[8, 1, 2, 2, 8]];
const int[][] actual = slice("81228", 5);

assert(equal(actual, expected));
}

// exception_if_not_enough_digits_to_slice
{
assertThrown(slice("01032987583", 12));
}

// exception_if_invalid_input
{
assertThrown(slice("01032i987583", 12));
}

// exception_if_invalid_input_2
{
assertThrown(digits("01032i987583"));
}

}

}``````