-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsolidated_exercises.py
More file actions
172 lines (139 loc) · 4.07 KB
/
consolidated_exercises.py
File metadata and controls
172 lines (139 loc) · 4.07 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
# consolidated_exercises.py
# Oba Ozai 2024
# Description: A collection of Python exercises for arithmetic, geometry, temperature conversions, and more.
# ----------------------------------------------
# 1. Temperature Conversion Functions
# ----------------------------------------------
def fahrenheit_to_celsius(f):
"""
Convert Fahrenheit to Celsius.
Formula: (F - 32) * 5/9
"""
return (f - 32) * 5 / 9
def celsius_to_fahrenheit(c):
"""
Convert Celsius to Fahrenheit.
Formula: (C * 9/5) + 32
"""
return (c * 9 / 5) + 32
def celsius_to_kelvin(c):
"""
Convert Celsius to Kelvin.
Formula: C + 273.15
"""
return c + 273.15
def kelvin_to_celsius(k):
"""
Convert Kelvin to Celsius.
Formula: K - 273.15
"""
return k - 273.15
# Example usage:
# print(fahrenheit_to_celsius(98.6)) # Output: 37.0
# ----------------------------------------------
# 2. Determine Type of Triangle
# ----------------------------------------------
def triangle_type(a, b, c):
"""
Determine the type of triangle based on its sides.
Equilateral: All sides equal.
Isosceles: Two sides equal.
Scalene: No sides equal.
"""
if a == b == c:
return "Equilateral"
elif a == b or b == c or a == c:
return "Isosceles"
else:
return "Scalene"
# Example usage:
# print(triangle_type(3, 3, 3)) # Output: Equilateral
# ----------------------------------------------
# 3. Calculate the Sum of Numbers from 0 to N
# ----------------------------------------------
def sum_up_to_n(n):
"""
Calculate the sum of all numbers from 0 to n using a for loop.
Formula: n * (n + 1) / 2
"""
return sum(range(n + 1))
# Example usage:
# print(sum_up_to_n(10)) # Output: 55
# ----------------------------------------------
# 4. Print Even Numbers from 0 to N
# ----------------------------------------------
def print_even_numbers(n):
"""
Print all even numbers from 0 to n using a while loop.
"""
i = 0
while i <= n:
if i % 2 == 0:
print(i)
i += 1
# Example usage:
# print_even_numbers(10)
# ----------------------------------------------
# 5. Area of Common Shapes
# ----------------------------------------------
import math
def area_of_circle(radius):
"""
Calculate the area of a circle.
Formula: π * r^2
"""
return math.pi * radius ** 2
def area_of_triangle(base, height):
"""
Calculate the area of a triangle.
Formula: 0.5 * base * height
"""
return 0.5 * base * height
def area_of_polygon(sides, side_length):
"""
Calculate the area of a regular polygon.
Formula: (n * s^2) / (4 * tan(π / n))
"""
return (sides * side_length ** 2) / (4 * math.tan(math.pi / sides))
# Example usage:
# print(area_of_circle(5)) # Output: 78.54
# print(area_of_triangle(10, 5)) # Output: 25.0
# print(area_of_polygon(6, 4)) # Output: 41.57 (Regular Hexagon)
# ----------------------------------------------
# 6. Character Printing from a String
# ----------------------------------------------
def print_characters(s):
"""
Print each character of a string on a new line using a loop.
"""
for char in s:
print(char)
# Example usage:
# print_characters("Hello")
# ----------------------------------------------
# 7. Numpy Array from a List
# ----------------------------------------------
import numpy as np
def list_to_numpy_array(sample_list):
"""
Convert a Python list to a Numpy array.
"""
return np.array(sample_list)
# Example usage:
# sample_list = [101, 102, 103, 104]
# print(list_to_numpy_array(sample_list))
# ----------------------------------------------
# 8. Print Multiplication Table for N
# ----------------------------------------------
def print_multiplication_table(n):
"""
Print the multiplication table for a given number n.
"""
for i in range(1, 11):
print(f"{n} x {i} = {n * i}")
# Example usage:
# print_multiplication_table(5)
# ----------------------------------------------
# End of File
# ----------------------------------------------
#EOF