zt日記

書いているのは Kazuhiro NISHIYAMA a.k.a. ZnZです。
2001|09|10|11|12|
2002|01|02|03|04|05|06|07|08|09|10|11|12|
2003|01|02|03|04|05|06|07|08|09|10|11|12|
2004|01|02|03|04|05|06|07|08|09|10|11|12|
2005|01|02|03|04|05|06|07|08|09|10|11|12|
2006|01|02|03|04|05|06|07|08|09|10|11|12|
2007|01|02|03|04|05|06|07|08|09|10|11|12|
2008|01|02|03|04|05|06|07|08|09|10|11|12|
2009|01|02|03|04|05|06|07|08|09|10|11|12|
2010|01|02|03|04|05|06|07|08|09|10|11|12|
2011|01|02|03|04|05|06|07|08|09|10|
2012|04|05|06|09|

この日記にはツッコミを入れられます。 ツッコミを入れたい日付をクリックすると、フォームが現れます。

xreaでtDiaryを使う方法はxrea.com で tDiary を使う方法インストールメモXREA + tDiary + Namazuをどうぞ。


2008年01月12日(Sat)

[ruby] 第22回 Ruby/Rails勉強会@関西のアレ

問題4のしりとりをやってみたもの。実行時には入力にあわせて-Kオプションの指定が必要。

#!/usr/bin/ruby
$longest = 0 # ugly
def siritori(selected_words, rest_words)
  word = selected_words[-1]
  if word
    last_char = word[/.$/]
    next_words = rest_words.grep(/^#{last_char}/) # TODO: Regexp.escape
  else
    next_words = rest_words # first call
  end
  if next_words.empty?
    if selected_words.size >= $longest
      $longest = selected_words.size
      p [selected_words.size, selected_words]
    end
  else
    next_words.each do |next_word|
      siritori(selected_words + [next_word], rest_words - [next_word])
    end
  end
end
words = $<.read.split(/\W/)
p [words.size, words]
siritori([], words)

copyright © 2001-2013 ZnZ
Key fingerprint = 6E14 2C9C DBD7 874D 8B3C CAA8 9B58 5538 ED7E 1B73