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

ズンドコキヨシ with SQL

More than 3 years have passed since last update.

関連: http://qiita.com/yancya/items/1167e9764719151d0c80

PostgreSQL の再帰 SQL を使って頑張って書いた

SQL

WITH RECURSIVE t(w) AS (
        VALUES ('')
         UNION
        SELECT w || ('{ズン, ドコ}'::TEXT[])[TRUNC(RANDOM() * 2) + 1]
          FROM t
         WHERE NOT w ~ 'ズンズンズンズンドコ$'
               )
        SELECT w || 'キ・ヨ・シ!'
          FROM t
      ORDER BY LENGTH(w) DESC
         LIMIT 1;

解説

  1. 最初、t は '' という w を持つ行のみ
  2. 次のループで、t の各行の w の末尾に 'ズン' か 'ドコ' を結合した行を UNION(重複を除く) する。ただし、末尾が 'ズンズンズンズンドコ' となっている行を除く。これを繰り返していく
  3. 末尾が 'ズンズンズンズンドコ' となっている行がある状態のループでは、UNION により1行も増加しないので、そこで再帰が終了して t が返る
  4. t の中で w が一番長い行の末尾が 'ズンズンズンズンドコ' となっているはずなので、その行だけを対象に 'キ・ヨ・シ!' を結合して返す
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
ユーザーは見つかりませんでした