我想检查一个字符串是否是回文或使用ruby代码。
我是红宝石的首发,因此不太喜欢红宝石中的string methods
答案 0 :(得分:48)
如果您不熟悉Ruby的String
方法,那么您应该看一下documentation,这非常好。 Mithun的回答已经向您展示了基本原则,但由于您是Ruby的新手,还有许多事情要记住:
*)如果你有一个谓词方法,按惯例用一个尾随问号命名,例如: palindrome?
。
*)布尔表达式求值为布尔值,因此您无需显式返回true
或false
。因此,一个简短的习惯版本将是
def palindrome?(str)
str == str.reverse
end
*)由于Ruby的类是开放的,你可以将它添加到字符串类:
class String
def palindrome?
self == self.reverse
end
end
*)如果您不想修补String
,可以直接在单个对象上定义方法(或使用模块和Object#extend):
foo = "racecar"
def foo.palindrome?
self == self.reverse
end
*)您可能希望使回文检查更复杂,例如当涉及到案件或空白时,你也能够发现回文句,像“Racecar”等大写字样。
pal = "Never a foot too far, even."
class String
def palindrome?
letters = self.downcase.scan(/\w/)
letters == letters.reverse
end
end
pal.palindrome? #=> true
答案 1 :(得分:16)
def check_palindromic(variable)
if variable.reverse == variable #Check if string same when reversed
puts "#{ variable } is a palindrome."
else # If string is not the same when reversed
puts "#{ variable } is not a palindrome."
end
end
答案 2 :(得分:9)
递归解决方案显示了如何在Ruby中索引字符串:
def palindrome?(string)
if string.length == 1 || string.length == 0
true
else
if string[0] == string[-1]
palindrome?(string[1..-2])
else
false
end
end
end
如果阅读Ruby字符串文档对您来说太无聊,请尝试使用CodeQuizzes上的Ruby练习题,然后您将学习大多数重要方法。
答案 3 :(得分:5)
def is_palindrome(value)
value.downcase!
# Reverse the string
reversed = ""
count = value.length
while count > 0
count -= 1
reversed += value[count]
end
# Instead of writing codes for reverse string
# we can also use reverse ruby method
# something like this value == value.reverse
if value == reversed
return "#{value} is a palindrom"
else
return "#{value} is not a palindrom"
end
end
puts "Enter a Word"
a = gets.chomp
p is_palindrome(a)
答案 4 :(得分:2)
class String
def palindrome?
self.downcase == self.reverse.downcase
end
end
puts "racecar".palindrome? # true
puts "Racecar".palindrome? # true
puts "mississippi".palindrome? # false
答案 5 :(得分:1)
> first method
a= "malayalam"
if a == a.reverse
puts "a is true"
else
puts "false"
end
> second one
a= "malayalam"
a=a.split("")
i=0
ans=[]
a.count.times do
i=i+1
k=a[-(i)]
ans << k
end
if a== ans
puts "true"
else
puts "false"
end
&#13;
答案 6 :(得分:0)
str= gets.chomp
str_rev=""
n=1
while str.length >=n
str_rev+=str[-n]
n+=1
end
if str_rev==str
puts "YES"
else
puts "NO"
end
答案 7 :(得分:0)
def palindrome?(string)
string[0] == string[-1] && (string.length <= 2 || palindrome?(string[1..-2]))
end
答案 8 :(得分:0)
**Solution 1** Time complexity = O(n), Space complexity = O(n)
This solution does not use the reverse method of the String class. It uses a stack(we could use an array that only allows entry and exit of elements from one end to mimic a stack).
def is_palindrome(str)
stack = []
reversed_str = ''
str.each_char do |char|
stack << char
end
until stack.empty?
reversed_str += stack.pop
end
if reversed_str == str
return true
else
return false
end
end
`解决方案2:时间复杂度= O(n),空间复杂度= O(1)
def inplace_reversal!(str)
i =0
j = str.length - 1
while i < j
temp = str[i]
str[i] = str[j]
str[j] = temp
i+=1
j-=1
end
return str
end