From a0ffcd82d783e55f5c1912e88e2eb52f04bd7883 Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Thu, 7 Aug 2025 07:47:55 +0000 Subject: [PATCH] [Sync Iteration] elixir/series/1 --- solutions/elixir/series/1/lib/string_series.ex | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 solutions/elixir/series/1/lib/string_series.ex diff --git a/solutions/elixir/series/1/lib/string_series.ex b/solutions/elixir/series/1/lib/string_series.ex new file mode 100644 index 0000000..a74c494 --- /dev/null +++ b/solutions/elixir/series/1/lib/string_series.ex @@ -0,0 +1,14 @@ +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) when size < 1, do: [] + def slices(s, size), do: slice(s, size, String.length(s) - size) + + defp slice(s, size, position, list \\ []) + defp slice(_, _, position, list) when position < 0, do: list + defp slice(s, size, position, list), do: slice(s, size, position - 1, [String.slice(s, position, size) | list]) +end