Skip to content

Commit

Permalink
Run rubocop autocorrrect
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanpaulsio committed Aug 21, 2023
1 parent c53a2ec commit 6aa77a0
Show file tree
Hide file tree
Showing 37 changed files with 143 additions and 165 deletions.
2 changes: 1 addition & 1 deletion lib/arrays_and_strings/dutch_national_flag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.run(arr, index)

target = arr[index]

while (mid + 1 < hi)
while mid + 1 < hi
if arr[mid + 1] < target
arr[mid + 1], arr[lo + 1] = arr[lo + 1], arr[mid + 1]
lo += 1
Expand Down
2 changes: 1 addition & 1 deletion lib/arrays_and_strings/reverse_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def self.run(arr)
i = 0
j = arr.length - 1

while (i < j)
while i < j
arr[i], arr[j] = arr[j], arr[i]
i += 1
j -= 1
Expand Down
8 changes: 5 additions & 3 deletions lib/arrays_and_strings/shortest_subarray_unsorted.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ def self.run(arr)
end

break if arr[i + 1] <= item

i += 1
end

# find bump
arr.reverse_each do |item|
break if arr[j - 1] > item

j -= 1
end

Expand All @@ -32,11 +34,11 @@ def self.run(arr)
max = arr[i..j].max

# expands the dip left
i -= 1 while (i > 0 && arr[i - 1] > min)
i -= 1 while i > 0 && arr[i - 1] > min

# expand the bump right
j += 1 while (j < arr.length - 1 && arr[j + 1] < max)
j += 1 while j < arr.length - 1 && arr[j + 1] < max

return [i, j]
[i, j]
end
end
14 changes: 7 additions & 7 deletions lib/arrays_and_strings/shortest_subarray_unsorted_test.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
require_relative '../../test_helper'
require_relative 'shortest_subarray_unsorted'
require_relative "../../test_helper"
require_relative "shortest_subarray_unsorted"

describe ShortestSubarrayUnsorted do
describe 'regular cases' do
it 'should find subarray towards the middle' do
describe "regular cases" do
it "should find subarray towards the middle" do
expected = [2, 4]
actual = ShortestSubarrayUnsorted.run [1, 2, 4, 5, 3, 5, 6, 7, 9]
expect(actual).must_equal expected
end

it 'should work when subarray reaches end of array' do
it "should work when subarray reaches end of array" do
expected = [1, 8]
actual = ShortestSubarrayUnsorted.run [1, 3, 5, 2, 10, 8, 7, 8, 9]
expect(actual).must_equal expected
end

it 'should returns nil for sorted arrays' do
it "should returns nil for sorted arrays" do
actual = ShortestSubarrayUnsorted.run [1, 2, 3, 4, 5]
expect(actual).must_be_nil
end

it 'should work for array of length 3' do
it "should work for array of length 3" do
expected = [0, 1]
actual = ShortestSubarrayUnsorted.run [1, 0, 3]
expect(actual).must_equal expected
Expand Down
4 changes: 2 additions & 2 deletions lib/arrays_and_strings/sliding_window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def self.run(arr, target:)
elsif sum < target
if j + 1 < arr.length
j += 1
sum = sum + arr[j]
sum += arr[j]
else
next
end
Expand All @@ -32,6 +32,6 @@ def self.run(arr, target:)
end
end

return nil
nil
end
end
4 changes: 2 additions & 2 deletions lib/arrays_and_strings/two_sum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ def self.run(arr, target)
i = 0
j = arr.length - 1

while (i < j)
while i < j
sum = arr[i] + arr[j]
i += 1 if sum < target
j -= 1 if sum > target
return [i, j] if sum == target
end

return nil
nil
end
end
2 changes: 1 addition & 1 deletion lib/arrays_and_strings/zero_sum_subarray.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ def self.run(arr)
hsh[sum] = index
end

return nil
nil
end
end
2 changes: 1 addition & 1 deletion lib/binary_search/binary_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def self.run(arr, target:)
low = 0
high = arr.length - 1

while (low <= high)
while low <= high
mid = low + (high - low) / 2

if arr[mid] == target
Expand Down
2 changes: 1 addition & 1 deletion lib/binary_search/closest_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ def self.run(arr, target:)
end
end

return result
result
end
end
24 changes: 12 additions & 12 deletions lib/binary_search/cyclically_sorted_min_test.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
require_relative '../../test_helper'
require_relative 'cyclically_sorted_min'
require_relative "../../test_helper"
require_relative "cyclically_sorted_min"

describe CyclicallySortedMin do
describe 'edge cases' do
it 'should return -1 given an empty array' do
describe "edge cases" do
it "should return -1 given an empty array" do
actual = CyclicallySortedMin.run([])
expect(actual).must_equal(-1)
end
end

describe 'base cases' do
it 'should return 0 given a single item array' do
describe "base cases" do
it "should return 0 given a single item array" do
actual = CyclicallySortedMin.run([1])
expect(actual).must_equal 0
end

it 'should return 0 given a non-rotated double item array' do
it "should return 0 given a non-rotated double item array" do
actual = CyclicallySortedMin.run([1, 2])
expect(actual).must_equal 0
end

it 'should return 1 given a rotated double item array' do
it "should return 1 given a rotated double item array" do
actual = CyclicallySortedMin.run([2, 1])
expect(actual).must_equal 1
end
end

describe 'regular cases' do
it 'should return 0 given a sorted array' do
describe "regular cases" do
it "should return 0 given a sorted array" do
actual = CyclicallySortedMin.run([0, 1, 2, 3, 4, 5, 6])
expect(actual).must_equal 0
end

it 'should find the index of a rotated array' do
it "should find the index of a rotated array" do
actual = CyclicallySortedMin.run([3, 4, 5, 6, 0, 1, 2])
expect(actual).must_equal 4
end

it 'should find the array min at the end of an array' do
it "should find the array min at the end of an array" do
actual = CyclicallySortedMin.run([3, 4, 5, 6, 7, 8, 2])
expect(actual).must_equal 6
end
Expand Down
2 changes: 1 addition & 1 deletion lib/binary_search/search_insert_position.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ def self.run(arr, target:)
end
end

return low
low
end
end
22 changes: 11 additions & 11 deletions lib/binary_search/search_insert_position_test.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
require_relative '../../test_helper'
require_relative 'search_insert_position'
require_relative "../../test_helper"
require_relative "search_insert_position"

describe SearchInsertPosition do
describe 'edge cases' do
it 'should insert item into an empty array' do
describe "edge cases" do
it "should insert item into an empty array" do
actual = SearchInsertPosition.run([], target: 0)
expect(actual).must_equal 0
end
end

describe 'regular cases' do
it 'should insert before existing target item' do
describe "regular cases" do
it "should insert before existing target item" do
actual = SearchInsertPosition.run([1, 3, 5, 6], target: 3)
expect(actual).must_equal 1
end

it 'should insert at 0th index when target < arr[0]' do
it "should insert at 0th index when target < arr[0]" do
actual = SearchInsertPosition.run([1, 3, 5, 6], target: 0)
expect(actual).must_equal 0
end

it 'should insert at 1st index' do
it "should insert at 1st index" do
actual = SearchInsertPosition.run([1, 3, 5, 6], target: 2)
expect(actual).must_equal 1
end

it 'should insert at 2nd index' do
it "should insert at 2nd index" do
actual = SearchInsertPosition.run([1, 3, 5, 6], target: 4)
expect(actual).must_equal 2
end

it 'should insert at 3rd index' do
it "should insert at 3rd index" do
actual = SearchInsertPosition.run([1, 3, 5, 7], target: 6)
expect(actual).must_equal 3
end

it 'should insert at last index' do
it "should insert at last index" do
actual = SearchInsertPosition.run([1, 3, 5, 7], target: 100)
expect(actual).must_equal 4
end
Expand Down
4 changes: 3 additions & 1 deletion lib/binary_search/square_root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

class SquareRoot
def self.run(i)
result, low, high = [0, 0, i]
result = 0
low = 0
high = i

while low <= high
mid = low + (high - low) / 2
Expand Down
3 changes: 2 additions & 1 deletion lib/binary_search/unknown_length.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def self.run(arr, target:)
upper_limit = 1
while true
break if arr[upper_limit].nil?

upper_limit *= 2
end

Expand Down Expand Up @@ -49,6 +50,6 @@ def self.run(arr, target:)
end
end

return -1
-1
end
end
26 changes: 14 additions & 12 deletions lib/linked_lists/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ def get(n)

begin
raise IndexError if node.nil?
(n - 1).times { |i| node = node.next }

(n - 1).times { |_i| node = node.next }
rescue IndexError
puts "No node at index #{n}"
end
Expand All @@ -27,7 +28,7 @@ def append(node)

def delete(node, prev_node)
if node.nil?
return
nil
elsif node == head && prev_node.nil?
self.head = node.next
elsif node == tail
Expand All @@ -39,12 +40,12 @@ def delete(node, prev_node)
end

def create_cycle_at(target_node)
self.tail.next = target_node
tail.next = target_node
end

def has_cycle?
fast = self.head
slow = self.head
fast = head
slow = head

until fast.nil?
fast = fast.next
Expand All @@ -62,8 +63,8 @@ def has_cycle?
end

def cycle_length
fast = self.head
slow = self.head
fast = head
slow = head

until fast.nil?
fast = fast.next
Expand All @@ -89,13 +90,14 @@ def cycle_length
end

def median
fast = self.head
slow = self.head
fast = head
slow = head

until fast.nil?
fast = fast.next
fast = fast.next unless fast.nil?
break if fast.nil?

slow = slow.next unless slow.next.nil?
end

Expand All @@ -104,8 +106,8 @@ def median

def cycle_head
# Initialize two pointers
fast = self.head
slow = self.head
fast = head
slow = head

# Move the fast pointer the cycle length
# By the time the fast pointer reaches the "tail",
Expand All @@ -122,7 +124,7 @@ def cycle_head
end

def self.generate(*values)
list = self.new
list = new
values.each { |v| list.append Node.new(v) }
list
end
Expand Down
2 changes: 1 addition & 1 deletion lib/linked_lists/linked_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

describe "when querying an empty list" do
it "should print an error message" do
-> { LinkedList.new.get(1) }.must_output(/No node at index 1/)
_ { LinkedList.new.get(1) }.must_output(/No node at index 1/)
end
end
end
Expand Down
Loading

0 comments on commit 6aa77a0

Please sign in to comment.