Skip to content

Commit 36f2984

Browse files
committed
Minor style fixes
1 parent f97e0a4 commit 36f2984

File tree

5 files changed

+29
-16
lines changed

5 files changed

+29
-16
lines changed

.rubocop.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ Metrics/MethodLength:
1212
CountComments: false # count full line comments?
1313
Max: 20
1414

15+
Metrics/BlockLength:
16+
Enabled: true
17+
Exclude:
18+
- spec/**/*
19+
1520
Style/ExtraSpacing:
1621
AllowForAlignment: true
1722
ForceEqualSignAlignment: true
@@ -22,6 +27,9 @@ Style/FrozenStringLiteralComment:
2227
Style/TrailingBlankLines:
2328
Enabled: false
2429

30+
Style/TernaryParentheses:
31+
Enabled: false
32+
2533
AllCops:
2634
TargetRubyVersion: 2.3
2735
DisplayCopNames: true

algorithms/animal_shelter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def enqueue(animal)
3131
def dequeue_any
3232
return @dog_queue.pop if @cat_queue.last.nil?
3333
return @cat_queue.pop if @dog_queue.last.nil?
34+
3435
@cat_queue.first.shelter_number < @dog_queue.first.shelter_number ? @cat_queue.shift : @dog_queue.shift
3536
end
3637

algorithms/sort_stack.rb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# CtCI 6th Edition Problem 3.5
22
# Sort Stack: Write a program to sort a stack such that the smallest items are on the top. You can use
33
# an additional temporary stack, but you may not copy the elements into any other data structure
4-
# (such as an array). The stack supports the following operations: push, pop, peek, and isEmpty.
4+
# (such as an array). The stack supports the following operations: push, pop, peek, and isEmpty.
55

66
class Stack
77
attr_reader :size
@@ -17,7 +17,8 @@ def push(el)
1717
end
1818

1919
def pop
20-
return nil if is_empty?
20+
return nil if empty?
21+
2122
@size -= 1
2223
@stack.pop
2324
end
@@ -26,24 +27,24 @@ def peek
2627
@stack.last
2728
end
2829

29-
def is_empty?
30+
def empty?
3031
@size.zero?
3132
end
3233

3334
# Sorts so that the smallest items are on top
34-
#
35+
# Pops each element, and potentially pops each element back. Worst cash O(n^2) time complexity.
3536
def sort
36-
return self if is_empty?
37+
return self if empty?
38+
3739
temp = Stack.new
3840
temp.push pop
39-
until is_empty?
41+
42+
until empty?
4043
el = pop
41-
if el <= temp.peek
42-
temp.push el
43-
else
44-
push temp.pop until temp.is_empty? || temp.peek <= el
45-
temp.push el
44+
if el > temp.peek
45+
push temp.pop until temp.empty? || temp.peek <= el
4646
end
47+
temp.push el
4748
end
4849
temp
4950
end

data-structures/queue_using_two_stacks.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# CtCI 6th Edition Problem 3.4
2-
# Queue via Stacks: Implement a MyQueue class which implements a queue using two stacks.
2+
# Queue via Stacks: Implement a MyQueue class which implements a queue using two stacks.
33

44
class Stack
55
attr_reader :size
@@ -34,8 +34,9 @@ def add(item)
3434
# Then return what is popped from the pop stack which is the first element added to the queue.
3535
# Then pop the pop stack back onto the push stack reversing the data again to how it was.
3636
# O(n) operation
37-
def remove()
37+
def remove
3838
return nil if @push_stack.size.zero?
39+
3940
@push_stack.size.times { @pop_stack.push @push_stack.pop }
4041
@pop_stack.pop.tap { @pop_stack.size.times { @push_stack.push @pop_stack.pop } }
4142
end

data-structures/set_of_stacks.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# SetOfStacks. push () and SetOfStacks. pop () should behave identically to a single stack
77
# (that is, pop ( ) should return the same values as it would if there were just a single stack).
88
# FOLLOW UP
9-
# Implement a function popAt (int index) which performs a pop operation on a specific sub-stack.
9+
# Implement a function popAt (int index) which performs a pop operation on a specific sub-stack.
1010

1111
class Stack
1212
def initialize
@@ -39,8 +39,9 @@ def push(el)
3939
end
4040

4141
def pop
42-
return nil if @stacks.size == 1 && @last_size.last == 0
43-
if @last_size.last == 0
42+
return nil if @stacks.size == 1 && @last_size.last.zero?
43+
44+
if @last_size.last.zero?
4445
@stacks.pop
4546
@last_size.pop
4647
end
@@ -50,6 +51,7 @@ def pop
5051

5152
def pop_at(index)
5253
return nil if @last_size[index].zero?
54+
5355
@last_size[index] -= 1
5456
@stacks[index].pop
5557
end

0 commit comments

Comments
 (0)