#i want to pass the list, and algorithm (bubblesort) into the sort method with a requirement (temp or wind_speed)

 class Reading:
        def __init__(self, _temperature, _windspeed):
            self.temp = _temperature
            self.windspeed = _windspeed
def bubblesort(num):
        for i in range (len(num)-1, 0, -1):
            for j in range (i):
                if num[j] > num [j+1] :
                    temp = num[j]
                    num[j] = num[j+1]
                    num[j+1] = temp
        return num
 r_list = [Reading(randint(10, 60), randint(10, 60)) for i in range(20)]
def sort(lst, alg): #how do i pass the requirement, and alg?
        bubblesort(lst)

sort(r_list, alg) #how do i create a templated bubblesort to either sort temp or windspeed?

#The Output is supposed to return a sorted list (r_list) according to the requirement

1

There are 1 answers

0
Triceratops On

Here's an example how to pass a function as an argument to another function:

def add(x, y):
    return x + y

def mul(x,y):
    return x * y

def calculate(x, y, func):
    return func(x, y)

z1 = calculate(1, 1, add)
z2 = calculate(1, 1, mul)

print(f"add = {z1}, mul = {z2}")