4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Elixir]文字列を一文字ずつsplitしたいとき

4
Last updated at Posted at 2020-06-28

AtCoderで問題を解いているときのこと、
Elixirを使って文字列を一文字ずつsplitしたいことがあったので、書き方をメモしておこうと思います。

String.codepoints
https://hexdocs.pm/elixir/String.html#codepoints/1

iex(1)> String.codepoints("hogehoge")
["h", "o", "g", "e", "h", "o", "g", "e"]

別の関数に渡したいときは|>を使って

placing_marble.ex
defmodule Main do
  def placing_marble(seq) do
    seq |> String.codepoints
        |> Enum.map(&String.to_integer(&1))
        |> Enum.reduce(0, &(&1 + &2))
  end

  def main do
    seq =  IO.read(:line) |> String.trim()
    IO.puts placing_marble(seq)
  end

end

ソースは下記の問題を解いたときのものです
A - Placing Marbles

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?