add recursive soln.

This commit is contained in:
=
2020-06-03 15:28:01 +02:00
parent d8da439590
commit 345d09b69e
4 changed files with 28 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
def count(arr):
if not arr:
return 0
return 1 + count(arr[1:])

View File

@@ -0,0 +1,15 @@
def binary_search(arr, target):
if not arr:
return -1
if len(arr) == 1 and arr[0] == target:
return arr[0]
if len(arr) == 1 and arr[0] != target:
return -1
low = 0
high = len(arr) - 1
mid = (low + high) // 2
if arr[mid] > target:
return binary_search(arr[:mid], target)
else:
return binary_search(arr[mid+1:], target)

View File

@@ -0,0 +1,5 @@
def find_max(arr):
if len(arr) == 2:
return arr[0] if arr[0] > arr[1] else arr[1]
sub_max = find_max(arr[1:])
return arr[0] if arr[0] > sub_max else sub_max

View File

@@ -0,0 +1,4 @@
def sum_array(arr):
if not arr:
return 0
return arr[0] + sum_array(arr[1:])