Help us understand the problem. What is going on with this article?

ズンドコキヨシ with Ruby

More than 3 years have passed since last update.

参考: http://qiita.com/y__sama/items/7c451dfd706f9b36729d

ひとまず1つめの実装を張っておきます
他に思いついたら足していきます

単にループ

def zundoko
  z, d = zd = ['ズン', 'ドコ']
  w1, w2, w3, w4, w5 = 5.times.map { nil }

  until [w1, w2, w3, w4, w5] == [z, z, z, z, d]
    w1, w2, w3, w4, w5 = w2, w3, w4, w5, zd.sample.tap(&method(:print))
  end

  print 'キ・ヨ・シ!'
end

zundoko

Enumerator

なるほど

def zundoko
  z, d = zd = ['ズン', 'ドコ']

  loop.lazy.map { zd.sample }.each_cons(5).with_index do |nominees, i|
    i.zero? ? nominees.each(&method(:print)) : print(nominees.last)
    break if nominees == [z, z, z, z, d]
  end

  print 'キ・ヨ・シ!'
end

zundoko

その2

def zundoko
  z, d = zd = ['ズン', 'ドコ']

  loop.lazy.map { zd.sample }.reduce([]) do |nominees, w|
    break print('キ・ヨ・シ!') if nominees == [z, z, z, z, d]
    nominees.last(4) + [w.tap(&method(:print))]
  end
end

zundoko

再帰

Z, D = ZD = ['ズン', 'ドコ']

def zundoko(nominees = [])
  return print('キ・ヨ・シ!') if nominees == [Z, Z, Z, Z, D]

  zundoko(nominees.last(4) + [ZD.sample.tap(&method(:print))])
end

zundoko
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした