bob elixir solution

This commit is contained in:
Xevion
2021-01-18 17:01:34 -06:00
parent e631ea80bc
commit db60759456
8 changed files with 290 additions and 0 deletions

View File

@@ -0,0 +1 @@
{"track":"elixir","exercise":"bob","id":"6d5bb0c1f206484b82bbbb9c9f01069a","url":"https://exercism.io/my/solutions/6d5bb0c1f206484b82bbbb9c9f01069a","handle":"Xevion","is_requester":true,"auto_approve":false}

View File

@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

24
elixir/bob/.gitignore vendored Normal file
View File

@@ -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").
bob-*.tar

58
elixir/bob/README.md Normal file
View File

@@ -0,0 +1,58 @@
# Bob
Bob is a lackadaisical teenager. In conversation, his responses are very limited.
Bob answers 'Sure.' if you ask him a question.
He answers 'Whoa, chill out!' if you yell at him.
He answers 'Calm down, I know what I'm doing!' if you yell a question at him.
He says 'Fine. Be that way!' if you address him without actually saying
anything.
He answers 'Whatever.' to anything else.
Bob's conversational partner is a purist when it comes to written communication and always follows normal rules regarding sentence punctuation in English.
## Running tests
Execute the tests with:
```bash
$ mix test
```
### Pending tests
In the test suites, all but the first test have been 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
```
Or, you can enable all the tests by commenting out the
`ExUnit.configure` line in the test suite.
```elixir
# ExUnit.configure exclude: :pending, trace: true
```
If you're stuck on something, it may help to look at some of
the [available resources](https://exercism.io/tracks/elixir/resources)
out there where answers might be found.
## Source
Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. [http://pine.fm/LearnToProgram/?Chapter=06](http://pine.fm/LearnToProgram/?Chapter=06)
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

31
elixir/bob/lib/bob.ex Normal file
View File

@@ -0,0 +1,31 @@
defmodule Bob do
def hey(input) do
input = String.trim(input)
uppercase =
Enum.all?(
Enum.filter(String.graphemes(input), &String.match?(&1, ~r/^\p{L}$/u)),
&String.match?(&1, ~r/^\p{Lu}$/u)
)
has_letters = Enum.any?(String.graphemes(input), &String.match?(&1, ~r/^\p{L}$/u))
question = String.ends_with?(input, "?")
cond do
String.length(input) == 0 ->
"Fine. Be that way!"
has_letters && uppercase && question ->
"Calm down, I know what I'm doing!"
question ->
"Sure."
has_letters && uppercase ->
"Whoa, chill out!"
true ->
"Whatever."
end
end
end

28
elixir/bob/mix.exs Normal file
View File

@@ -0,0 +1,28 @@
defmodule Bob.MixProject do
use Mix.Project
def project do
[
app: :bob,
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

View File

@@ -0,0 +1,142 @@
defmodule BobTest do
use ExUnit.Case
test "stating something" do
assert Bob.hey("Tom-ay-to, tom-aaaah-to.") == "Whatever."
end
@tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
@tag :pending
test "shouting gibberish" do
assert Bob.hey("FCECDFCAAB") == "Whoa, chill out!"
end
@tag :pending
test "asking a question" do
assert Bob.hey("Does this cryogenic chamber make me look fat?") == "Sure."
end
@tag :pending
test "asking a numeric question" do
assert Bob.hey("You are, what, like 15?") == "Sure."
end
@tag :pending
test "asking gibberish" do
assert Bob.hey("fffbbcbeab?") == "Sure."
end
@tag :pending
test "talking forcefully" do
assert Bob.hey("Hi there!") == "Whatever."
end
@tag :pending
test "using acronyms in regular speech" do
assert Bob.hey("It's OK if you don't want to go to the DMV.") == "Whatever."
end
@tag :pending
test "talking in capitals" do
assert Bob.hey("This Isn't Shouting!") == "Whatever."
end
@tag :pending
test "forceful question" do
assert Bob.hey("WHAT'S GOING ON?") == "Calm down, I know what I'm doing!"
end
@tag :pending
test "asking in capitals" do
assert Bob.hey("THIS ISN'T SHOUTING?") == "Calm down, I know what I'm doing!"
end
@tag :pending
test "shouting numbers" do
assert Bob.hey("1, 2, 3 GO!") == "Whoa, chill out!"
end
@tag :pending
test "shouting with special characters" do
assert Bob.hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!") == "Whoa, chill out!"
end
@tag :pending
test "shouting with no exclamation mark" do
assert Bob.hey("I HATE THE DENTIST") == "Whoa, chill out!"
end
@tag :pending
test "statement containing question mark" do
assert Bob.hey("Ending with ? means a question.") == "Whatever."
end
@tag :pending
test "silence" do
assert Bob.hey("") == "Fine. Be that way!"
end
@tag :pending
test "prolonged silence" do
assert Bob.hey(" ") == "Fine. Be that way!"
end
@tag :pending
test "alternate silence" do
assert Bob.hey("\t\t\t\t\t\t\t\t\t\t") == "Fine. Be that way!"
end
@tag :pending
test "only numbers" do
assert Bob.hey("1, 2, 3") == "Whatever."
end
@tag :pending
test "multiple line question" do
assert Bob.hey("\nDoes this cryogenic chamber make me look fat?\nNo.") == "Whatever."
end
@tag :pending
test "question with numbers" do
assert Bob.hey("4?") == "Sure."
end
@tag :pending
test "non-letters with question" do
assert Bob.hey(":) ?") == "Sure."
end
@tag :pending
test "prattling on" do
assert Bob.hey("Wait! Hang on. Are you going to be OK?") == "Sure."
end
@tag :pending
test "starting with whitespace" do
assert Bob.hey(" hmmmmmmm...") == "Whatever."
end
@tag :pending
test "ending with whitespace" do
assert Bob.hey("Okay if like my spacebar quite a bit? ") == "Sure."
end
@tag :pending
test "other whitespace" do
assert Bob.hey("\n\r \t") == "Fine. Be that way!"
end
@tag :pending
test "non-question ending with whitespace" do
assert Bob.hey("This is a statement ending with whitespace ") == "Whatever."
end
@tag :pending
test "shouting in Russian" do
assert Bob.hey("УХОДИ") == "Whoa, chill out!"
end
end

View File

@@ -0,0 +1,2 @@
ExUnit.start()
# ExUnit.configure(exclude: :pending, trace: true)