Skip to content

Commit a777aa6

Browse files
authored
Merge pull request egonSchiele#167 from moogacs/master
add recursive soln. for python/03_recursion
2 parents 1459b89 + 345d09b commit a777aa6

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

03_recursion/python/04_count.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def count(arr):
2+
if not arr:
3+
return 0
4+
return 1 + count(arr[1:])
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def binary_search(arr, target):
2+
if not arr:
3+
return -1
4+
if len(arr) == 1 and arr[0] == target:
5+
return arr[0]
6+
if len(arr) == 1 and arr[0] != target:
7+
return -1
8+
low = 0
9+
high = len(arr) - 1
10+
mid = (low + high) // 2
11+
12+
if arr[mid] > target:
13+
return binary_search(arr[:mid], target)
14+
else:
15+
return binary_search(arr[mid+1:], target)

03_recursion/python/06_find_max.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def find_max(arr):
2+
if len(arr) == 2:
3+
return arr[0] if arr[0] > arr[1] else arr[1]
4+
sub_max = find_max(arr[1:])
5+
return arr[0] if arr[0] > sub_max else sub_max

03_recursion/python/07_sum_array.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def sum_array(arr):
2+
if not arr:
3+
return 0
4+
return arr[0] + sum_array(arr[1:])

0 commit comments

Comments
 (0)