给定一个由纯数字组成的字符串,每个数字之间使用空格分隔。我们的任务是,编写一个函数,找出其中最大和最小的两个数字。返回结果的两个数字格式为字符串,并且逆序排列,也使用空格分隔。
示例:
输入:1 2 3 4 5
,输出:5 1
。
题目难度:简单
题目来源:CodeWars: Highest and Lowest
def solution(nums: str) -> str:
# your code here
assert solution("1 2 3 4 5") == "5 1"
assert solution("1 2 -3 4 5") == "5 -3"
assert solution("1 9 3 4 -5") == "9 -5"
assert solution("1 2 3") == "3 1"
assert solution("8 3 -5 42 -1 0 0 -9 4 7 4 -4") == "42 -9"
Kawi
2022 年2 月 11 日 03:08
2
def solution(nums: str) -> str:
# your code here
dict = nums.split(" ")
dict1 = []
for i in range(len(dict)):
dict1.append(int(dict[i]))
return "".join(str(max(dict1)) + " " + str(min(dict1)))
def solution(nums: str) -> str:
lstr = nums.split(' ')
lint = []
for n in lstr:
lint.append(int(n))
lint.sort()
return str(lint[-1]) + " " + str(lint[0])
def solution(nums: str) -> str:
nums_list = nums.split(' ')
nums_list_int = []
for i in nums_list:
nums_list_int.append(int(i))
return ' '.join([str(max(nums_list_int)),str(min(nums_list_int))])
assert solution("1 2 3 4 5") == "5 1"
assert solution("1 2 -3 4 5") == "5 -3"
assert solution("1 9 3 4 -5") == "9 -5"
assert solution("1 2 3") == "3 1"
assert solution("8 3 -5 42 -1 0 0 -9 4 7 4 -4") == "42 -9"
java代码
public String getMaxAndMin(String str){
String strList = str.split("\s+");
if(strList.length<2) {
return null;
}
Arrays.sort(strList);
return strList[strList.length-1] +" " + strList[0];
}
@Test
public void getMaxAndMinTest(){
Assert.assertEquals("9 -1",this.getMaxAndMin("5 4 3 9 -1"));
Assert.assertEquals(null,this.getMaxAndMin("5 "));
Assert.assertEquals("1 -1",this.getMaxAndMin("-1 1"));
}
冒泡排序
def solution(nums: str) -> str:
nums = nums.split(' ')
n = len(nums)
for i in range(n):
for j in range(0, n-i-1):
if int(nums[j]) > int(nums[j+1]):
nums[j], nums[j+1] = nums[j+1], nums[j]
return ' '.join((nums[-1], nums[0]))
def solution(nums: str) -> str:
n_list = [int(i) for i in nums.split(" ")]
return str(max(n_list)) + ' ' + str(min(n_list))
assert solution("1 2 3 4 5") == "5 1"
assert solution("1 2 -3 4 5") == "5 -3"
assert solution("1 9 3 4 -5") == "9 -5"
assert solution("1 2 3") == "3 1"
assert solution("8 3 -5 42 -1 0 0 -9 4 7 4 -4") == "42 -9"
def solution(nums: str) -> str:
tmp = [int(x) for x in numbers.split()]
return " ".join([str(max(tmp)),str(min(tmp))])
def solution(nums: str) -> str:
return " ".join([max(nums.split(" ")), min(nums.split(" "))])
bliss
(bliss)
2022 年2 月 16 日 12:56
10
‘’’
def solution(nums):
nums_list=sorted(map(int,nums.split(’ ‘)),reverse=True)
return str(nums_list[0])+’ ‘+str(nums_list[-1])
‘’’
def solution(nums: str) -> str:
# your code here
nums_list = [int(i) for i in nums.split(" ")]
for i in range(len(nums_list)-1):
for j in range(len(nums_list)-i-1):
if(nums_list[j]<nums_list[j + 1]):
nums_list[j], nums_list[j + 1] = nums_list[j + 1], nums_list[j]
return str(nums_list[0]) + " " + str(nums_list[-1])
assert solution("1 2 -3 4 5") == "5 -3"
assert solution("1 9 3 4 -5") == "9 -5"
assert solution("1 2 3") == "3 1"
assert solution("8 3 -5 42 -1 0 0 -9 4 7 4 -4") == "42 -9"```
To_my
(To my~)
2022 年3 月 2 日 07:43
13
def solution(nums: str) -> str:
return f'{max([int(i) for i in nums.split(" ")])} {min([int(i) for i in nums.split(" ")])}'
assert solution("1 2 3 4 5") == "5 1"
assert solution("1 2 -3 4 5") == "5 -3"
assert solution("1 9 3 4 -5") == "9 -5"
assert solution("1 2 3") == "3 1"
assert solution("8 3 -5 42 -1 0 0 -9 4 7 4 -4") == "42 -9"
def solution(nums: str) -> str:
lis_nums = nums.split(" ")
max = int(lis_nums[0])
min = int(lis_nums[0])
for i in lis_nums:
if int(i) > max:
max = int(i)
if int(i) < min:
min = int(i)
return f"{max} {min}"
assert solution("1 2 3 4 5") == "5 1"
assert solution("1 2 -3 4 5") == "5 -3"
assert solution("1 9 3 4 -5") == "9 -5"
assert solution("1 2 3") == "3 1"
assert solution("8 3 -5 42 -1 0 0 -9 4 7 4 -4") == "42 -9"
def solution(nums):
results = sorted([int(x) for x in nums.split(' ')])
return '%s %s' % (results[-1],results[0])
def solution(nums: str) -> str:
return str(max([int(i) for i in nums.split()]))+' '+str(min([int(i) for i in nums.split()]))
def get_range(nums: str) -> str:
dict = nums.split(" ")
dict1 = []
for i in range(len(dict)):
dict1.append(int(dict[i]))
return str(max(dict1)) + " " + str(min(dict1))
def solution(nums: str) -> str:
# your code here
list1=sorted(nums.split(' '),key=int,reverse=True)
return f"{list1[0]} {list1[-1]}"
def solution(nums: str) -> str:
num_list = [int(n) for n in nums.split(' ')]
return f"{max(num_list)} {min(num_list)}"
def solution(nums: str) -> str:
ret = [int(i) for i in nums.split(" ")]
return ' '.join([str(max(ret)), str(min(ret))])
assert solution("1 2 3 4 5") == "5 1"
assert solution("1 2 -3 4 5") == "5 -3"
assert solution("1 9 3 4 -5") == "9 -5"
assert solution("1 2 3") == "3 1"