在这个小任务中,您将获得一串空格分隔的数字,并且必须返回最高和最低的数字。
例:
high_and_low("1 2 3 4 5") # return "5 1"
high_and_low("1 2 -3 4 5") # return "5 -3"
high_and_low("1 9 3 4 -5") # return "9 -5"
说明:
实现方法:
#!/usr/bin/env python
# coding=utf-8
# Codewars Problem: 001
# python3获得一串空格分隔的数字,并且必须返回最高和最低的数字
# 方法一
class Solution1:
def high_and_low(self,numbers):
x = []
for i in numbers.split():
if i != " ":
x.append(int(i))
numbers = str(max(x)) + " " + str(min(x))
return numbers
# 方法二
class Solution2:
def high_and_low(self,numbers):
num = [int(s) for s in numbers.split(" ")]
# return "%s %s"%(max(num),min(num))
return "{} {}".format(max(num),min(num))
# 方法三
class Solution3:
def high_and_low(self,numbers):
arr = sorted(numbers.split(),key=int)
return "%s %s"%(arr[-1],arr[0])
#===============================================================================
# Test测试
#===============================================================================
test = {
"1 2 3 4 5": "5 1",
"1 2 -3 4 5": "5 -3",
"1 9 3 0 4 -5": "9 -5",
"489 1564 3041 1376 3520 1689 3392 1061 1608 1026 2724 2579 919 1247 2625 2094 1356 3445 1316 2558 1647 1646":"3520 489",
}
# 切换不同的方法
sol = Solution2()
for k, v in test.items():
res = sol.high_and_low(k)
print('-'*80)
print('Input:', k) # 输入的内容
print('Correct:', v) # 正确的结果
print('Output:', res) # 输出的结果
if res == v: print('Passed^_^')
else: print('Wrong!!')