LoginSignup
1
0

More than 5 years have passed since last update.

Rubyで言語処理100本ノック 第1章:準備運動 (00 ~ 02)

Last updated at Posted at 2018-05-21

趣旨

今までなんとなく触っていたRubyの勉強とチャレンジの備忘録として、
言語処理100本ノック 2015 をやっていきたいと思います。

間違いがあればご指摘いただけると嬉しいです。

00.文字列の逆順

文字列"stressed"の文字を逆に(末尾から先頭に向かって)並べた文字列を得よ.

puts "stressed".reverse

01.「パタトクカシーー」

「パタトクカシーー」という文字列の1,3,5,7文字目を取り出して連結した文字列を得よ.

str = "パタトクカシーー"
puts str.chars.select.with_index{ |c, i| i.even? }.join

02.「パトカー」+「タクシー」=「パタトクカシーー」

「パトカー」+「タクシー」の文字を先頭から交互に連結して文字列「パタトクカシーー」を得よ.

array = ["パトカー", "タクシー"]
array.map{ |str| str.chars }.transpose.join

Array#transpose はネストされた配列を行列と見立てて、行と列を入れ替えてくれるんですね。素敵です。

1
0
2

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
1
0