Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • cp/02002students
  • cp/02002-students-internal
2 results
Show changes
Commits on Source (27)
Showing
with 112 additions and 137 deletions
......@@ -9,22 +9,29 @@ cache.db
*.artifacts.pkl
.coveragerc
*_tests_complete_grade.py
tmp.txt
tmp.zip
# Lock file. DISABLE checking with gitlab (and syncing), so you *wnat* this on your computer, but *not* on the students computer.
**/unitgrade_data/dont_check_remote.lock
cp/ex03/simple_function.py
cp/tests/unitgrade_data/Week01Dummy.pkl
cp/tests/unitgrade_data/Week01Palindrome.pkl
######################## Comment these out upon release. #############################
cp/ex05
cp/ex06
cp/ex07
cp/ex08
cp/ex09
cp/ex10
cp/ex11
cp/ex12
cp/exam
cp/project1
# cp/project2
cp/project3
#cp/project1
#cp/project2
#cp/project3
cp/project4
cp/project5
cp/project6
cp/tests/tests_week01.py
#*04*
#*05*
#*06*
*07*
*08*
*09*
*10*
*11*
*12*
"""A handful of python programming examples."""
# Example 1
print("Hello world")
# Example 2
print("hello")
print("world")
a = 2
b = 3
c = 1
x = 3
y = a*x**2 + b * x + c
# Example 3
def second_poly(x):
"""Compute a first order polynomial.
:param x: Input value :math:`x`
:return: :math:`y = 2x + 4`
"""
return 2 * x + 4
# example 4
def say_hello(name):
"""Print out "Hello {name}.
:param name: The name to say hello to
"""
print("Hello " + name)
"""Exercise 2.3: The Fibonacci sequence and recursion."""
def fibonacci_number(k):
"""Compute the :math:`k`'th Fibonacci number :math:`x_k`.
The function use the recursive relationship :math:`x_{k+1} = x_k + x_{k-1}`, along with the initial conditions
:math:`x_0 =0, x_1 = 1`, to complete the sequence up to :math:`k`.
:param k: An integer
:return: The corresponding term :math:`x_k` of the Fibonacci sequence as an integer.
"""
if k == 0:
# TODO: 1 lines missing.
raise NotImplementedError("Insert your solution and remove this error.")
elif k == 1:
# TODO: 1 lines missing.
raise NotImplementedError("Insert your solution and remove this error.")
else:
# TODO: 1 lines missing.
raise NotImplementedError("Fix this stuff")
if __name__ == "__main__":
print("The term x_0 should be 0, you got:", fibonacci_number(0))
print("The term x_1 should be 1, you got:", fibonacci_number(1))
print("The term x_2 should be 1, you got:", fibonacci_number(1))
print("The term x_6 should be 8, you got:", fibonacci_number(6))
"""Exercise 2.1: Printing full name."""
def full_name(first_name:str, last_name:str):
"""Determine and print the full name given first name and last name.
:param first_name: your first name.
:param last_name: your last name.
"""
# TODO: Code has been removed from here.
raise NotImplementedError("Insert your solution and remove this error.")
"""Exercise: Estimate fetal weight using Hadlock formula."""
import math
def hadlock(head_circ:float,abdominal_circ:float,femur_length:float):
"""Estimate fetal weight using Hadlock formula.
:param head_circ: head circumference in cm.
:param abdominal_circ: abdominal circumference in cm.
:param femur_length: femur length in cm.
"""
# TODO: Code has been removed from here.
raise NotImplementedError("Insert your solution and remove this error.")
"""Exercise 2.3: Printing number of characters in name."""
def name_length(name:str):
"""Calculate and print the number of characters in name.
:param name: your name.
"""
# TODO: Code has been removed from here.
raise NotImplementedError("Insert your solution and remove this error.")
"""Exercise 2.2: Round a number to the next nearest thousand."""
import math
def next_thousand(a:int):
"""Round a number to the next nearest thousand and print.
:param a: the number to be rounded.
"""
# TODO: Code has been removed from here.
raise NotImplementedError("Insert your solution and remove this error.")
"""Exercise 2.5: Calculate the normal weight range for a given height."""
import math
def normal_weight(height:float):
"""Calculate and print the range of normal weights for a given height.
:param height: the height.
"""
# TODO: Code has been removed from here.
raise NotImplementedError("Insert your solution and remove this error.")
"""Exercise 2.6: Calculate the lowest survival temperature."""
def survival_temperature(metabolic_heat:int, thermal_conductance:float):
"""Calculate and print the lowest survival temperature.
:param metabolic_heat: the metabolic heat production.
:param thermal_conductance: the thermal conductance.
"""
# TODO: Code has been removed from here.
raise NotImplementedError("Insert your solution and remove this error.")
"""Problem 1.1."""
def evaluate_taylor(x: float) -> float:
r"""Compute the third order taylor approximation.
Compute (and return) the Taylor approximation of :math:`\log(x)`. Remember the function must return a ``float``.
:param x: The input to evaluate.
:return: The taylor approximation of :math:`\log(x)`, i.e. :math:`y`
"""
# TODO: 1 lines missing.
raise NotImplementedError("Define y = (x-1) - 1/2 * ... here.")
return y
"""Problem 1.2. Implement a function that computes (and return) the third order Taylor-approximation here."""
# TODO: Code has been removed from here.
raise NotImplementedError("Insert your solution and remove this error.")
"""Problem 1.3. Compute the third order taylor approximation evaluated in x here and store it in a variable y."""
x = 1.5
# Define y = ... here.
# TODO: 1 lines missing.
raise NotImplementedError("Insert your solution and remove this error.")
# print(y) should work.
"""Exercise: Convert length in foot and inch to centimeter."""
import math
def unit_conversion(foot:int, inch:int):
"""Convert length in foot and inch to centimeter.
:param foot: foot portion of the length in imperical unit.
:param inch: inch portion of the length in imperical unit.
"""
# TODO: Code has been removed from here.
raise NotImplementedError("Insert your solution and remove this error.")
"""Exercise 2.4: Wind chill."""
def wind_chill(temperature:int, windspeed:float):
"""Calculate and print the wind chill temperatures.
:param temperature: the actual temperature.
:param windspeed: the wind speed.
"""
# TODO: Code has been removed from here.
raise NotImplementedError("Insert your solution and remove this error.")
"""Exercise 3.9: Ackermann's function."""
"""Exercise 3.10: Ackermann's function."""
def ackermann(m:int, n:int):
"""Compute the Ackermann's function :math:`A(m, n)`.
......@@ -9,5 +9,4 @@ def ackermann(m:int, n:int):
:param n: the variable n.
:return: the computed value :math:`A(m,n)`.
"""
# TODO: 5 lines missing.
raise NotImplementedError("Insert your solution and remove this error.")
# TODO: Code has been removed from here.
"""Exercise 3.5: BAC Calculator."""
"""Exercise 3.6: BAC Calculator."""
def bac_calculator(alcohol_consumed: float, weight: float, gender: str, time: float) -> float:
"""Calculate the blood alcohol concentration based on the alcohol consumed, body weight, and time since consumption.
......@@ -10,6 +9,4 @@ def bac_calculator(alcohol_consumed: float, weight: float, gender: str, time: fl
:param time: The time elapsed since alcohol consumption in hours (float)
:return: The calculated blood alcohol concentration (BAC) as a float value.
"""
# TODO: 10 lines missing.
raise NotImplementedError("Insert your solution and remove this error.")
return bac
# TODO: Code has been removed from here.
......@@ -4,19 +4,12 @@ import math
def f(x : float) -> float:
r"""Find the roots of this function.
You should implement the function :math:`f(x)` here. It is defined as:
You should implement the function f(x) here.
.. math::
f(x) = \sin(3\cos(\frac{1}{2} x^2))
:param x: The value to evaluate the function in :math:`x`
:return: :math:`f(x)`.
:param x: The value to evaluate the function in x
:return: f(x)
"""
# TODO: 1 lines missing.
raise NotImplementedError("Compute f(x) here.")
return y
# TODO: Code has been removed from here.
def is_there_a_root(a : float, b : float) -> bool:
......@@ -26,9 +19,7 @@ def is_there_a_root(a : float, b : float) -> bool:
:param b: Highest x-value to consider
:return: ``True`` if we are guaranteed there is a root otherwise ``False``.
"""
# TODO: 1 lines missing.
raise NotImplementedError("Compute the condition here")
return has_root
# TODO: Code has been removed from here.
def bisect(xmin : float, xmax : float, delta : float) -> float:
"""Find a candidate root within ``xmin`` and ``xmax`` within the given tolerance.
......@@ -38,19 +29,5 @@ def bisect(xmin : float, xmax : float, delta : float) -> float:
:param delta: The tolerance.
:return: The first value :math:`x` which is within ``delta`` distance of a root according to the bisection algorithm
"""
# TODO: 6 lines missing.
raise NotImplementedError("Insert your solution and remove this error.")
if __name__ == "__main__":
print("Are we guaranteed there is a zero within the interval [1, 3]?", is_there_a_root(1, 3), "(this should be True)")
print("Are we guaranteed there is a zero within the interval [1, 3.5]?", is_there_a_root(1, 3.5), "(this should be False)")
print("Find a zero within tolerance 0.1 in the interval [1, 2]")
print(bisect(1, 2, 0.1))
print("Same, but with a tolerance of 0.5")
print(bisect(1, 2, 0.1))
# TODO: Code has been removed from here.
print("Should return math.nan")
print(bisect(1, 3.5, 0.1))
"""Exercise 3.4: Body Temperature."""
def body_temperature(temperature : float) -> str:
def body_temperature(temperature):
"""Calculate the body's response based on the given temperature.
:param temperature: The temperature in degrees Celsius.
:return: The body's response as a string.
"""
# TODO: 10 lines missing.
raise NotImplementedError("Insert your solution and remove this error.")
return response
# TODO: Code has been removed from here.
"""Exercise 3.3: Compare numbers."""
"""Exercise 3.5: Compare numbers."""
def compare_numbers(first_number:int, second_number:int) -> str:
"""Return a string based on which number has the greatest numerical value.
......@@ -7,6 +7,4 @@ def compare_numbers(first_number:int, second_number:int) -> str:
:param second_number: second number.
:return: string stating which number is the greatest.
"""
# TODO: 6 lines missing.
raise NotImplementedError("Insert your solution and remove this error.")
return comparison
# TODO: Code has been removed from here.
"""Exercise 3.8: Exponential function."""
"""Exercise 3.9: Exponential function."""
def exponential(x : float, n : int) -> float:
"""Compute the exponential :math:`x^n` using recursion.
......@@ -9,5 +9,4 @@ def exponential(x : float, n : int) -> float:
:param n: the power :math:`n`.
:return: the computed value.
"""
# TODO: 6 lines missing.
raise NotImplementedError("Insert your solution and remove this error.")
# TODO: Code has been removed from here.