python
def find_max(lst):
if not lst:
return "Empty list"
max_val = lst[0]
for num in lst:
if num > max_val:
max_val = num
return max_val
shell
#!/bin/bash
find_max() {
local -a lst=("$@")
if [ ${#lst[@]} -eq 0 ]; then
echo "Empty list"
else
max_val=${lst[0]}
for num in "${lst[@]}"; do
if [ $num -gt $max_val ]; then
max_val=$num
fi
done
echo $max_val
fi
}
# 测试函数
echo $(find_max 1 5 3 9 2) # 输出:9
echo $(find_max -1 -5 -3 -9 -2) # 输出:-1
标签:val,max,最大值,寻找,echo,lst,num,简单,find
From: https://www.cnblogs.com/dewan/p/17624406.html