五个基本函数max,min,sum,count,avg的算法

五个基本函数算法

准备阶段
import random
from functools import reduce
from collections import Iterable
test=list(range(11))
isinstance(test,Iterable)
image.png
max()
def max_count(l:list) -> int:
max_num=l[0]
for i in l:
    if max_num < i:
        max_num=i
return max_num
image.png
min()
def min_count(l:list) -> int:
min_num=l[0]
for i in l:
    if min_num > i:
        min_num=i
return min_num
image.png
sum()
def sum_count(l:list) -> int:
sum_num=0
for i in l:
    sum_num+=i
return sum_num
image.png

image.png
count()
def count_len(l:list) -> int:
len_num=0
for i in l:
    len_num+=1
return len_num
image.png
avg()
def ave_count(l:list) -> int:
sum_num=0
for i in l:
    sum_num+=i
return sum_num/count_len(l)
image.png

你可能感兴趣的:(五个基本函数max,min,sum,count,avg的算法)