-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday27_speakPython16.py
More file actions
110 lines (76 loc) · 2.38 KB
/
Copy pathday27_speakPython16.py
File metadata and controls
110 lines (76 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""🧠 **Day 27 – Speak Python 16**
### **Phase 5: Recursion Mastery – Real Problem Solving**
**Focus Areas:**
* More Practice with Recursion
* Thinking about Base Case First
* Visualizing Call Stack
* Easy–to–Medium problems only (প্রজেক্ট বাদ)"""
"""✅ Problem 1: Sum of Digits
📌 **Task:**
Write a recursive function to calculate the sum of digits of a number.
📎 **Example:**
```python
sum_digits(123) ➞ 6 # (1+2+3)
sum_digits(9875) ➞ 29
```
💡 **Hint:**
Last digit পাওয়া যাবে `n % 10` দিয়ে, বাকি digit → `n // 10`"""
# def sum_digits(n):
# if n == 0:
# return n
# return (n%10) + sum_digits(n//10)
# print(sum_digits(234))
# print(sum_digits(123))
# print(sum_digits(9875))
"""✅ Problem 2: Product of List (Recursive)
📌 **Task:**
Write a recursive function to multiply all numbers in a list.
📎 **Example:**
```python
product([1, 2, 3, 4]) ➞ 24
product([2, 5, 6]) ➞ 60
```
💡 **Hint:**
`[x]` → return x
Otherwise → first \* product(rest)"""
# def product(lst):
# if len(lst) == 1:
# return lst[0]
# return lst[0] * product(lst[1:])
# print(product([1, 2, 3, 4]))
# print(product([2, 5, 6]))
"""✅ Problem 3: Recursive Max Finder
📌 **Task:**
Find the maximum number in a list using recursion.
📎 **Example:**
```python
recursive_max([3, 5, 2, 9, 1]) ➞ 9
recursive_max([10, 7, 22, 14]) ➞ 22
```
💡 **Hint:**
Compare first element with `recursive_max(rest)`"""
# def recursive_max(lst):
# if len(lst) == 1:
# return lst[0]
# max_result = recursive_max(lst[1:])
# return lst[0] if lst[0] > max_result else max_result
# print(recursive_max([30, 5, 2, 9, 1]))
# print(recursive_max([3, 5, 2, 9, 1]))
# print(recursive_max([10, 7, 22, 14]))
"""✅ Problem 4: Count Occurrences (Recursive)
📌 **Task:**
Count how many times a given element appears in a list using recursion.
📎 **Example:**
```python
count_occurrences([1, 2, 3, 2, 4, 2], 2) ➞ 3
count_occurrences([5, 5, 5, 5], 5) ➞ 4
```
💡 **Hint:**
Check first element, then recurse on the rest of the list."""
# def count_occurrences(lst, terget):
# if not lst:
# return 0
# count = 1 if lst[0] == terget else 0
# return count + count_occurrences(lst[1:], terget)
# print(count_occurrences([1, 2, 3, 2, 4, 2], 2))
# print(count_occurrences([5, 5, 5, 5], 5))