Skip to content
Open

KS #21

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
17 changes: 13 additions & 4 deletions lib/max_subarray.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# Four-space tabs courtesey of github's editor ... doing this one straight in the browser because of internet issues

def max(num1, num2)
return num1 > num2 ? num1 : num2
end

# Time Complexity: ?
# Space Complexity: ?
def max_sub_array(nums)
return 0 if nums == nil

raise NotImplementedError, "Method not implemented yet!"

max_now = max = nums[0]

(1...nums.length).each do |i|
max_now = nums[i] > max_now + nums[i] ? nums[i] : max_now + nums[i]
max = max_now if max_now > max
end
Comment on lines +12 to +15

Choose a reason for hiding this comment

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

Very concise

return max
end
24 changes: 19 additions & 5 deletions lib/newman_conway.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
def newman_conway(num)
raise ArgumentError, "must be a natural number" if num <= 0
return "1" if num == 1

m = Array.new(num)
m[0] = 0
m[1] = 1
m[2] = 1
output = "1"
count = 3

while count <= num
m[count] = m[m[count - 1]] + m[count - m[count - 1]]
count += 1
end

# Time complexity: ?
# Space Complexity: ?
def newman_conway(num)
raise NotImplementedError, "newman_conway isn't implemented"
end
(2..num).each do |i|
output += "#{m[i]}"
end
Comment on lines +17 to +19

Choose a reason for hiding this comment

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

This could probably be replaced with:

Suggested change
(2..num).each do |i|
output += "#{m[i]}"
end
return m.join(" ")

return output
end