2025-01-28
Max. 100 points
Name:
Task | Max. | Achieved |
---|---|---|
1 | 12 | |
2 | 24 | |
3 | 24 | |
4 | 40 | |
Sum | 100 |
Statement | True | False |
---|---|---|
Python is a compiled language. | ||
Tuples are mutable. | ||
The == operator is used to compare values. | ||
Object oriented programming is a programming paradigm that uses classes. | ||
Python packages can be downloaded and installed using the
numpy command. | ||
PuLP is a library that focuses on data analysis. |
def my_function(n):
return n % 2 == 1
print(my_function(3))
def my_function(n):
return [i**2 for i in range(n)]
print(my_function(4))
def my_function(my_list):
return my_list.sortby(t for t in [str, int, int])
print(my_function(["c", 3, 1]))
def my_function(n):
for i in range(n):
if i / 5 == 1:
return i
x = my_function(10)
class my_class():
def __init__(self, x):
self.x = x
def my_method(self, y):
return (self.x**2 + y**2)**(0.5)
foo = my_class(3)
print(foo.my_method(4))
def print_together(a, b):
print(a, b)
print_together(b='luck!', a='Good')
count_non_zeros(lst)
that takes a list of
integers as argument. The function must return the number of non-zeros
in the list.
Use a list comprehension to solve this problem, and write a proper
docstring including a doctest to get the full points.
def cumsum(l: List[int]) -> List[int]:
"""
Return the number of non-zeros in the list
>>> count_non_zeros([0, 0]])
0
>>> count_non_zeros([0, 1])
1
"""
if not l: return []
r = l[:1]
for e in l[1:]:
r.append(r[-1] + e)
return r
generate_colors(resistance)
that takes a desired resistance value and returns a list of colors to
be printed. For example:
>>> generate_colors(330)
['orange', 'orange', 'brown']
>>> generate_colors(8200)
['grey', 'red', 'red']
Use function annotations and a docstring including a doctest to receive
full points.
values = [ # value corresponds to index of color
"black", "brown", "red", "orange", "yellow", "green", "blue", "violet",
"grey", "white"]
def generate_colors(resistance: int) -> list[str]:
"""
Generate the colors on a resistor given its resistance.
>>> generate_colors(330)
['orange', 'orange', 'brown']
"""
colors = []
resistance = str(resistance)
colors.append(values[int(resistance[0])])
colors.append(values[int(resistance[1])])
colors.append(values[len(resistance[2:])])
return colors