Avatar of davearonson

davearonson's solution

to Series in the Elixir Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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.

Running tests

Execute the tests with:

$ elixir series_test.exs

Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant @tag :pending with a # symbol.

For example:

# @tag :pending
test "shouting" do
  assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end

Or, you can enable all the tests by commenting out the ExUnit.configure line in the test suite.

# ExUnit.configure exclude: :pending, trace: true

For more detailed information about the Elixir track, please see the help page.

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_test.exs

if !System.get_env("EXERCISM_TEST_EXAMPLES") do
  Code.load_file("series.exs", __DIR__)
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

defmodule StringSeriesTest do
  use ExUnit.Case

  # @tag :pending
  test "slices of size 1" do
    assert StringSeries.slices("01234", 1) == ["0", "1", "2", "3", "4"]
  end

  @tag :pending
  test "slices of size 2" do
    assert StringSeries.slices("01234", 2) == ["01", "12", "23", "34"]
  end

  @tag :pending
  test "slices of size 3" do
    assert StringSeries.slices("01234", 3) == ["012", "123", "234"]
  end

  @tag :pending
  test "slices of size 4" do
    assert StringSeries.slices("01234", 4) == ["0123", "1234"]
  end

  @tag :pending
  test "slices same size as string" do
    assert StringSeries.slices("01234", 5) == ["01234"]
  end

  @tag :pending
  test "Unicode characters count as a single character" do
    assert StringSeries.slices("José", 1) == ["J", "o", "s", "é"]
    assert StringSeries.slices("José", 2) == ["Jo", "os", "sé"]
  end

  @tag :pending
  test "slices with size longer than string return empty list" do
    assert StringSeries.slices("01234", 6) == []
  end

  @tag :pending
  test "slices with size zero or negative return empty list" do
    assert StringSeries.slices("01234", -1) == []
    assert StringSeries.slices("01234", 0) == []
  end
end
defmodule StringSeries do
  @doc """
  Given a string `s` and a positive integer `size`, return all substrings
  of that size. If `size` is greater than the length of `s`, or less than 1,
  return an empty list.
  """
  @spec slices(s :: String.t(), size :: integer) :: list(String.t())
  def slices(s, size) do
    do_slices(String.graphemes(s), size, [])
  end

  defp do_slices(_s, size, _acc) when size <= 0, do: []
  defp do_slices(s, size, acc) when length(s) < size, do: acc |> Enum.reverse
  defp do_slices(s, size, acc) do
    substr = s |> Enum.take(size) |> Enum.join 
    rest = s |> Enum.drop(1)
    do_slices(rest, size, [substr|acc])
  end

end

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?