import numpy as np
import numpy.typing as npt
def replace(a: npt.NDArray, low: int, high: int) -> npt.NDArray:
"""Return a mutated array containing all the same values
as the array a with all values below low replaced with the
number low and all values over high replaces with the number
high"""
maskLow = a < low
maskHigh = a > high
a[maskLow] = low
a[maskHigh] = high
return a
a = np.array([1, 4, 5, 6, 9, 2])
print(replace(a, 3, 6))
print(a)
[3 4 5 6 6 3] [3 4 5 6 6 3]
import numpy as np
temps = np.array([-23, -9, 10, -3, -18, 20, 34])
safe_temps = np.where((temps < -15) | (temps > 30), 1, 0)
np.savetxt("safe_temps.ssv", safe_temps)
class Student:
"""
Store information about a students assignment grades
as a list and compute their average.
"""
grades: list[float]
def __init__(self) -> None:
self.grades = []
def assignment(self, score: float) -> None:
self.grades.append(score)
def average(self) -> float:
if self.grades == []:
return 0.0
average = 0.0
for grade in self.grades:
average += grade
return average / len(self.grades)
studentA = Student()
studentA.assignment(90.0)
studentA.assignment(88.5)
print("grades:", studentA.grades, "avg:", studentA.average())
grades: [90.0, 88.5] avg: 89.25