-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday28_speakPython17.py
More file actions
167 lines (111 loc) Β· 3.16 KB
/
Copy pathday28_speakPython17.py
File metadata and controls
167 lines (111 loc) Β· 3.16 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""π§ **Day 28 β Speak Python 17**
### **Phase 6: Balanced Learning (Recursion + Core Topics)**
**Focus Areas:**
* Recursion practice
* Core Python concepts (OOP, File Handling, etc.)
* Debugging real problems
* Mini Project for fun π"""
"""β
Problem 1: Recursion β Print Numbers (1 to n)
π **Task:**
Write a recursive function that prints numbers from 1 to `n`.
π Example:
```python
print\_numbers(5)
\# Output: 1 2 3 4 5
```
π‘ **Hint:**
Print smaller first, then current number."""
def print_numbers(n):
if n == 0:
return
print_numbers(n-1)
print(n)
print_numbers(5)
"""β
Problem 2: Recursion β Sum of Even Numbers
π **Task:**
Write a recursive function that calculates the sum of even numbers up to `n`.
π Example:
```python
sum\_even(6) β 12 # (2+4+6)
```
π‘ **Hint:**
Check if `n` is even β add, otherwise skip.
"""
def sum_even(n):
if n == 0:
return 0
even = n if n % 2 == 0 else 0
return even + sum_even(n-1)
print(sum_even(6))
print(sum_even(10))
"""β
Problem 3: OOP β Student Class
π Task:
Create a Student class with attributes: name, age, grade.
Add a method display_info() to print details.
π Example:
s1 = Student("Jisan", 20, "A")
s1.display_info()
# Output: Name: Jisan, Age: 20, Grade: A"""
class Student:
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
def display_info(self):
print(f"Name: {self.name}, Age: {self.age}, Grade: {self.grade}")
s1 = Student("Jisan", 20, "A")
s2 = Student("Robart", 45, "A")
s1.display_info()
s2.display_info()
"""β
Problem 4: File Handling β Word Counter
π Task:
Write a program that opens a text file sample.txt, counts total words, and prints the result.
π Example:
sample.txt β "Python is fun and powerful"
Output: Total words = 5"""
with open("sample.txt", "w") as f:
f.write("Python is fun and powerful")
with open("sample.txt", "r") as f:
data = f.read()
word_count = len(data.split())
print(f"Total words = {word_count}")
"""βοΈ Mini Project β Simple Calculator (OOP Version)
π Task:
Make a Calculator class with methods: add, subtract, multiply, divide.
Create an object and test all operations.
π Example:
calc = Calculator()
print(calc.add(5, 3)) # 8
print(calc.multiply(4, 2)) # 8"""
class Calculator:
def add(self, num1, num2):
return num1 + num2
def subtract(self, num1, num2):
return num1 - num2
def multiply(self, num1, num2):
return num1 * num2
def divide(self, num1, num2):
try:
return num1 / num2
except ZeroDivisionError:
print("ZeroDivitionError")
calc = Calculator()
print(calc.add(5, 3))
print(calc.multiply(4, 2))
print(calc.subtract(45, 23))
print(calc.divide(89, 8))
"""π Debugging Task β Fix the Code
β Wrong Code:
def factorial(n):
if n == 0:
return 0
return n * factorial(n-1)
print(factorial(5))
β
Expected Output: 120"""
# β
Fixed Code:
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
print(factorial(5))
print(factorial(7))