-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday38_speakPython27.py
More file actions
152 lines (106 loc) · 3.52 KB
/
Copy pathday38_speakPython27.py
File metadata and controls
152 lines (106 loc) · 3.52 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
"""Phase 7: Real-World Thinking (Recursion + Data Structures + OOP + Debugging + Mini Project)
✅ Problem Set
🔁 Recursion (2 problems)
Greatest Common Divisor (GCD – Recursive)
Write a recursive function to find the GCD of two numbers using the Euclidean algorithm.
Input: gcd(48, 18)
Output: 6"""
def gcd(a, b):
if a % b == 0:
return b
return gcd(b, a%b)
# print(gcd(48, 18))
"""String Length (Recursive)
Write a recursive function to calculate the length of a string without using len().
Input: str_len("python")
Output: 6"""
def str_len(s):
if s == "":
return 0
return 1 + str_len(s[1:])
# print(str_len("jisan"))
"""⚡ Other Concepts (2 problems)
Tuple Comprehension (Generator Expression)
Create a generator expression that yields squares of numbers from 1 to 10.
Output: 1, 4, 9, 16, ..., 100"""
seq = (n * n for n in range(1, 11))
for x in seq:
#print(x, end=", ")
pass
"""Dictionary Inversion
Given:
students = {"Alice": 1, "Bob": 2, "Charlie": 3}
Invert it to:
{1: "Alice", 2: "Bob", 3: "Charlie"}"""
students = {"Alice": 1, "Bob": 2, "Charlie": 3}
inv = {v: k for k, v in students.items()}
#print(inv)
"""🛠️ Debugging Task
Buggy code:
class Rectangle:
def __init__(self, width, height):
self.width = width
height = height
def area(self):
return self.width * self.height
r = Rectangle(5, 10)
print(r.area()) # Expected: 50
👉 Fix the error so that it works correctly. """
# Fixed code:
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
r = Rectangle(5, 10)
# print(r.area())
"""👉 Fix the error so that it works correctly.
📂 Mini Project – Expense Tracker (OOP + File Handling)
Create a simple Expense Tracker.
Features:
add_expense(category, amount) → add a new expense
view_expenses() → display all expenses
total_expenses() → show total amount spent
remove_expense(category) → remove expense by category"""
# All expenses should be saved in expenses.json.
from file_utilites import save_json, load_json
class ExpenseTracker:
def __init__(self, file="expensea.json"):
self.file = file
self.expenses = load_json(self.file)
def add_expense(self, category, amount):
count = str(len(self.expenses) + 1)
self.expenses[count] = {"category": category, "amount": amount}
save_json(self.expenses, self.file)
def view_expenses(self):
if self.expenses:
for main in self.expenses.values():
print(f"Category: {main["category"]}, Amount: {main["amount"]}")
else:
print("Empty")
def total_expenses(self):
if self.expenses:
total = sum([main["amount"] for main in self.expenses.values()])
print(f"Total amount: {total}")
else:
print("Empty")
def remove_expense(self, category):
found = False
for key, value in list(self.expenses.items()):
if value["category"] == category:
found = key
break
if found:
del self.expenses[key]
save_json(self.expenses, self.file)
print(f"Category removed: {category}")
else:
print("Category not foune")
# test:
# et = ExpenseTracker()
# et.add_expense("frout", 89)
# et.add_expense("game", 6799)
# et.view_expenses()
# et.total_expenses()
# et.remove_expense("game")