#### PRG - Midterm 2022 ####
# This is the assignment for the PRG midterm test. This file is a valid Python module, instructions
# are written in the form of comments. Follow the instructions precisely and please note:
#
# - If the module executes any functions when imported (e.g. print), points will be subtracted.
# - If the module fails to be imported (due to errors), the solution is invalid.
#### TASK 1 ####
# With the functions below, change the value of variable 'end_fahrenheits' (and only that) in the
# definition of function 'evaluate_class_experiment', so that the following traceback is obtained
# when the function 'evaluate_experiment()' is called (the paths reported will differ):
# Traceback (most recent call last):
# File "/midterm_2022.py", line 40, in
# evaluate_experiment()
# File "/midterm_2022.py", line 37, in evaluate_experiment
# print("heat capacity:", round(heat_capacity(50, end_celsius - 20), 2), "J/C")
# File "/midterm_2022.py", line 31, in heat_capacity
# return energy / temperature_difference
# ZeroDivisionError: float division by zero
def fahrenheit_to_celsius(temperature):
"""Convert given temperature in fahrenheit degrees into celsius degrees"""
return (temperature - 32) / 0.5556
def heat_capacity(energy, temperature_difference):
"""Compute the heat capacity (J/C) given energy (in Joules) and temperature_difference
(in degree Celsius)"""
return energy / temperature_difference
def evaluate_experiment():
"""Evaluate some example experiment and print the computed heat capacity"""
end_fahrenheits = 56
end_celsius = round(fahrenheit_to_celsius(end_fahrenheits), 6)
print("heat capacity:", round(heat_capacity(50, end_celsius - 20), 2), "J/C")
#### TASK 2 ####
# Fill the body of the function below, so that it corresponds to its docstring.
def random_elements(array, number):
"""Return a given number of elements from the array randomly (without replacement) using
the function 'shuffle' from module 'random'. The input list is not changed.
:param array: List from which elements are drawn randomly (without replacement)
:param number: Number of elements to return
:return: List of random elements
>>> random_elements([1, 2, 3, 4, 5], 2)
[4, 1]
"""
#### TASK 3 ####
# Fill all the question marks in the docstring below and rename 'function', 'parameter', 'variable'
# and 'result', both in the code and the docstring.
def function(parameter):
"""???
:param parameter: ???
:return: ???
Examples
>>> function(???)
???
"""
result = ""
for variable in range(parameter):
result += " " * (parameter-variable) + "*" * (2*variable+1) + "\n"
return result.strip("\n")
#### TASK 4 ####
# Change the function body, so that it follows its docstring and also follows best practices. Do
# not rename the function or change its docstring.
def remove_numbers(text):
"""Remove all numeric characters contained in given text ('a1b2c' -> 'abc')"""
_ = ""
for CHAR in text:
if CHAR in "0123456789":
break
_ += CHAR
return _
return "success"