简体   繁体   English

在不使用条件语句或python 3.5中循环的情况下从三个数字中获取两个最大数字

[英]Getting two largest numbers from three numbers without using conditional statement or loop in python 3.5

Complete the function1() function which is passed three whole numbers.The function returns the total of the two bigger numbers. 完成传递三个整数的function1()函数,该函数返回两个较大数字的总和。

print()
print(1, function1(1, 2, 3))
print(2, function1(11, 12, 3))
print(3, function1(6, 2, 5))

output should be 
1 5
2 23
3 11

This was the question. 这就是问题所在。 I try googling but all I have found was using conditional statement or loop. 我尝试使用谷歌搜索,但发现的全部是使用条件语句或循环。 I was wondering if there is other way to do this without using both? 我想知道是否还有其他方法可以同时使用两者? Reason being is that this question was on before the introduction of conditional statement and loop. 原因是这个问题是在引入条件语句和循环之前进行的。 I can advance my self-studying and do it however there must be a reason why it poped up before we go onto the next chapter. 我可以提高自己的学习水平,但是在我们进入下一章之前,一定有一个原因突然出现。 What do you reckon?.. (During self-studying for CS101) ` 您怎么看?..(在学习CS101的过程中)`

you could sort the tuple of the numbers and add the first 2 values: 您可以对数字的元组进行排序并添加前两个值:

def function1(a, b, c):
    s = sorted((a, b, c), reverse=True)
    return s[0] + s[1]

sorted starts with the smalles item normally that's why you need to set reverse=True . sorted通常从smalles项开始,这就是为什么需要设置reverse=True

or you could sum the three and subtract the minimal value: 或者您可以将这三个数相加并减去最小值:

def function1(a, b, c):
    return sum((a, b, c)) - min(a, b, c)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中计算三个中最大的两个数的平方和 - How to compute sum of squares of two largest numbers out of three in python 如何在python中找到由两个三位数组成的最大回文? - How to find the largest palindrome made of two three digit numbers in python? Python,使用for循环打印两列数字 - Python, printing two columns of numbers using for loop python for循环与if语句来划分数字 - python for loop with if statement to divide numbers 从python中3个数字的乘积中找到最大的回文 - find the largest palindrome from the product of 3 numbers in python 在不使用Python中的条件语句的情况下舍入为整数-逻辑 - Round to whole numbers without using conditional statements in Python - Logic 在循环中查找最大和第二大数字 - Finding largest and second largest numbers in a loop 从用户通过条件语句输入的三个变量中打印最大和第二大的数字 - Print largest & second largest number from three variables entered by the user by conditional statement Python Pandas:从一组数字中获取两个最大值,但确保它们相距 x 个值 - Python Pandas: Get the two largest values from a set of numbers but ensure they are x amount of values apart 在Python中找到两个三位数乘积的最大回文 - Finding the largest palindrome of the product of two 3-digit numbers in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM