Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fire - Blaine #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions lib/max_subarray.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def max_sub_array(nums)
Comment on lines +2 to 4

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The space complexity here is O(1) because you're not building another array etc.

return 0 if nums == nil

raise NotImplementedError, "Method not implemented yet!"
return nil if nums.empty?
# Initialize:
max_so_far = nums[0]
max_ending_here = 0

# Loop for each element of the array
nums.each do |num|
max_ending_here = max_ending_here + num
if max_so_far < max_ending_here
max_so_far = max_ending_here
end
if max_ending_here < 0
max_ending_here = 0
end
end
return max_so_far
end
19 changes: 14 additions & 5 deletions lib/newman_conway.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@


# Time complexity: ?
# Space Complexity: ?
# Time complexity: O(n)
# Space Complexity: O(n)2
def newman_conway(num)
Comment on lines +2 to 4

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Time complexity: O(n)
# Space Complexity: O(n)2
def newman_conway(num)
# Time complexity: O(n)
# Space Complexity: O(n)
def newman_conway(num)

I'm assuming you meant to say space complexity was O(n)

raise NotImplementedError, "newman_conway isn't implemented"
end
raise ArgumentError if num == 0
return "1" if num == 1
arr = [0, 1, 1]

current_index = 3 # why index??
while num >= current_index
arr.push(arr[arr[current_index - 1]] + arr[current_index - arr[current_index - 1]])
current_index += 1
end

return arr[1..arr.length].join(' ')
end
14 changes: 7 additions & 7 deletions test/max_sub_array_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative "test_helper"

xdescribe "max subarray" do
describe "max subarray" do
it "will work for [-2,1,-3,4,-1,2,1,-5,4]" do
# Arrange
input = [-2,1,-3,4,-1,2,1,-5,4]
Expand Down Expand Up @@ -46,14 +46,14 @@
end

it "will return nil for an empty array" do
# Arrange
input = []
# Arrange
input = []

# Act
answer = max_sub_array(input)
# Act
answer = max_sub_array(input)

# Assert
expect(answer).must_be_nil
# Assert
expect(answer).must_be_nil
end

it "will work for [50, -50, 50]" do
Expand Down
52 changes: 26 additions & 26 deletions test/newman_conway_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,31 @@
end

it "works with base cases" do
# Arrange
input = 0

# Act-Assert
expect {
newman_conway(input)
}.must_raise ArgumentError

# Arrange
input = 1

# Act
answer = newman_conway(input)
# Assert
expect(answer).must_equal "1"

# Arrange
input = 2

# Act
answer = newman_conway(input)
# Assert
expect(answer).must_equal "1 1"
# Arrange
input = 0

# Act-Assert
expect {
newman_conway(input)
}.must_raise ArgumentError


# Arrange
input = 1

# Act
answer = newman_conway(input)

# Assert
expect(answer).must_equal "1"

# Arrange
input = 2

# Act
answer = newman_conway(input)

# Assert
expect(answer).must_equal "1 1"
end
end