Merge pull request #167 from moogacs/master

add recursive soln. for python/03_recursion
This commit is contained in:
Aditya Bhargava
2020-09-14 10:56:40 -05:00
committed by GitHub
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:])