Created
August 28, 2020 17:13
-
-
Save tmecklem/276da3dd40736aaf3391d119eb8d6f76 to your computer and use it in GitHub Desktop.
This file contains 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 ElixirGame do | |
@moduledoc """ | |
Documentation for `ElixirGame`. | |
""" | |
@doc ~S""" | |
return the answer to a very important topic | |
## Examples | |
iex> ElixirGame.answer_the_question("answer to the meaning of life, the universe, and everything") | |
42 | |
""" | |
def answer_the_question(question) do | |
question | |
|> replace_whitespace_with_puppies() | |
|> every_other_character() | |
|> split_string_at_oddly_specific_string_token() | |
|> count_puppies() | |
|> smash_numbers_together() | |
end | |
@doc ~S""" | |
return a string containing every other character from the given string, starting with the first character and discarding the second, etc | |
def every_other_character(string) do | |
... | |
end | |
## Examples | |
iex> ElixirGame.every_other_character("This is a sentence") | |
"Ti sasnec" | |
""" | |
def every_other_character(string) do | |
String.split(string, "") | |
|> Enum.drop_every(2) | |
|> Enum.join("") | |
end | |
@doc ~S""" | |
return a string where every whitespace character in the original string is replaced by the pupper emoji "🐶" | |
def replace_whitespace_with_puppies(string) do | |
... | |
end | |
## Examples | |
iex> ElixirGame.replace_whitespace_with_puppies("Pupper Pupper") | |
"Pupper🐶Pupper" | |
""" | |
def replace_whitespace_with_puppies(string) do | |
String.replace(string, " ", "🐶") | |
end | |
@doc ~S""" | |
return an array of strings created by splitting the given string at the very specific token string of "nvr" | |
def split_string_at_oddly_specific_string_token(string) do | |
... | |
end | |
## Examples | |
iex> ElixirGame.split_string_at_oddly_specific_string_token("This is a nvr string to split") | |
["This is a ", " string to split"] | |
""" | |
def split_string_at_oddly_specific_string_token(string) do | |
String.split(string, "nvr") | |
end | |
@doc ~S""" | |
take an array of strings as input, and return an array of integers that represents the number of puppy emojis in the corresponding string in the input array | |
def count_puppies(strings) do | |
... | |
end | |
## Examples | |
iex> ElixirGame.count_puppies(["Ha🐶🐶y", "D🐶y", "🐶🐶🐶"]) | |
[2,1,3] | |
""" | |
@puppy_emoji ~r/🐶/ | |
def count_puppies(list_of_str) do | |
list_of_str | |
|> Enum.map(&count_emoji/1) | |
end | |
defp count_emoji(string) do | |
@puppy_emoji | |
|> Regex.scan(string) | |
|> length() | |
end | |
@doc ~S""" | |
take an array of single digit numbers and "smash them together" so that the returned integer is equal to the last number in the array + the second to last number * 10 + the third to last * 100, etc | |
(equivalent to if you took the commas out of the array and read the numbers in sequence) | |
def smash_numbers_together(number_array) do | |
... | |
end | |
## Examples | |
iex> ElixirGame.smash_numbers_together([1, 4, 5]) | |
145 | |
""" | |
def smash_numbers_together(number_array) do | |
reducer = fn number, running_total -> | |
number + running_total * 10 | |
end | |
Enum.reduce(number_array, 0, reducer) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment