야구의 볼 카운트(어떻게 쓰는)(Elixir)
                                            
                                                
                                                
                                                
                                                
                                                
                                                 14130 단어  Elixir
                    
소개
Elixir 즐기십니까
 
  
 
@ 나베타 님의 「 오프라인 실시간 어떻게 쓰는 제3회 참고문제 」를 Elixir 로 해 보았습니다
@obelisk68의 "야구 볼 카운트 (어떻게 쓰는)"에서 문제의 존재를 알았습니다.
준비
Elixir을 설치합시다
잘못하면
elixirjp.slack.com slack workspace 또는 NervesJP workspace에 들어와서
@torifukukaiou에 문의하십시오.mix new
$ mix new ball_count
$ cd ball_count
 소스 코드 작성
lib/ball_count.exdefmodule BallCount do
  @doc """
  ## Examples
      iex> BallCount.solve("s")
      "010"
      iex> BallCount.solve("sss")
      "010,020,100"
      iex> BallCount.solve("bbbb")
      "001,002,003,000"
      iex> BallCount.solve("ssbbbb")
      "010,020,021,022,023,000"
      iex> BallCount.solve("ssbbbb")
      "010,020,021,022,023,000"
      iex> BallCount.solve("hsbhfhbh")
      "000,010,011,000,010,000,001,000"
      iex> BallCount.solve("psbpfpbp")
      "100,110,111,200,210,000,001,100"
      iex> BallCount.solve("ppp")
      "100,200,000"
      iex> BallCount.solve("ffffs")
      "010,020,020,020,100"
      iex> BallCount.solve("ssspfffs")
      "010,020,100,200,210,220,220,000"
      iex> BallCount.solve("bbbsfbppp")
      "001,002,003,013,023,000,100,200,000"
      iex> BallCount.solve("sssbbbbsbhsbppp")
      "010,020,100,101,102,103,100,110,111,100,110,111,200,000,100"
      iex> BallCount.solve("ssffpffssp")
      "010,020,020,020,100,110,120,200,210,000"
  """
  def solve(input) do
    String.codepoints(input)
    |> Enum.reduce([{0, 0, 0}], fn c, [head | _] = acc ->
      [do_solve(c, head) | acc]
    end)
    |> Enum.reverse()
    |> tl()
    |> Enum.map(&Tuple.to_list/1)
    |> Enum.map(&Enum.join/1)
    |> Enum.join(",")
  end
  defp do_solve("s", {2, 2, _}), do: {0, 0, 0}
  defp do_solve("s", {out, 2, _}), do: {out + 1, 0, 0}
  defp do_solve("s", {out, strike, ball}), do: {out, strike + 1, ball}
  defp do_solve("b", {out, _, 3}), do: {out, 0, 0}
  defp do_solve("b", {out, strike, ball}), do: {out, strike, ball + 1}
  defp do_solve("h", {out, _, _}), do: {out, 0, 0}
  defp do_solve("f", {out, 2, ball}), do: {out, 2, ball}
  defp do_solve("f", {out, strike, ball}), do: {out, strike + 1, ball}
  defp do_solve("p", {2, _, _}), do: {0, 0, 0}
  defp do_solve("p", {out, _, _}), do: {out + 1, 0, 0}
end
 Doctests
$ mix new ball_count
$ cd ball_count
lib/ball_count.ex
defmodule BallCount do
  @doc """
  ## Examples
      iex> BallCount.solve("s")
      "010"
      iex> BallCount.solve("sss")
      "010,020,100"
      iex> BallCount.solve("bbbb")
      "001,002,003,000"
      iex> BallCount.solve("ssbbbb")
      "010,020,021,022,023,000"
      iex> BallCount.solve("ssbbbb")
      "010,020,021,022,023,000"
      iex> BallCount.solve("hsbhfhbh")
      "000,010,011,000,010,000,001,000"
      iex> BallCount.solve("psbpfpbp")
      "100,110,111,200,210,000,001,100"
      iex> BallCount.solve("ppp")
      "100,200,000"
      iex> BallCount.solve("ffffs")
      "010,020,020,020,100"
      iex> BallCount.solve("ssspfffs")
      "010,020,100,200,210,220,220,000"
      iex> BallCount.solve("bbbsfbppp")
      "001,002,003,013,023,000,100,200,000"
      iex> BallCount.solve("sssbbbbsbhsbppp")
      "010,020,100,101,102,103,100,110,111,100,110,111,200,000,100"
      iex> BallCount.solve("ssffpffssp")
      "010,020,020,020,100,110,120,200,210,000"
  """
  def solve(input) do
    String.codepoints(input)
    |> Enum.reduce([{0, 0, 0}], fn c, [head | _] = acc ->
      [do_solve(c, head) | acc]
    end)
    |> Enum.reverse()
    |> tl()
    |> Enum.map(&Tuple.to_list/1)
    |> Enum.map(&Enum.join/1)
    |> Enum.join(",")
  end
  defp do_solve("s", {2, 2, _}), do: {0, 0, 0}
  defp do_solve("s", {out, 2, _}), do: {out + 1, 0, 0}
  defp do_solve("s", {out, strike, ball}), do: {out, strike + 1, ball}
  defp do_solve("b", {out, _, 3}), do: {out, 0, 0}
  defp do_solve("b", {out, strike, ball}), do: {out, strike, ball + 1}
  defp do_solve("h", {out, _, _}), do: {out, 0, 0}
  defp do_solve("f", {out, 2, ball}), do: {out, 2, ball}
  defp do_solve("f", {out, strike, ball}), do: {out, strike + 1, ball}
  defp do_solve("p", {2, _, _}), do: {0, 0, 0}
  defp do_solve("p", {out, _, _}), do: {out + 1, 0, 0}
end
Doctests
## Examples 아래에있는 것은 Doctests이라는 것으로 테스트 할 수 있습니다  
 
$ mix test
..............
Finished in 0.07 seconds
13 doctests, 1 test, 0 failures
Wrapping Up 🎍🎍🎍🎍🎍
 
  
 
Reference
이 문제에 관하여(야구의 볼 카운트(어떻게 쓰는)(Elixir)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/torifukukaiou/items/27bdd4dd71eaebd9d410텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)