small medium large xlarge

Dave_gnome_head_isolated_pragsmall
16 Jul 2013, 02:16
Dave Thomas (344 posts)
  • Write a function to capitalize the sentences in a string. Each sentence is terminated by a period and a space. Right now, the case of the characters in the string is random.

    iex> capitalize_sentences("oh. a DOG. woof. ")
    "Oh. A dog. Woof. "
    

A Possible Solution</summary>

defmodule MyString do

  def capitalize_sentences(string) do
    string
    |> String.split(%r{\.\s+})
    |> Enum.map(String.capitalize(&1))
    |> Enum.join(". ")
  end

end

IO.inspect MyString.capitalize_sentences("oh. a DOG. woof. ") 

</details>

Generic-user-small
07 Jan 2014, 00:59
Eric Liaw (3 posts)

irc chat discussion noted a fix for this should have a & in front of the String.capitalize, so it would instead be:

  def capitalize_sentences(string) do
    string
    |> String.split(%r{\.\s+})
    |> Enum.map(&String.capitalize(&1))
    |> Enum.join(". ")
  end
Generic-user-small
21 Jun 2014, 01:35
Luke Imhoff (6 posts)

You can get rid of the argument to Enum.join with a slightly more complex regex as I used in my solution:

defmodule Sentences do
  def captialize_sentences(string) do
    String.split(string, ~r{(?<=\. )(?=.)})
    |> Enum.map(&String.capitalize/1)
    |> Enum.join
  end
end

The regex does a look behind (?<=) for \. and then I had to add a look ahead (?=) for any character ., so that I didn’t get an empty final string after “woof. “.

Generic-user-small
12 Nov 2014, 17:53
Elliot Finley (11 posts)

Here is my version:

defmodule MyString do
  def capitalize_sentences(string) do
    string
    |> String.split(~r{\.\s+}, trim: true)
    |> Enum.map(&String.capitalize/1)
    |> Enum.join(". ")
    |> add_period
  end

  defp add_period(string) do string <> "." end
end
9863_pragsmall
05 Dec 2014, 21:15
Suraj Kurapati (15 posts)

I thought this exercise was about pattern matching on binaries, not about regular expressions:

defmodule MyString do
  def capitalize_sentences(string, starting\\true)
  def capitalize_sentences(<< ".", " ", tail::binary >>, _starting) do
    ". " <> capitalize_sentences(tail, true)
  end
  def capitalize_sentences(<< head::utf8, tail::binary >>, true) do
    String.upcase(<< head >>) <> capitalize_sentences(tail, false)
  end
  def capitalize_sentences(<< head::utf8, tail::binary >>, false) do
    String.downcase(<< head >>) <> capitalize_sentences(tail, false)
  end
  def capitalize_sentences(empty=<< >>, _starting), do: empty
end

The sample solution doesn’t seem to reflect the spirit of this excerise, which happens to be under the “Binaries and Pattern Matching” section of Chapter 11 in the P1.0 printing of this book. :-/

Generic-user-small
29 Dec 2014, 22:07
Pierre Sugar (56 posts)
defmodule MyString

  def capitalize(sentences), 
    do: capitalize_sentences(String.capitalize(sentences))
  def capitalize_sentences(<<". ", tail::binary>>), 
    do: <<". ">> <> capitalize_sentences(String.capitalize(tail))
  def capitalize_sentences(<<head::utf8, tail::binary>>), 
    do: <<head>>  <> capitalize_sentences(tail)
  def capitalize_sentences(<<>>), do: ""
    
end
  You must be logged in to comment