🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of angelikatyborska

angelikatyborska's solution

to Series in the Erlang Track

Published at Apr 18 2019 · 0 comments
Test suite


This exercise has changed since this solution was written.

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 tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, rebar3 is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

$ rebar3 eunit


For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.


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.


%% Based on canonical data version 1.0.0
%% https://github.com/exercism/problem-specifications/raw/master/exercises/series/canonical-data.json
%% This file is automatically generated from the exercises canonical data.



'1_slices_of_one_from_one_test'() ->
    ?assertEqual(["1"], series:slices(1, "1")).

'2_slices_of_one_from_two_test'() ->
    ?assertEqual(["1", "2"], series:slices(1, "12")).

'3_slices_of_two_test'() ->
    ?assertEqual(["35"], series:slices(2, "35")).

'4_slices_of_two_overlap_test'() ->
    ?assertEqual(["91", "14", "42"],
		 series:slices(2, "9142")).

'5_slices_can_include_duplicates_test'() ->
    ?assertEqual(["777", "777", "777", "777"],
		 series:slices(3, "777777")).

'6_slices_of_a_long_series_test'() ->
    ?assertEqual(["91849", "18493", "84939", "49390",
		  "93904", "39042", "90424", "04243"],
		 series:slices(5, "918493904243")).

'7_slice_length_is_too_large_test'() ->
    ?assertError(_, series:slices(6, "12345")).

'8_slice_length_cannot_be_zero_test'() ->
    ?assertError(_, series:slices(0, "12345")).

'9_slice_length_cannot_be_negative_test'() ->
    ?assertError(_, series:slices(-1, "123")).

'10_empty_series_is_invalid_test'() ->
    ?assertError(_, series:slices(1, [])).


slices(SliceLength, _Series) when SliceLength =< 0 ->

slices(SliceLength, Series) when SliceLength > length(Series) ->

slices(SliceLength, Series) ->
  Range = lists:seq(1, length(Series) - SliceLength + 1),
  lists:map(fun(N) -> lists:sublist(Series, N, SliceLength) end, Range).

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?