Last active
March 6, 2024 16:44
-
-
Save rpt/d550fd31fb779b71b51af35cda6c38f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Mumbling do | |
def accum(s) do | |
accum(String.graphemes(s), 1, []) | |
end | |
defp accum([], n, acc) do | |
acc | |
|> Enum.reverse() | |
|> Enum.join("-") | |
end | |
defp accum([s | rest], n, acc) do | |
accum(rest, n+1, [duplicate(s, n) | acc]) | |
end | |
def duplicate(s, n) when n > 0, do: duplicate(s, n, []) | |
defp duplicate(s, 1, acc) do | |
List.to_string([String.upcase(s) | acc]) | |
end | |
defp duplicate(s, n, acc) do | |
duplicate(s, n-1, [String.downcase(s) | acc]) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MumblingTest do | |
use ExUnit.Case | |
defp testing(numtest, s, ans) do | |
IO.puts("Test #{numtest} \n") | |
assert Mumbling.accum(s) == ans | |
end | |
test "duplicate" do | |
assert Mumbling.duplicate("p", 1) == "P" | |
assert Mumbling.duplicate("z", 3) == "Zzz" | |
assert Mumbling.duplicate("M", 5) == "Mmmmm" | |
end | |
test "accum" do | |
testing 1, "ZpglnRxqenU", "Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu" | |
testing 2, "NyffsGeyylB", "N-Yy-Fff-Ffff-Sssss-Gggggg-Eeeeeee-Yyyyyyyy-Yyyyyyyyy-Llllllllll-Bbbbbbbbbbb" | |
testing 3, "MjtkuBovqrU", "M-Jj-Ttt-Kkkk-Uuuuu-Bbbbbb-Ooooooo-Vvvvvvvv-Qqqqqqqqq-Rrrrrrrrrr-Uuuuuuuuuuu" | |
testing 4, "EvidjUnokmM", "E-Vv-Iii-Dddd-Jjjjj-Uuuuuu-Nnnnnnn-Oooooooo-Kkkkkkkkk-Mmmmmmmmmm-Mmmmmmmmmmm" | |
testing 5, "HbideVbxncC", "H-Bb-Iii-Dddd-Eeeee-Vvvvvv-Bbbbbbb-Xxxxxxxx-Nnnnnnnnn-Cccccccccc-Ccccccccccc" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment