------ art_165006_19573430.1167788635874
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Here is my solution. It supports ? and * as wildcard letters.
-Chunyun
Sample input:
U E W R T R B H C D
C X U Z U W R Y E R
R O C K S B A U C U
Y F K F M T Y S G E
Y S O O U N M E I M
T C G P R T I D A N
Y Z G H Q G W T U R
H B M N D X Z B U T
N T U L A T N X C E
Y B U R P Z Y X M S
Ru?y Year ro*s DAN matZ
Output:
+ + + R + + + + + +
+ + U + U + + + + +
R O C K S B + + + +
Y + + + + + Y + + +
+ + + + + + + E + M
+ + + + + + + D A N
Y + + + + + + T + R
+ B + + + + Z + U +
+ + U + + + + X + +
Y B U R + + Y + + +
Code:
# Synopsis
#This is the solution to Ruby Quiz #107 described on
http://www.rubyquiz.com/quiz107.html.
#
# Usage
# ruby word_search.rb
# OR
# ruby word_search.rb input_file
#
# Author
# Chunyun Zhao(chunyun.zhao / gmail.com)
#
class WordSearch
attr_reader :found_coords
def initialize(box)
@box ox
@height box.size
@width box[0].size
end
def search_words(words)
@found_coords
regexize_words(words)
each_line do |line_coords|
line_str et_line_str(line_coords)
words.each do |word|
if line_str #{word}/i
offset ~.offset(0)
@found_coords | ine_coords[offset[0]...offset[1]]
end
end
end
end
def display_words
for x in 0...@height
for y in 0...@width
@found_coords.include?([x,y])? print(@box[x][y]):print('+')
print ' '
end
puts
end
end
private
#Generates all possible lines(represented as arrays of coordinates) from
@box , and
#calls the block with the line coordinates.
def each_line
vertical_line_proc ambda {|x, y| [x+1, y]}
horizonal_line_proc ambda {|x, y| [x, y+1]}
backward_diagonal_line_proc ambda {|x, y| [x+1, y+1]}
forward_diagonal_line_proc ambda {|x, y| [x+1, y-1]}
lines ]
#Genernates the lines starting with the top horizonal line
for y in 0...@width
lines << get_line_coords(0, y, &vertical_line_proc)
lines << get_line_coords(0, y, &backward_diagonal_line_proc)
lines << get_line_coords(0, y, &forward_diagonal_line_proc)
end
#Genernates the lines starting with the leftmost and rightmost vertical
lines
for x in 0...@height
lines << get_line_coords(x, 0, &horizonal_line_proc)
lines << get_line_coords(x, 0, &backward_diagonal_line_proc)
lines << get_line_coords(x, @width-1, &forward_diagonal_line_proc)
end
lines.each{|line_coords|yield line_coords; yield line_coords.reverse}
end
#Generates the line starting with coordinate [x,y]. It calls the block to
find the
#next position in the line. It can be used to generate snake lines if
necessary.
def get_line_coords(x, y)
line [x,y]]
loop do
next_x, next_y ield x, y
@box[next_x] && @box[next_x][next_y] ? line << [next_x, next_y] :
break
x, y ext_x, next_y
end
line
end
#Gets the string represented by an array of coordinates.
def get_line_str(coords)
line_str '
coords.each{|x,y| line_str << @box[x][y]}
line_str
end
#Replaces ? and * with \w? and \w* in each word so that it could be used
#as the regex to support wildcard letter matching.
def regexize_words(words)
words.each {|word|word.gsub!(/(\?|\*)/, '\w\1')}
end
end
box ]
width il
while line
ョョソ
ョ
「ヤ 。「 。ョソ
。 ョ
ョ
シシ
「ル 。「 ョソ
ョ
「ル 。「 ョソ
モョィゥ
ョ゜ィゥ
ョ゜
゜゜ナホト゜゜
マ アイッイケッーカャ メ ム シタョセ コ
セ
セ ヤ メ ムコ
セ
セ アョ ミ
セ
セ エク ョ
セ
セ イョ モ メ ム コ
セ
セ コッッョョッ
セ
セ ウョ ナ。
セ
セ モコ チ ロムユノレン
セ
セ メ ヤ ョ ミ
セ ャ
セ ョ
セ
セ
セ ュ ュ ュ ュ ュ ュ ュ ュ ュ ュ ュ ュ ュ ュ ュ ュ ュ ュ ュ ュ
セ
セ ト ニ
セ
セ ヤァ ァ ャ
セ
セ ョ ヤ
セ
セ
セ コ
セ ャ
セ ョ
セ
セ ニ ャ ャ ャ ャ
セ コ
セ
セ ユ ナ ラ メ ヤ メ ツ ネ テ ト
セ テ リ ヌ レ ユ ラ メ ル ナ メ
セ メ マ テ ヒ モ ツ チ ユ テ ユ
セ モ ニ ヒ ニ ヘ ヤ ル モ ヌ ナ
セ ル モ マ マ ユ ホ ヘ レ ノ ヘ
セ ヤ テ ヌ ミ メ ヤ ノ ト チ ホ
セ ネ レ ヌ ネ ム ヌ ラ ヤ ユ ヨ
セ ネ ム ヘ ホ ト リ レ ツ モ ヤ
セ ホ ヤ テ フ チ ヤ ホ ツ テ ナ
セ ル ツ ユ メ ミ レ ユ リ ヘ モ
セ
セ ヤ コ
セ
セ ォ ォ ォ メ ォ ォ ォ ォ ォ ォ
セ ォ ォ ォ ォ ユ ォ ォ ォ ォ ォ
セ メ マ テ ヒ モ ツ ォ ォ ォ ォ
セ ォ ォ ォ ォ ォ ォ ル ォ ォ ォ
セ ォ ォ ォ ォ ォ ォ ォ ォ ォ ヘ
セ ォ ォ ォ ォ ォ ォ ォ ト チ ホ
セ ォ ォ ォ ォ ォ ォ ォ ヤ ォ ォ
セ ォ ォ ォ ォ ォ ォ レ ォ ォ ォ
セ ォ ォ ォ ォ ォ ォ ォ ォ ォ ォ
セ ォ ォ ォ ォ ォ ォ ォ ォ ォ ォ
セ
セ ホ ャ
セ
セ ョ モ ョ
セ
セ ヤ
セ
セ ャ ョョャ モヤトノホ ャ
セ
セ ャ ョ チ
セ
セ ョ ヤ
セ
セ ャ
セ
セ ョ
セ
セ 、 ョッョ
セ ユナラメヤメツネテト
セ テリヌレユラメルナメ
セ メマテヒモツチユテユ
セ モニヒニヘヤルモヌナ
セ ルモママユホヘレノヘ
セ ヤテヌミメヤノトチホ
セ ネレヌネムヌラヤユヨ
セ ネムヘホトリレツモヤ
セ ホヤテフチヤホツテナ
セ ルツユメミレユリヘモ
セ
セ メャ ャ トチホャ レ
セ
セ ホャ ャ ャ ノ
セ ョ
セ ラ ャ ャ ュ
セ
セ ョ ラ ァ ャ
セ オ カ ィゥ ョ
セ
セ ェ チ ョ ヤ
セ
セ ァ
セ ョ ヤ ャ ノ ァ
セ 。
セ ェ チ 「「 ャ ャ
セ ァ ョ
セ ェ チ ャ ョョャ 「ヤ
セ
セ ョ「
セ ェ ト ョ
セ ェ チ ョ
セ
セ
ュュュュュュ ゜アカオーーカ゜アケオキウエウーョアアカキキククカウオクキエュュ