1
1

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.

ずんだの菱形アルファベット問題 Ruby編

Last updated at Posted at 2018-11-05

ルール

  • 入力は整数の1-25まで
  • 入力が偶数だった場合は "invalid" と出力する
  • 入力の数値が2増えるごとに、出力の行と列が1行増え、1行進むごとに使用する文字は a, b, c と変化していく
  • 出力は以下のような菱形になり、菱形を形作るのは a-y の文字で、それ以外の箇所は半角のアンダースコアとする

出力例

入力 1

$ test.rb 1
a

入力 5

$ test.rb 5
__a__
_b_b_
c___c
_d_d_
__e__

入力 25

$ test.rb 25
____________a____________
___________b_b___________
__________c___c__________
_________d_____d_________
________e_______e________
_______f_________f_______
______g___________g______
_____h_____________h_____
____i_______________i____
___j_________________j___
__k___________________k__
_l_____________________l_
m_______________________m
_n_____________________n_
__o___________________o__
___p_________________p___
____q_______________q____
_____r_____________r_____
______s___________s______
_______t_________t_______
________u_______u________
_________v_____v_________
__________w___w__________
___________x_x___________
____________y____________

入力 2

$ test.rb 2
invalid

プログラム(Ruby)

on_char = 'a'
count = ARGV.first.to_i
row_base = Array.new(count, '_')
rows = Array.new(count)
if count.even?
  puts "invalid"
  exit(0)
end

count.times do |i|
  j = i < (count / 2) ? i : count - i - 1
  row = row_base.dup
  center = count / 2
  row[center + j] = on_char
  row[center - j] = on_char
  rows[i] = row
  on_char = on_char.succ
end

puts rows.map(&:join).join("\n")
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?