Add recursive binary search for ruby (ch. 1) and improve recursive max for ruby (ch. 4) (#113)

* add recursive binary search for ruby

* account for cases with empty lists and lists with only one item

* use p instead of print to show nil value

* add newline at end of file
This commit is contained in:
Felice Forby
2019-11-13 00:18:34 +09:00
committed by Aditya Bhargava
parent 6b9faa9cab
commit 4c3bc702f4
2 changed files with 30 additions and 3 deletions

View File

@@ -1,7 +1,8 @@
def max(list)
if list.length == 2
# condition ? then : else
list[0] > list[1] ? list[0] : list[1]
if list.empty?
nil
elsif list.length == 1
numbers[0]
else
sub_max = max(list[1..-1])
list[0] > sub_max ? list[0] : sub_max