Reputation: 67
I wrote a piece of code to return false
when there is a repeated digit in the number passed to the method like this.
no_repeat(2114567) #=> false
The following is the code. I could not find what is wrong with it. Any suggestion to improve this please.
def no_repeat(x)
x = x.to_s.split('')
i = 0
while i < x.length
if x[i].to_s == x[i + 1]
false
end
i += 1
end
true
end
no_repeat(2114567) #=> true
Upvotes: 3
Views: 143
Reputation: 1749
If you want to know if a digit in a number shows up more than once
x.to_s.chars - x.to_s.chars.uniq == 0
If you'd like to know if a digit shows up consecutively in a number you could try this
x.to_s.chars.each_cons(2).to_a.count{|array|array.uniq.length == 1} > 0
Upvotes: 0
Reputation: 110695
def no_repeat(n)
s = n.to_s
s.size == s.squeeze.size
end
no_repeat(2114567) #=> false
no_repeat(-2114567) #=> false
no_repeat(2141567) #=> true
I suggest you change the method so it returns true
when there are repeated digits, and rename it to something like repeated_digits?
.
Upvotes: 1
Reputation: 369194
false
does not return function unless it is the last expression of the function; explicitly return it.
def no_repeat(x)
x = x.to_s.split('')
i = 0
while i < x.length
if x[i].to_s == x[i + 1]
return false # <--------
end
i += 1
end
true
end
no_repeat(2114567) # => false
no_repeat(1234) # => true
'12345'.each_char.each_cons(2).any? { |x, y| x == y } false '11345'.each_char.each_cons(2).any? { |x, y| x == y } true
Alternative using regular expression (capturing group, backreference):
def no_repeat(x)
! (/(.)\1/ === x.to_s)
end
Another alternative suggested by p11y using each_cons
:
'12345'.each_char.each_cons(2).none? { |x, y| x == y }
# => true
'11345'.each_char.each_cons(2).none? { |x, y| x == y }
# => false
'12345'.each_char.each_cons(2).all? { |x, y| x != y }
# => true
'11345'.each_char.each_cons(2).all? { |x, y| x != y }
# => false
Upvotes: 6