I’m working on a problem and this works…
def no_repeats(year_start, year_end)
no_repeats = []
(year_start…year_end).each do |yr|
if no_repeat?(yr) == true
no_repeats << yr
end
end
no_repeats
end
def no_repeat?(year)
chars_seen = []
year.to_s.each_char do |char|
return false if chars_seen.include?(char)
chars_seen << char
end
return true
end
…yet this doesn’t work:
def no_repeat?(year)
chars_seen = []
year.to_s.each_char do |char|
if chars_seen.include?(char)
chars_seen << char
return false
end
end
return true
end
Any idea(s) why?
#confused
On Feb 18, 2014, at 6:09 PM, Gabe P. [email protected] wrote:
end
…yet this doesn’t work:
end
Any idea(s) why?
In the last example, how would characters ever get into the chars_seen
array?
If you want to eliminate the postfix if then you might write:
def no_repeat?(year)
chars_seen = []
year.to_s.each_char do |char|
if chars_seen.include?(char)
return false
end
chars_seen << char
end
return true
end
Hope this helps,
Mike
–
Mike S. [email protected]
http://www.stok.ca/~mike/
The “`Stok’ disclaimers” apply.
Because they are two different logic structures
Maybe
if chars_seen.include?(char)
return false
end
chars_seen << char
John
Thanks! Much appreciated.