Skip to content

Commit

Permalink
more recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
scoutjohn committed Dec 1, 2024
1 parent 2b11e7f commit e0e3058
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion AIML/Numpy/Numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
print()

print("random matrix of size, standard random variable")
e=np.random.rand(4)
e=np.random.randn(4)
print(e)
print()

Expand Down
20 changes: 20 additions & 0 deletions DSA/recursion/String.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def reverse(s):
if not s:
return ""
return reverse(s[1:])+s[0]

def count_specific_char_in_string(s,_char):
if not s:
return 0
if s[0]==_char:
return 1 + count_specific_char_in_string(s[1:],_char)
else:
return count_specific_char_in_string(s[1:],_char)


if __name__ == '__main__':
print(f"!NataSha = {reverse('!NataSha')}")

str = "axbxcx"
c = "x"
print(f"{c} in {str} is repeated {count_specific_char_in_string(str,c)} times")
15 changes: 15 additions & 0 deletions DSA/recursion/SumOfNumbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def sum_of_numbers(array):
if not array:
return 0
# this statement is not needed because , a[1:] on an array with one value will give empty array
#if(len(array))==1:
#return array[0]
return array[0]+sum(array[1:])


if __name__ == '__main__':
arr = [1,2,3,4]
print(f"sum of {arr} is {sum_of_numbers(arr)}")

arr = [10,20,3,14]
print(f"sum of {arr} is {sum_of_numbers(arr)}")

0 comments on commit e0e3058

Please sign in to comment.