-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday34_speakPython23.py
More file actions
178 lines (125 loc) · 4.05 KB
/
Copy pathday34_speakPython23.py
File metadata and controls
178 lines (125 loc) · 4.05 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
168
169
170
171
172
173
174
175
176
177
178
""" Day 34 – Speak Python 23
Phase 6: Balanced Learning (Recursion + Data Structures + OOP + Debugging + Mini Project)
✅ Problem Set
🔁 Recursion (2 problems)
1. Factorial (Recursive)
Write a recursive function to calculate factorial of a number.
Input: factorial(5) → Output: 120"""
# def factorial(n):
# if n == 0:
# return 1
# return n * factorial(n-1)
# print(factorial(5))
"""2. Sum of Digits (Recursive)
Write a recursive function that returns the sum of digits of a number.
Input: sum_digits(1234) → Output: 10"""
# def sum_digits(d):
# if d // 10 == 0:
# return d
# return (d % 10) + sum_digits(d // 10)
# print(sum_digits(1234))
"""⚡ Other Concepts (2 problems)
3. Dictionary Merging
Merge two dictionaries and handle duplicate keys by summing values.
# dict1 = {"a": 2, "b": 4, "c": 6}
# dict2 = {"a": 3, "c": 1, "d": 7}
# # Output → {"a": 5, "b": 4, "c": 7, "d": 7}"""
def int_dict_marge(dict1, dict2):
try:
marged = dict1.copy()
for k, v in dict2.items():
marged[k] = marged.get(k, 0) + v
return marged
except TypeError:
return "Function onley marge int"
dict1 = {"a": 2, "b": 4, "c": 6}
dict2 = {"a": 3, "c": 1, "d": "7"}
print(int_dict_marge(dict1, dict2))
"""4. Tuple Operations
Given a tuple of numbers, find:
- Maximum value
- Minimum value
- Sum of all numbers"""
# my_tuple = (4, 6, 4, 89, 7, 34)
# # unfuni method
# def main():
# print(max(my_tuple))
# print(min(my_tuple))
# print(sum(my_tuple))
# def max_tup(my_tuple):
# max_num = my_tuple[0]
# for n in my_tuple:
# if n > max_num:
# max_num = n
# return max_num
# def mini_tup(my_tuple):
# mini_num = my_tuple[0]
# for n in my_tuple:
# if n < mini_num:
# mini_num = n
# return mini_num
# def sum_tup(my_tuple):
# my_sum = 0
# for n in my_tuple:
# my_sum += n
# return my_sum
# def arg_sum_tup(*my_tuple):
# my_sum = 0
# for n in my_tuple:
# my_sum += n
# return my_sum
# print(max_tup(my_tuple))
# print(mini_tup(my_tuple))
# print(sum_tup(8, 7, 7)) # TypeError and this function need extra bracket (8, 7, 7) this shodbe right.
# print(arg_sum_tup(8, 7, 7)) # no error and also no need any extra bracket.
"""🛠️ Debugging Task
Fix this code:
class Student:
def __init__(self, name):
self.name = name
class CollegeStudent(Student):
def __init__(self, name, subject):
self.subject = subject
s = CollegeStudent("Jisan", "Python")
print(s.name, s.subject) # Expected: Jisan Python"""
# fixed code:
# class Student:
# def __init__(self, name):
# self.name = name
# class CollegeStudent(Student):
# def __init__(self, name, subject):
# super().__init__(name)
# self.subject = subject
# s = CollegeStudent("Jisan", "Python")
# print(s.name, s.subject)
"""📂 Mini Project
Expense Tracker (OOP + File Handling)
Create a class ExpenseTracker.
Features:
add_expense(amount, category) → add new expense
view_expenses() → view all expenses
total_expense() → show total spending
Save data persistently in expenses.json."""
# import file_utilites as fu
# class ExpenseTracker:
# def __init__(self, filename="expenses.json"):
# self.file = filename
# self.data = fu.load_json(self.file)
# def add_expenses(self, amount, category):
# count = str(len(self.data) + 1)
# self.data[count] = {"amount": amount, "category": category}
# fu.save_json(self.data, self.file)
# def view_expenses(self):
# if not self.data:
# print("No data recorded")
# return
# for vlu in self.data.values():
# print(f"Category: {vlu["category"]}, Amount: {vlu["amount"]}")
# def total_expense(self):
# total = sum(vlu["amount"] for vlu in self.data.values())
# return total
# et = ExpenseTracker()
# et.add_expenses(66, "Drink")
# et.add_expenses(120, "Food")
# et.view_expenses()
# print("Total:", et.total_expense())