简体   繁体   English

如何使用递归而不是在python中使用运算符找到两个整数中的较大者

[英]How to find larger of two integers using recursion and not using operators in python

I want to find the larger of two integers.我想找到两个整数中较大的一个。 But I have to use Recursion and without python operators.但我必须使用递归而不使用 python 运算符。 Code structure is given below.代码结构如下。

def gt(a, b):
  '''Returns True if a is an integer greater than b;
     returns False otherwise'''
  # using only incr, decr, zero, and recursion
  return 0


def incr(a):
  '''Returns the next integer after a'''
  return a + 1

def zero(a):
  '''Returns True if a is zero'''
  return a == 0

def decr(a):
  '''Returns the integer before a'''
  return a - 1
version for positive integers正整数版本

You could decrement both numbers and see whether a or b reaches zero first:您可以减少两个数字,然后查看 a 或 b 是否先达到零:

def gt(a, b):
    '''Returns True if a is an integer greater than b;
       returns False otherwise'''
    # using only incr, decr, zero, and recursion
    if zero(b) and not zero(a):
        return True
    if zero(a):
        return False
    return gt(decr(a), decr(b))

examples:例子:

>>> gt(100, 10)
True

>>> gt(10, 10)
False

>>> gt(1, 10)
False

If you are permitted the simplest of loops (using only zero for the breaking condition):如果允许您使用最简单的循环(仅使用zero作为中断条件):

def gt(a, b):
    # 1. get a to zero by going up and down in parallel
    aup = adown = a
    bup = bdown = b
    while True:
        if zero(aup):
            a, b = aup, bup
            break
        if zero(adown):
            a, b = adown, bdown
            break
        aup, adown = incr(aup), decr(adown)
        bup, bdown = incr(bup), decr(bdown)
    if zero(b):
        return False 
    
    # 2. get b to zero
    bup = bdown = b
    while True:
        if zero(bup):
            return True
        if zero(bdown):
            return False
        bup, bdown = incr(bup), decr(bdown)

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

相关问题 Python:如何使用递归反转整数 - Python: How to reverse integers using recursion 在python中使用递归查找1和最大N位之间的整数 - Find integers between 1 and the biggest N-bits using recursion in python 使用python查找整数中数字总和的递归函数 - Recursion function to find sum of digits in integers using python 比较两个整数而不使用任何比较(递归,python) - Comparing two integers without using any comparison (recursion, python) 如何使用python为方程找到正确的运算符? - How to find the correct operators for an equation, using python? 如何在 Python 中使用递归编织两个列表 - How to weave two lists using recursion in Python 没有While循环的Python递归并在不使用%运算符的情况下找到x / y的余数? - Python Recursion without While loop and find remainder of x/y without using %operators? 在 Python 3 中使用 sys 查找两个排序的整数列表的交集 - Find the intersection of two sorted lists of integers using sys in Python 3 如何仅使用递归(无循环或列表推导)在Python中查找两个列表的点积? - how to find dot product of two lists using only recursion(no loops or list comprehensions) in python? Python使用递归来获取混合字符串并在字符串中添加整数 - Python using recursion to take a mixed string and add up the integers in the string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM