I want to get general feedback and feedback about if I can make this more Ruby-like, if there are any obvious inefficiencies, and if I need to organize my class differently. The topcoder question is here:
http://community.topcoder.com/stat?c=problem_statement&pm=1835&rd=4655
#!/usr/bin/env ruby -w
require 'set'
class Poetry
def find_ending(str)
vowels = Set.new(['a', 'e', 'i', 'o', 'u'])
vowel_seen = false
ending = ""
str.reverse.each_char.with_index do |c, i|
if vowels.include?(c) or
(c == 'y' and i != 0 and i != str.length-1)
vowel_seen = true
ending = c + ending
elsif !vowel_seen
ending = c + ending
else
break
end
end
ending
end
def rhyme_scheme(poem_array)
scheme = ""
next_label = 'a'
ending_labels = {}
poem_array.each do |line|
if line.strip.empty?
scheme += ' '
else
word = line.split(' ')[-1]
ending = self.find_ending(word.downcase)
label = ending_labels[ending]
if label.nil?
scheme += next_label
puts ending_labels
ending_labels[ending] = next_label
puts ending_labels
if next_label == 'z'
next_label = 'A'
else
next_label = next_label.next
end
else
scheme += label
end
end
end
scheme
end
end
poetry = Poetry.new
poem_array = ["I hope this problem",
"is a whole lot better than",
"this stupid haiku"]
puts "'#{poetry.rhyme_scheme(poem_array)}'"
and
andor
, which are control flow operators. Use&&
and||
for boolean logic, they are not the same asand
andor
. – meagar May 23 at 20:37