-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18_queues_stacks.rb
72 lines (59 loc) · 1.74 KB
/
day18_queues_stacks.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/ruby
# create the Solution class
class Solution
# create two readable & writable instance variables for stack and queue
attr_accessor :stack, :queue
# method to push character onto the stack
def push_character(char)
# if stack is nil, create an array with char and store it inside stack.
if @stack == nil
@stack = [char]
# if stack is not empty, push char to stack array.
else
@stack.push(char)
end
end
# method to enqueue character in queue instance variable
def enqueue_character(char)
# if queue is nil, create an array with char and store it inside queue.
if @queue == nil
@queue = [char]
# if queue is not empty, push char to queue array.
else
@queue.push(char)
end
end
# method to pop and return the character at the top of the stack instance variable.
def pop_character
@stack.pop
end
# method to dequeue and return the first character in the queue instance variable
def dequeue_character
@queue.shift
end
end
### Provided by Hacker Rank:
# 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