Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Please complete this by **Monday October 11th**

## Wave 1 Newman-Conway Sequence

[Newman-Conway sequence](https://archive.lib.msu.edu/crcmath/math/math/n/n078.htm) is the one which generates the following integer sequence. 1 1 2 2 3 4 4 4 5 6 7 7….. and follows below recursive formula.
[Newman-Conway sequence](https://archive.lib.msu.edu/crcmath/math/math/n/n078.htm) is the one which generates the following integer sequence. 1 1 2 2 3 4 4 4 5 6 7 7….. and follows the below recursive formula.

Choose a reason for hiding this comment

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

😃


```
P(0) = 1
Expand Down
23 changes: 19 additions & 4 deletions lib/max_subarray.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) where n is the length of the input array
# Space Complexity: O(1)
def max_sub_array(nums)
return 0 if nums == nil
return 0 if nums == nil

raise NotImplementedError, "Method not implemented yet!"
max_so_far = 0
max = nums[0]
i = 0

while i < nums.length
max_so_far += nums[i]

if max_so_far > max
max = max_so_far
end
if max_so_far < 0
max_so_far = 0
end
i += 1
end
return max
end
17 changes: 14 additions & 3 deletions lib/newman_conway.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@


# Time complexity: ?
# Space Complexity: ?
# Time complexity: O(n) where n is the size of the input
# Space Complexity: O(n) where n is the size of the input (space needed for storing each char)
def newman_conway(num)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "newman_conway isn't implemented"
raise ArgumentError, "Number must be larger than 0" if num < 1

result = []
num.times do |i|
if i < 2
result << 1
else
last_char = result[i - 1]
result[i] = result[last_char - 1] + result[i - last_char]
end
end
return result.join(" ")
Comment on lines +8 to +17

Choose a reason for hiding this comment

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

Very elegant solution!

end
2 changes: 1 addition & 1 deletion 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