-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercises.py
More file actions
281 lines (234 loc) · 9.13 KB
/
exercises.py
File metadata and controls
281 lines (234 loc) · 9.13 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"""
Functions — Exercises
======================
Practice problems to test your understanding of functions.
Try to solve each exercise before looking at the solutions below!
Run this file:
python3 exercises.py
"""
import math
# =============================================================================
# Exercise 1: Area of a circle
#
# Write a function called `area_of_circle` that takes a radius (a number)
# and returns the area of a circle.
#
# Formula: area = pi * radius^2
# Use math.pi for the value of pi.
#
# Examples:
# area_of_circle(5) -> 78.53981633974483
# area_of_circle(1) -> 3.141592653589793
# area_of_circle(10) -> 314.1592653589793
# =============================================================================
def exercise_1():
# YOUR CODE HERE — define area_of_circle, then test it:
# print(f"Radius 5: {area_of_circle(5):.2f}")
# print(f"Radius 1: {area_of_circle(1):.2f}")
# print(f"Radius 10: {area_of_circle(10):.2f}")
pass
# =============================================================================
# Exercise 2: Palindrome checker
#
# Write a function called `is_palindrome` that takes a string and returns
# True if it reads the same forwards and backwards, False otherwise.
#
# Make it case-insensitive (treat "Racecar" the same as "racecar").
# Ignore spaces too (so "taco cat" counts as a palindrome).
#
# Examples:
# is_palindrome("racecar") -> True
# is_palindrome("hello") -> False
# is_palindrome("Madam") -> True
# is_palindrome("taco cat") -> True
# =============================================================================
def exercise_2():
# YOUR CODE HERE — define is_palindrome, then test it:
# print(f"'racecar': {is_palindrome('racecar')}")
# print(f"'hello': {is_palindrome('hello')}")
# print(f"'Madam': {is_palindrome('Madam')}")
# print(f"'taco cat': {is_palindrome('taco cat')}")
pass
# =============================================================================
# Exercise 3: Power function with default parameter
#
# Write a function called `power` that takes a base and an optional exponent.
# If no exponent is provided, it should default to 2 (squaring).
#
# Examples:
# power(5) -> 25 (5 squared)
# power(3, 3) -> 27 (3 cubed)
# power(2, 10) -> 1024
# power(7, 1) -> 7
# =============================================================================
def exercise_3():
# YOUR CODE HERE — define power, then test it:
# print(f"power(5): {power(5)}")
# print(f"power(3, 3): {power(3, 3)}")
# print(f"power(2, 10): {power(2, 10)}")
# print(f"power(7, 1): {power(7, 1)}")
pass
# =============================================================================
# Exercise 4: Average with *args
#
# Write a function called `average` that accepts any number of numeric
# arguments using *args and returns their average.
#
# If no arguments are passed, return 0.0.
#
# Examples:
# average(10, 20, 30) -> 20.0
# average(100) -> 100.0
# average(90, 85, 92, 88) -> 88.75
# average() -> 0.0
# =============================================================================
def exercise_4():
# YOUR CODE HERE — define average, then test it:
# print(f"average(10, 20, 30): {average(10, 20, 30)}")
# print(f"average(100): {average(100)}")
# print(f"average(90, 85, 92, 88): {average(90, 85, 92, 88)}")
# print(f"average(): {average()}")
pass
# =============================================================================
# Exercise 5: Greeting builder with **kwargs
#
# Write a function called `build_greeting` that accepts a required `name`
# parameter and any number of keyword arguments (**kwargs).
#
# It should return a greeting string that includes the name and lists
# all the extra info on separate lines.
#
# Example:
# build_greeting("Alice", role="Engineer", city="Seattle")
#
# Should return:
# "Hello, Alice!\n role: Engineer\n city: Seattle"
#
# When printed, that looks like:
# Hello, Alice!
# role: Engineer
# city: Seattle
#
# If no kwargs are passed, just return "Hello, {name}!"
# =============================================================================
def exercise_5():
# YOUR CODE HERE — define build_greeting, then test it:
# print(build_greeting("Alice", role="Engineer", city="Seattle"))
# print()
# print(build_greeting("Bob"))
# print()
# print(build_greeting("Charlie", language="Python", level="beginner", goal="web dev"))
pass
# =============================================================================
# Exercise 6: Temperature converter
#
# Write a function called `convert_temp` that takes a temperature value
# and a direction string.
#
# Parameters:
# - temp: the temperature to convert (a number)
# - direction: "F_to_C" or "C_to_F" (default: "F_to_C")
#
# Formulas:
# Fahrenheit to Celsius: C = (F - 32) * 5 / 9
# Celsius to Fahrenheit: F = C * 9 / 5 + 32
#
# Examples:
# convert_temp(212) -> 100.0 (boiling point, F to C)
# convert_temp(32) -> 0.0 (freezing point, F to C)
# convert_temp(100, "C_to_F") -> 212.0
# convert_temp(0, "C_to_F") -> 32.0
# =============================================================================
def exercise_6():
# YOUR CODE HERE — define convert_temp, then test it:
# print(f"212F -> C: {convert_temp(212):.1f}")
# print(f"32F -> C: {convert_temp(32):.1f}")
# print(f"100C -> F: {convert_temp(100, 'C_to_F'):.1f}")
# print(f"0C -> F: {convert_temp(0, 'C_to_F'):.1f}")
pass
# =============================================================================
# Solutions (no peeking until you've tried!)
# =============================================================================
def solution_1():
def area_of_circle(radius):
"""Calculate the area of a circle given its radius."""
return math.pi * radius ** 2
print(f"Radius 5: {area_of_circle(5):.2f}")
print(f"Radius 1: {area_of_circle(1):.2f}")
print(f"Radius 10: {area_of_circle(10):.2f}")
def solution_2():
def is_palindrome(text):
"""Check if a string is a palindrome (case-insensitive, ignoring spaces)."""
cleaned = text.lower().replace(" ", "")
return cleaned == cleaned[::-1]
print(f"'racecar': {is_palindrome('racecar')}")
print(f"'hello': {is_palindrome('hello')}")
print(f"'Madam': {is_palindrome('Madam')}")
print(f"'taco cat': {is_palindrome('taco cat')}")
def solution_3():
def power(base, exponent=2):
"""Raise base to the given exponent. Defaults to squaring."""
return base ** exponent
print(f"power(5): {power(5)}")
print(f"power(3, 3): {power(3, 3)}")
print(f"power(2, 10): {power(2, 10)}")
print(f"power(7, 1): {power(7, 1)}")
def solution_4():
def average(*values):
"""Return the average of any number of values. Returns 0.0 if none given."""
if len(values) == 0:
return 0.0
return sum(values) / len(values)
print(f"average(10, 20, 30): {average(10, 20, 30)}")
print(f"average(100): {average(100)}")
print(f"average(90, 85, 92, 88): {average(90, 85, 92, 88)}")
print(f"average(): {average()}")
def solution_5():
def build_greeting(name, **kwargs):
"""Build a greeting string with optional extra info."""
greeting = f"Hello, {name}!"
for key, value in kwargs.items():
greeting += f"\n {key}: {value}"
return greeting
print(build_greeting("Alice", role="Engineer", city="Seattle"))
print()
print(build_greeting("Bob"))
print()
print(build_greeting("Charlie", language="Python", level="beginner", goal="web dev"))
def solution_6():
def convert_temp(temp, direction="F_to_C"):
"""
Convert a temperature between Fahrenheit and Celsius.
Args:
temp: The temperature value to convert.
direction: "F_to_C" or "C_to_F" (default: "F_to_C").
Returns:
The converted temperature as a float.
"""
if direction == "F_to_C":
return (temp - 32) * 5 / 9
return temp * 9 / 5 + 32
print(f"212F -> C: {convert_temp(212):.1f}")
print(f"32F -> C: {convert_temp(32):.1f}")
print(f"100C -> F: {convert_temp(100, 'C_to_F'):.1f}")
print(f"0C -> F: {convert_temp(0, 'C_to_F'):.1f}")
# =============================================================================
# Run it!
# =============================================================================
if __name__ == "__main__":
exercises = [
("Area of a circle", exercise_1),
("Palindrome checker", exercise_2),
("Power function with default", exercise_3),
("Average with *args", exercise_4),
("Greeting builder with **kwargs", exercise_5),
("Temperature converter", exercise_6),
]
for i, (title, func) in enumerate(exercises, 1):
print("=" * 50)
print(f"EXERCISE {i}: {title}")
print("=" * 50)
func()
print()
print("-" * 50)
print("Done! Compare your output with the solutions.")