add recursive soln.
This commit is contained in:
4
03_recursion/python/04_count.py
Normal file
4
03_recursion/python/04_count.py
Normal file
@@ -0,0 +1,4 @@
|
||||
def count(arr):
|
||||
if not arr:
|
||||
return 0
|
||||
return 1 + count(arr[1:])
|
||||
15
03_recursion/python/05_binary_search_recursive.py
Normal file
15
03_recursion/python/05_binary_search_recursive.py
Normal 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)
|
||||
5
03_recursion/python/06_find_max.py
Normal file
5
03_recursion/python/06_find_max.py
Normal 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
|
||||
4
03_recursion/python/07_sum_array.py
Normal file
4
03_recursion/python/07_sum_array.py
Normal file
@@ -0,0 +1,4 @@
|
||||
def sum_array(arr):
|
||||
if not arr:
|
||||
return 0
|
||||
return arr[0] + sum_array(arr[1:])
|
||||
Reference in New Issue
Block a user