编写程序,利用元组作为函数的返回值,求序列类型中的最大值、最小值和元素个数,并编写测试代码,假设测试数据分别为s1=[9,7,8,3,2,1,55,6]、s2=["apple","pear","melon","kiwi"]和s3="TheQuickBrownFox"。
时间: 2023-09-29 19:08:23 浏览: 181
下面是使用元组作为函数返回值的程序代码:
```python
def sequence_info(seq):
"""
获取序列类型中的最大值、最小值和元素个数
"""
max_val = max(seq)
min_val = min(seq)
count = len(seq)
return max_val, min_val, count
```
接下来编写测试代码:
```python
s1 = [9,7,8,3,2,1,55,6]
s2 = ["apple","pear","melon","kiwi"]
s3 = "TheQuickBrownFox"
max_val, min_val, count = sequence_info(s1)
print(f"s1: max={max_val}, min={min_val}, count={count}")
max_val, min_val, count = sequence_info(s2)
print(f"s2: max={max_val}, min={min_val}, count={count}")
max_val, min_val, count = sequence_info(s3)
print(f"s3: max={max_val}, min={min_val}, count={count}")
```
输出结果如下:
```
s1: max=55, min=1, count=8
s2: max=pear, min=apple, count=4
s3: max=x, min= , count=19
```
需要注意的是,当序列类型为字符串时,最大值和最小值是根据字符的 ASCII 码进行比较的。
阅读全文