Skip to content

Instantly share code, notes, and snippets.

@DogPawHat
Created February 24, 2021 08:33
Show Gist options
  • Save DogPawHat/2e4af0c090b49b5fccd4794f86a77fd4 to your computer and use it in GitHub Desktop.
Save DogPawHat/2e4af0c090b49b5fccd4794f86a77fd4 to your computer and use it in GitHub Desktop.
cassidoo string deletion test
defmodule LimitedImaginations do
@spec check_for_deletion(
String.grapheme(),
{:ok, number(), String.t()}
) :: {:ok, number(), String.t()}
@spec every_other(String.t()) :: {:ok, number(), String.t()}
def check_for_deletion(el, {:ok, 0, ""}) do
{:ok, 0, el}
end
def check_for_deletion(el, {:ok, count, builder}) do
if String.last(builder) == el do
{:ok, count + 1, builder}
else
{:ok, count, builder <> el}
end
end
def every_other(input) do
String.graphemes(input)
|> Enum.reduce(
{:ok, 0, ""},
&check_for_deletion/2
)
end
end
defmodule LimitedImaginationsTest do
use ExUnit.Case
doctest LimitedImaginations
test "has no matching ajacent characters 1" do
assert LimitedImaginations.every_other("xxyxxy") == {:ok, 2, "xyxy"}
end
test "has no matching ajacent characters 2" do
assert LimitedImaginations.every_other("yyyyy") == {:ok, 4, "y"}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment