首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >选择最大的奇数python

选择最大的奇数python
EN

Stack Overflow用户
提问于 2013-04-01 02:18:25
回答 28查看 20.9K关注 0票数 6

我正在尝试用Python编写一个简单的程序来计算x,y,z值中最大的奇数。如何让用户选择x,y和z的值呢?

因此,程序将询问x,y和z是什么,然后说"x,y,z是最大的奇数“,或者这些数字都是偶数。

到目前为止,我拥有的内容如下所示。这至少是一个良好的开始吗?

代码语言:javascript
复制
  # This program exmamines variables x, y, and z 
  # and prints the largest odd number among them

  if x%2 !== 0 and x > y and y > z:
      print 'x is the largest odd among x, y, and z'
  elif y%2 !== 0 and y > z and z > x:
     print 'y is the largest odd among x, y, and z'
  elif z%2 !== 0 and z > y and y > x:
     print 'z is the largest odd among x, y, and z'
  elif x%2 == 0 or y%2 == 0 or z%2 == 0:
     print 'even'

有了这个帖子,我现在有了:

代码语言:javascript
复制
  # This program exmamines variables x, y, and z 
  # and prints the largest odd number among them

  if x%2 !== 0:
    if y%2 !== 0:
      if z%2 !== 0:
        if x > y and x > z: #x is the biggest odd
        elif y > z and y > x: #y is the biggest odd
        elif z > x and z > y: #z is the biggest odd

      else: #z is even
        if x > y: #x is the biggest odd
        else: #y is the biggest odd

    else: #y is even
      if z%2 != 0: #z is odd
        if x > z: #x is the biggest odd
        else: #z is the biggest odd
      else: #y,z are even and x is the biggest odd

  else: #x is even
    if y%2 != 0 and z%2 != 0; #y,z is odd
      if y > z: #y is the biggest odd
      else: #z is the biggest odd
    else: #x and y is even
      if z%2 != 0: #z is the biggest odd
EN

回答 28

Stack Overflow用户

回答已采纳

发布于 2013-04-01 02:24:09

最好先对数字进行过滤,然后再对它们进行排序。

代码语言:javascript
复制
numbers = [x, y, z]

sorted_odd_nums = sorted((x for x in enumerate(numbers) if x[1]%2), 
                         key = lambda x:x[1], 
                         reverse=True)

if not sorted_odd_nums:
   # all numbers were even and filtered out.
elif sorted_odd_nums[0][0] == 0:
   # x is the biggest odd number
elif sorted_odd_nums[0][0] == 1:
   # y is the biggest odd number
elif sorted_odd_nums[0][0] == 2:
   # z is the biggest odd number

它的用途:

enumerate(numbers)返回一系列(index, item)对。由于原始列表是[x, y, z],所以即使在过滤和排序之后,我们仍然可以保持xyz的轨迹。

如果给定元组中的第二项不是偶数,则(x for x in enumerate(numbers) if x[1]%2)筛选上面的枚举。

sort( ... , key=lambda x:x[1], reverse=True)使用第二个索引项(即原始编号)的值按降序对筛选出的项进行排序。

用户输入

要从用户读取数据,最简单的方法是使用raw_input(py2) / input(py3k)。

代码语言:javascript
复制
number = int(raw_input('enter a number: '))

仅使用if语句

你必须嵌套if语句。像这样:

代码语言:javascript
复制
if x%2: # x is odd
  if y%2: # y is odd
    if z%2: #z is odd
      if x>y and x>z: #x is the biggest odd number
      elif y>z and y>x: #y is the biggest odd number
      elif z>x and z>y: #z is the biggest odd number

    else: #z is even
      if x>y: #x is the biggest odd number
      else: #y is the biggest odd number
  else: #y is even
    if z%2: #z is odd
...
票数 2
EN

Stack Overflow用户

发布于 2013-04-01 03:19:25

方法

避免使用if-stmts查找最大值。在max中使用python内置。使用生成器或filter仅查找奇数。

使用这样的内置更安全/更可靠,因为它更简单地组合它们,代码经过良好的测试,并且代码主要在C中执行(而不是多字节码指令)。

代码

代码语言:javascript
复制
def find_largest_odd(*args):
    return max(arg for arg in args if arg & 1)

或者:

代码语言:javascript
复制
def find_largest_odd(*args):
    return max(filter(lambda x: x & 1, args))

测试

代码语言:javascript
复制
>>> def find_largest_odd(*args):
...     return max(arg for arg in args if arg & 1)
... 
>>> print find_largest_odd(1, 3, 5, 7)
7
>>> print find_largest_odd(1, 2, 4, 6)
1

和:

代码语言:javascript
复制
>>> def find_largest_odd(*args):
...     return max(filter(lambda x: x & 1, args))
>>> print find_largest_odd(1, 3, 5, 7)
7
>>> print find_largest_odd(1, 2, 4, 6)
1

如果您传递一个空序列或仅提供偶数,您将得到一个ValueError

代码语言:javascript
复制
>>> find_largest_odd(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in find_largest_odd
ValueError: max() arg is an empty sequence

参考文献

  • filter
  • max
  • generators
票数 10
EN

Stack Overflow用户

发布于 2013-10-26 01:03:08

如果有人正在寻找使用条件语句的简单解决方案

代码语言:javascript
复制
x,y,z = 4,1,6
largest = None
if x%2:
 largest = x
if y%2:
 if y > largest:
  largest = y
if z%2:
 if z > largest:
  largest = z
if largest:
 print "largest number is" largest
else
 print "there are no odd numbers"
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15732805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档