选择最大奇数Python

2024-10-16 23:34:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Python编写一个简单的程序,计算出x,y,z值中最大的奇数。如何给用户一个选择x,y,z值的选项?

所以程序会问x,y,z是什么,然后说“x,y,z是最大的奇数”或者数字都是偶数。

到目前为止,我所掌握的情况如下。这至少是个不错的开始吗?

  # 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'

有了thkang post,我现在有了:

  # 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

Tags: andthe程序ifisthiselseeven
3条回答

最好先过滤数字,然后再分类。

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)。

number = int(raw_input('enter a number: '))

仅使用if语句

你必须嵌套if语句。比如:

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
...

接近

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

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

代码

def find_largest_odd(*args):
    return max(arg for arg in args if arg & 1)

或:

def find_largest_odd(*args):
    return max(filter(lambda x: x & 1, args))

测试

>>> 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

以及:

>>> 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

>>> 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

参考文献

try:
    largest = max(val for val in (x,y,z) if val % 2)
    print(largest)
except ValueError:
    print('Even')

注意sorted是一个O(n log n)操作,而maxO(n)。对于如此短的序列,速度差可能无关紧要,但使用最佳工具进行作业是一种良好的做法。

相关问题 更多 >