Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [RUBY QUIZ #107]

Reply
Thread Tools

[RUBY QUIZ #107]

 
 
Alex Watt
Guest
Posts: n/a
 
      01-01-2007
Unfortunately, I've only done 1 extra credit thing (wildcard characters). I
couldn't think of a good way of doing the snaking text without adding many
more lines, so I didn't.

class Array
def positions(value)
positions = []
each_index { |i| positions << i if self[i] == value }
positions
end
end

class WordSearch
def initialize(wordSearch,*words)
@wordSearch = wordSearch
@rows = @wordSearch.split(/\s+/).delete_if { |row| row == "" }
@letters = @wordSearch.gsub(/\s+/,"").split(//)
@words = *words
@letterPositions = []
@rowLength = @rows[0].length
@numberOfRows = @rows.length
end
def solve
@words.each { |word|
@letters.positions(word[0].chr).each { |letterPos|
tooCloseToRight = (@rowLength - (letterPos % @rowLength)) <
word.length
tooCloseToLeft = letterPos % @rowLength < word.length
tooCloseToTop = (letterPos / @rowLength) < word.length
tooCloseToBottom = (@numberOfRows - (letterPos / @rowLength).to_i) <
word.length
search(word,letterPos,1) unless tooCloseToRight
# to the right
search(word,letterPos,-1) unless tooCloseToLeft
# to the left

search(word,letterPos,-(@rowLength - 1)) unless tooCloseToTop or
tooCloseToRight # top right diagonal
search(word,letterPos,-(@rowLength)) unless tooCloseToTop
# above
search(word,letterPos,-(@rowLength + 1)) unless tooCloseToTop or
tooCloseToLeft # top left diagonal

search(word,letterPos,@rowLength + 1) unless tooCloseToBottom or
tooCloseToRight # bottom right diagonal
search(word,letterPos,@rowLength) unless tooCloseToBottom
# below
search(word,letterPos,@rowLength - 1) unless tooCloseToBottom or
tooCloseToLeft # bottom left diagonal
}
}
end
def search(word,pos,direction)
positions = []
for i in (0...word.length)
positions << (pos + direction*i) if word[i].chr == @letters[pos +
direction*i] or word[i].chr == "*" or @letters[pos + direction*i] == "*"
end
@letterPositions << positions if positions.length == word.length
end
def printWordSearch
@letterPositions.flatten!
@letters.each_index { |i|
character = @letterPositions.include?(i) ? @letters[i] : "+"
print character
if (i + 1) % @rows[0].length == 0
print "#{i/@rows[0].length + 1}".rjust(6)
puts
end
}
end
end

wordSearch = []
loop do
row = gets.chomp
wordSearch << row
break if row == ""
end
wordSearch.delete_at(-1)
words = gets.chomp.upcase.split(/\s*,\s*/)
w = WordSearch.new(wordSearch.join("\n"),words)
w.solve
w.printWordSearch

__________________________________________________ _______________
Find Singles In Your Area This Christmas With Match.com! msnuk.match.com


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[QUIZ] Gathering Ruby Quiz 2 Data (#189) Daniel Moore Ruby 10 01-31-2009 08:36 PM
[QUIZ] Newbie doubts about the quiz Marcelo Alvim Ruby 15 08-16-2006 02:08 PM
[QUIZ] 1-800-THE-QUIZ (#20) Ruby Quiz Ruby 15 02-24-2005 06:05 AM
[SOLUTION] Ruby Quiz #15 Animal Quiz David Tran Ruby 9 01-21-2005 02:11 AM
[QUIZ] Animal Quiz (#15) Ruby Quiz Ruby 11 01-18-2005 02:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57