Created
April 24, 2025 20:04
-
-
Save flavius-popan/bc8e9758b762ff5de3aa622268d8e4c6 to your computer and use it in GitHub Desktop.
Cracklepop
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 Cracklepop do | |
| @moduledoc """ | |
| Returns a list of number strings, 1 to 100 (inclusive). | |
| If the number is divisible by 3, prints 'Crackle' instead of the number. | |
| If it's divisible by 5, prints 'Pop' instead of the number. | |
| If it's divisible by both 3 and 5, prints 'CracklePop' instead of the number. | |
| DEV NOTES: I've decided to use strings for all values for consistency, and | |
| presumed "printing" means it's valid to return a list of strings to the console | |
| using iex instead of explicitly using IO.puts/1 for each value. Hope this is ok! | |
| """ | |
| @doc """ | |
| Entrypoint for the main cracklepop function. | |
| iex> length(Cracklepop.cracklepop()) | |
| 100 | |
| """ | |
| @spec cracklepop() :: [String.t()] | |
| def cracklepop do | |
| Enum.map(1..100, &cracklepop/1) | |
| end | |
| @doc """ | |
| Converts an integer to the required cracklepop string. | |
| iex> Cracklepop.cracklepop(3) | |
| "Crackle" | |
| iex> Cracklepop.cracklepop(10) | |
| "Pop" | |
| iex> Cracklepop.cracklepop(15) | |
| "CracklePop" | |
| iex> Cracklepop.cracklepop(11) | |
| "11" | |
| """ | |
| @spec cracklepop(integer()) :: String.t() | |
| def cracklepop(n) when rem(n, 3) == 0 and rem(n, 5) == 0, do: "CracklePop" | |
| def cracklepop(n) when rem(n, 3) == 0, do: "Crackle" | |
| def cracklepop(n) when rem(n, 5) == 0, do: "Pop" | |
| def cracklepop(n), do: Integer.to_string(n) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment