series
This commit is contained in:
parent
8fbd81ed9d
commit
6e761f7041
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"authors": [
|
||||
"DoggettCK"
|
||||
],
|
||||
"contributors": [
|
||||
"amencarini",
|
||||
"angelikatyborska",
|
||||
"Cohen-Carlisle",
|
||||
"devonestes",
|
||||
"neenjaw",
|
||||
"sotojuan"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"lib/string_series.ex"
|
||||
],
|
||||
"test": [
|
||||
"test/string_series_test.exs"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.ex"
|
||||
]
|
||||
},
|
||||
"blurb": "Given a string of digits, output all the contiguous substrings of length `n` in that string.",
|
||||
"source": "A subset of the Problem 8 at Project Euler",
|
||||
"source_url": "https://projecteuler.net/problem=8"
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"elixir","exercise":"series","id":"17f1138f483f482b8bb2877fbfb0f91c","url":"https://exercism.org/tracks/elixir/exercises/series","handle":"negrienko","is_requester":true,"auto_approve":false}
|
|
@ -0,0 +1,4 @@
|
|||
# Used by "mix format"
|
||||
[
|
||||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||
]
|
|
@ -0,0 +1,24 @@
|
|||
# The directory Mix will write compiled artifacts to.
|
||||
/_build/
|
||||
|
||||
# If you run "mix test --cover", coverage assets end up here.
|
||||
/cover/
|
||||
|
||||
# The directory Mix downloads your dependencies sources to.
|
||||
/deps/
|
||||
|
||||
# Where third-party dependencies like ExDoc output generated docs.
|
||||
/doc/
|
||||
|
||||
# Ignore .fetch files in case you like to edit your project deps locally.
|
||||
/.fetch
|
||||
|
||||
# If the VM crashes, it generates a dump, let's ignore it too.
|
||||
erl_crash.dump
|
||||
|
||||
# Also ignore archive artifacts (built via "mix archive.build").
|
||||
*.ez
|
||||
|
||||
# Ignore package tarball (built via "mix hex.build").
|
||||
series-*.tar
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
# Help
|
||||
|
||||
## Running the tests
|
||||
|
||||
From the terminal, change to the base directory of the exercise then execute the tests with:
|
||||
|
||||
```bash
|
||||
$ mix test
|
||||
```
|
||||
|
||||
This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs`
|
||||
|
||||
Documentation:
|
||||
|
||||
* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html)
|
||||
* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html)
|
||||
|
||||
## Pending tests
|
||||
|
||||
In test suites of practice exercises, all but the first test have been tagged to be 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:
|
||||
|
||||
```elixir
|
||||
# @tag :pending
|
||||
test "shouting" do
|
||||
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
|
||||
end
|
||||
```
|
||||
|
||||
If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command:
|
||||
|
||||
```bash
|
||||
$ mix test --include pending
|
||||
```
|
||||
|
||||
Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the file `test/test_helper.exs`.
|
||||
|
||||
```elixir
|
||||
# ExUnit.configure(exclude: :pending, trace: true)
|
||||
```
|
||||
|
||||
## Useful `mix test` options
|
||||
|
||||
* `test/<FILE>.exs:LINENUM` - runs only a single test, the test from `<FILE>.exs` whose definition is on line `LINENUM`
|
||||
* `--failed` - runs only tests that failed the last time they ran
|
||||
* `--max-failures` - the suite stops evaluating tests when this number of test failures
|
||||
is reached
|
||||
* `--seed 0` - disables randomization so the tests in a single file will always be ran
|
||||
in the same order they were defined in
|
||||
|
||||
## Submitting your solution
|
||||
|
||||
You can submit your solution using the `exercism submit lib/string_series.ex` command.
|
||||
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||
|
||||
It's possible to submit an incomplete solution which allows you to:
|
||||
|
||||
- See how others have completed the exercise
|
||||
- Request help from a mentor
|
||||
|
||||
## Need to get help?
|
||||
|
||||
If you'd like help solving the exercise, check the following pages:
|
||||
|
||||
- The [Elixir track's documentation](https://exercism.org/docs/tracks/elixir)
|
||||
- The [Elixir track's programming category on the forum](https://forum.exercism.org/c/programming/elixir)
|
||||
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||
|
||||
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||
|
||||
If you're stuck on something, it may help to look at some of the [available resources](https://exercism.org/docs/tracks/elixir/resources) out there where answers might be found.
|
|
@ -0,0 +1,43 @@
|
|||
# Series
|
||||
|
||||
Welcome to Series on Exercism's Elixir Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
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_.
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @DoggettCK
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @amencarini
|
||||
- @angelikatyborska
|
||||
- @Cohen-Carlisle
|
||||
- @devonestes
|
||||
- @neenjaw
|
||||
- @sotojuan
|
||||
|
||||
### Based on
|
||||
|
||||
A subset of the Problem 8 at Project Euler - https://projecteuler.net/problem=8
|
|
@ -0,0 +1,12 @@
|
|||
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
|
||||
length = String.length(s)
|
||||
for start <- 0..(length - size), size in 1..length, into: [], do: String.slice(s, start, size)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,28 @@
|
|||
defmodule StringSeries.MixProject do
|
||||
use Mix.Project
|
||||
|
||||
def project do
|
||||
[
|
||||
app: :string_series,
|
||||
version: "0.1.0",
|
||||
# elixir: "~> 1.8",
|
||||
start_permanent: Mix.env() == :prod,
|
||||
deps: deps()
|
||||
]
|
||||
end
|
||||
|
||||
# Run "mix help compile.app" to learn about applications.
|
||||
def application do
|
||||
[
|
||||
extra_applications: [:logger]
|
||||
]
|
||||
end
|
||||
|
||||
# Run "mix help deps" to learn about dependencies.
|
||||
defp deps do
|
||||
[
|
||||
# {:dep_from_hexpm, "~> 0.3.0"},
|
||||
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
|
||||
]
|
||||
end
|
||||
end
|
|
@ -0,0 +1,70 @@
|
|||
defmodule StringSeriesTest do
|
||||
use ExUnit.Case
|
||||
|
||||
test "slices of size 1 from one" do
|
||||
assert StringSeries.slices("1", 1) == ["1"]
|
||||
end
|
||||
|
||||
test "slices of size 1 from two" do
|
||||
assert StringSeries.slices("12", 1) == ["1", "2"]
|
||||
end
|
||||
|
||||
test "slices of size 1 from more" do
|
||||
assert StringSeries.slices("01234", 1) == ["0", "1", "2", "3", "4"]
|
||||
end
|
||||
|
||||
test "slices of size 2" do
|
||||
assert StringSeries.slices("35", 2) == ["35"]
|
||||
end
|
||||
|
||||
test "slices of size 2 overlap" do
|
||||
assert StringSeries.slices("9142", 2) == ["91", "14", "42"]
|
||||
end
|
||||
|
||||
test "slices of size 2 from more" do
|
||||
assert StringSeries.slices("01234", 2) == ["01", "12", "23", "34"]
|
||||
end
|
||||
|
||||
test "slices of size 3" do
|
||||
assert StringSeries.slices("01234", 3) == ["012", "123", "234"]
|
||||
end
|
||||
|
||||
test "slices of size 4" do
|
||||
assert StringSeries.slices("01234", 4) == ["0123", "1234"]
|
||||
end
|
||||
|
||||
test "slices include duplicates" do
|
||||
assert StringSeries.slices("777777", 3) == ["777", "777", "777", "777"]
|
||||
end
|
||||
|
||||
test "slices of a long series" do
|
||||
assert StringSeries.slices("918493904243", 5) == [
|
||||
"91849",
|
||||
"18493",
|
||||
"84939",
|
||||
"49390",
|
||||
"93904",
|
||||
"39042",
|
||||
"90424",
|
||||
"04243"
|
||||
]
|
||||
end
|
||||
|
||||
test "slices same size as string" do
|
||||
assert StringSeries.slices("01234", 5) == ["01234"]
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
test "slices with size longer than string return empty list" do
|
||||
assert StringSeries.slices("01234", 6) == []
|
||||
end
|
||||
|
||||
test "slices with size zero or negative return empty list" do
|
||||
assert StringSeries.slices("01234", -1) == []
|
||||
assert StringSeries.slices("01234", 0) == []
|
||||
end
|
||||
end
|
|
@ -0,0 +1,2 @@
|
|||
ExUnit.start()
|
||||
ExUnit.configure(exclude: :pending, trace: true)
|
Loading…
Reference in New Issue