2
2

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.

競プロにありがちな文字列をスペースで分割して整数値リストに変換するワンライナー

Last updated at Posted at 2016-10-20

競プロにありがちな

0 1 2 3 4

のような文字列を[0, 1, 2, 3, 4]なリストに変換するワンライナー。読み込みは標準入力からを想定している。

Ruby

N = gets.split.map(&:to_i)

気持ちいい。

Perl

my @N = map { $_ + 0 } split ' ', <>;

数値に変換するために+0した。

Perl6

my @N = get().split(' ').map({.Int});

5に比べて若干Rubyっぽくなった。

Python

N = map(lambda s:int(s), raw_input().split(' '))

特に感想はない。

PHP

<?php $N = array_map('intval', explode(' ', trim(fgets(STDIN))));

爆発しそう。

Node.js

var N = require('fs').readFileSync('/dev/stdin', 'utf8').split(' ').map(Number);

Nodeに組み込みのモジュールだけだとこんな感じにしか書けないと思う。

Lua

local N = (function(n) for s in string.gmatch(io.read(), "[^%s]+") do table.insert(n, s) end return n end)({})

標準ライブラリが貧弱なのでワンライナーというにはちょっと無理がある感じになった。

Elixir

"" |> IO.gets |> String.trim_trailing |> String.split(" ") |> Enum.map(&String.to_integer(&1))

もっと賢いやり方があると思う。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?