嗨,我是 Fly,用 Ruby 寫 Chatbot 並挑戰30天分享心得
為確保不會沒靈感
每日含 Ruby 解題增加內容
https://www.hackerrank.com/domains/tutorials/30-days-of-code
# create the Solution class
class Solution
def initialize
@stack =''
@queue=''
end
def push_character c
@stack += c if c!="\n"
end
def enqueue_character c
@queue += c if c!="\n"
end
def dequeue_character
@queue.reverse
end
def pop_character
@stack
end
end
# create Solution class object
solution = Solution.new
# read the input
input = gets
input.split('').each do |c|
# push the character to stack
solution.push_character c
# enqueue the character to queue
solution.enqueue_character c
end
# check if input string is palindrome or not
is_palindrome = true
(input.length / 2).times do
if solution.pop_character != solution.dequeue_character
is_palindrome = false
break
end
end
# print if string is palindrome or not
if is_palindrome
puts "The word, #{input}, is a palindrome."
else
puts "The word, #{input}, is not a palindrome."
end