-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercises.py
More file actions
197 lines (145 loc) · 5.48 KB
/
exercises.py
File metadata and controls
197 lines (145 loc) · 5.48 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
# Exercise 1: Calculate Area of a Triangle
#
# Write a function named `calculate_area_triangle` that takes the base and height of a triangle and returns the area.
# The area formula is (base * height) / 2.
#
# Examples:
# calculate_area_triangle(10, 5) should return 25.0.
# calculate_area_triangle(7, 3) should return 10.5.
#
# Define your function and call it below.
def calculate_area_triangle(base, height):
return (base * height) / 2
print("Exercise 1:", calculate_area_triangle(7, 3))
# Exercise 2: Calculate Simple Interest
#
# Write a function named `simple_interest` that takes principal, rate of interest (as a percentage), and time (years).
# Calculate and return the simple interest using the formula (principal * rate * time) / 100.
#
# Examples:
# simple_interest(1000, 5, 2) should return 100.
# simple_interest(1500, 3.5, 5) should return 262.5.
#
# Define your function and call it to see the result.
def simple_interest(principal, ROI, time):
return (principal * ROI * time) / 100
print("Exercise 2:", simple_interest(1500, 3.5, 5))
# Exercise 3: Apply a Discount
#
# Write a function named `apply_discount` that takes a product's price and a discount percentage (from 0 to 100).
# Return the new price after applying the discount.
#
# Examples:
# apply_discount(100, 25) should return 75.
# apply_discount(80, 10) should return 72.
#
# Define your function and call it to display the discounted price.
def apply_discount(Pprice, dis):
return Pprice - (Pprice * (dis / 100))
print("Exercise 3:", apply_discount(80, 10))
# Exercise 4: Convert Temperature
#
# Write a function called `convert_temperature` that takes a
# temperature and a unit ('C' for Celsius, 'F' for Fahrenheit)
# and converts the temperature to the other unit.
# The formula for converting Celsius to Fahrenheit is (Celsius * 9/5) + 32.
# The formula for converting Fahrenheit to Celsius is (Fahrenheit - 32) * 5/9.
#
# Examples:
# convert_temperature(0, 'C') should return 32.0.
# convert_temperature(32, 'F') should return 0.0.
#
# Define the function and then call it below.
def convert_temperature(temp, unit):
if unit == "C":
return (temp * 9 / 5) + 32
else:
return (temp - 32) * 5 / 9
print("Exercise 4: Convert 0°C to Fahrenheit:", convert_temperature(0, "C"))
print("Exercise 4: Convert 32°F to Celsius:", convert_temperature(32, "F"))
# Exercise 5: Sum to N
#
# Write a function named `sum_to` that takes a single integer n and returns the sum of all integers from 1 to n.
#
# Examples:
# sum_to(6) should return 21.
# sum_to(10) should return 55.
#
# Define the function and then call it below.
def sum_to(one_num):
sum = 0
for num in range(one_num + 1):
sum += num
return sum
print("Exercise 5:", sum_to(10))
# Exercise 6: Find the Largest Number
#
# Write a function named `largest` that takes three integers as arguments and returns the largest of them.
#
# Examples:
# largest(1, 2, 3) should return 3.
# largest(10, 4, 2) should return 10.
#
# Define your function and test it with different inputs.
def largest(*args):
large = args[0] # assume first is the largest num
for num in args:
if num > large:
large = num
return large
print("Exercise 6:", largest(10, 4, 2))
# Exercise 7: Calculate a Tip
#
# Create a function called `calculate_tip`. It should take the bill amount and the tip percentage (as a whole number).
# The function should return the amount of the tip.
#
# Examples:
# calculate_tip(50, 20) should return 10.
#
# Write your function and test its output below.
def calculate_tip(bill, tipPrc):
return bill * (tipPrc / 100)
print("Exercise 7:", calculate_tip(50, 20))
# Exercise 8: Calculate Product of Numbers
#
# Write a function named `product` that takes an arbitrary number of numbers, multiplies them, and returns the product.
# Review your notes on *args for handling an arbitrary number of arguments.
#
# Examples:
# product(-1, 4) should return -4.
# product(2, 5, 5) should return 50.
#
# Define the function and call it with different sets of numbers to test.
def product(nN, *args):
FProdunct = nN
for mult in args:
FProdunct *= mult
return FProdunct
print("Exercise 8:", product(2, 5, 5))
# Exercise 9: Basic Calculator
#
# Create a function named `basic_calculator` that takes three arguments:
# two numbers and a string representing an operation ('add', 'subtract', 'multiply', 'divide').
# Perform the provided operation on the two numbers. In operations where the order of numbers is important,
# treat the first parameter as the first operand and the second parameter as the second operand.
#
# Examples:
# basic_calculator(10, 5, 'subtract') should return 5.
# basic_calculator(10, 5, 'add') should return 15.
# basic_calculator(10, 5, 'multiply') should return 50.
# basic_calculator(10, 5, 'divide') should return 2.
#
# Define the function and then call it below.
def basic_calculator(Fnum, Snum, oper):
if oper == "subtract":
return (lambda num1, num2: num1 - num2)(Fnum, Snum)
elif oper == "add":
return (lambda num1, num2: num1 + num2)(Fnum, Snum)
elif oper == "multiply":
return (lambda num1, num2: num1 * num2)(Fnum, Snum)
elif oper == "divide":
return (lambda num1, num2: num1 / num2)(Fnum, Snum)
print("Exercise 9 Result:", basic_calculator(10, 5, "subtract"))
print("Exercise 9 Result:", basic_calculator(10, 5, "add"))
print("Exercise 9 Result:", basic_calculator(10, 5, "multiply"))
print("Exercise 9 Result:", basic_calculator(10, 5, "divide"))