jsonify

jsonify

I’m trying to continue to wrap my brain around how I should be structuring things and I’m getting stuck on the Bob exercise of Exercism. Here is my code and right now, I’m just not understanding why my “Talking in capitals” test isn’t passing like I think it should:

defmodule Bob do
  def hey("  "), do: "Fine. Be that way!"
  def hey(""), do: "Fine. Be that way!"
  def hey(message) do
    cond do
      shouting?(message) -> "Whoa, chill out!"
      asking?(message) -> "Sure."
      shout_numbers?(message) -> "Whoa, chill out!"
      capitals_letters?(message) -> "Whatever"
      # true -> "Whatever."
    end
  end

  defp shouting?(message) do
    message == String.upcase(message)
  end

  defp asking?(message) do
    String.ends_with?(message, "?")
  end

  defp shout_numbers?(message) do
    message
    |> String.upcase()
    String.ends_with?(message, "?")
  end

  defp capitals_letters?(message) do
    message
    |> String.split(" ")
    |> String.capitalize
    Enum.join(" ")
  end
end

Showing Posts 1 to 10

NobbZ

NobbZ

Your solution isn’t quite generalized.

You shouldn’t have anything aside of the cond, as well as there shouldn’t be more than 4 clauses in it, Chill out, be that way, Sure, and Whatever.

There is no reason to have any of the strings to be returned at more than one place.

Also your current version of shouting does consider "1, 2, 3" shout…

The cases you need to recognize is if the input was shout (at least one alphabetic character and all alphabetics are in uppercase), if it was a question, if it was silence or if it was none of the former.

themarlzy

themarlzy

Your capital_letters? function returns a string, whereas I believe you’re after a true/false?

Here was my solution which passes the tests, should you want to have a peak.

http://exercism.io/submissions/8314666634e546708d242d5f6d9a06d4

jsonify

jsonify OP

While this is still a work in progress, my goal was to get all of the tests to pass first, then refactor while learning more about how to better use Elixir. Thank you for the feedback.

jsonify

jsonify OP

How would you have known to use [\p{L}] in your Regex.match?/2 function? I would NEVER have even begun to figure that one out. Great solution, though.

themarlzy

themarlzy

I know a little regex, though, Dr. Google was helpful in this instance.

jsonify

jsonify OP

So if I may ask, what exactly did you Google as your search? Did you know that you needed to search for the Unicode solution? Just trying to get a better feel for how one would go about how to figure out how to come to a solution. When I saw your solution, all the pieces made sense and I thought, "of course that makes sense how that was solved. " But it’s the how that conclusion was reached that truly interests me. What steps did you go through to get the results you were looking for.

themarlzy

themarlzy

@jsonify, I will try to break out the process some more

Punctuation regex, yielded this article http://www.regular-expressions.info/posixbrackets.html

That was working well, except it was leaving in $ and ^, so added those on top.

Then it was failing the last test with the diaeresis on the o (ö). It was stripping it off and turning it into a binary e.g. <<107>> or whatever. Then, after about an hour of trying stuff, I tried the option for unicode u at the end of the ~r/regex/u.

Hope that was helpful.

M

Qqwy

Qqwy

TypeCheck Core Team

Actually, it is possible to fix this Exercise without using regular expressions.

Here is my submission.

That’s right. all_letters_uppercase?/1 checks if upcasing would not change the string, while downcasing would.

  • First statement: True if string doesn’t contain any lowercase characters.
  • Second statement: True if string contains any uppercase characters.

The logical AND of these statements will return false if either there is a lowercase character, or there are no uppercase-characters at all.

NobbZ

NobbZ

Please link to your submission at exercism, then we could like it or comment.

Qqwy

Qqwy

TypeCheck Core Team

Of course, how did I forget. :sweat_smile: Here is the submission on Exercism.

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews