2013-04-07 87 views
0

显然,哈希不适用于这种测试。不管怎么说,这是我到目前为止有:如何检查一个适当的枚举是否是回文

module Enumerable 
    def palindrome? 
    arr = [] 
    self.reverse_each do |x| 
     arr << x 
    end 
    self == arr 
    end 
end 

另一个想法我是循环ARR和自我逐个元素与一个for循环来检查。

+0

这是任何机会,一门功课的问题吗? – Jason 2013-04-07 16:09:04

回答

2
module Enumerable 
    def palindrome? 
    a = to_a 
    a == a.reverse 
    end 
end 
+0

超级简单,谢谢! – Rishi 2013-04-07 04:40:39

2
x = 'Tiger' 
p "#{x} is planidrome" if x == x.reverse #=> no ouput 
x = 'RADAR' 
p "#{x} is planidrome" if x == x.reverse #=> "RADAR is planidrome" 

module Enumerable 
    def palindrome? 
    p self.to_a == self.to_a.reverse 
    end 
end 
['r','a','d','a','r'].palindrome? #=> true 
+0

这是用于字符串,而不是枚举类型。 – Rishi 2013-04-07 04:31:33

+0

@ user1796994看我编辑 – 2013-04-07 04:45:36