[英]How to divide in python without using //?
这是我已经拥有的代码,只需要对其进行调整。 我认为这些公式可能并不都是正确的。 我只需要商。
def divide(x, y):
if x == 0 or y == 0:
return 0
if x < 0 and y < 0:
return divide(0 - x, 0 - y)
if x < 0 or y < 0:
return 0 - 1 - divide(0 - x, y)
else:
return divide_helper(x, y, 0)
def divide_helper(x, y, temp):
if y >= x:
return x
else:
return divide_helper(x - y, y, temp + 1)
divide(-5,-2)
应该给我 2
divide(13,3)
应该给我 4
返回x
是错误的,没有意义,我重写了function:
def divide_helper(x,y):
if y>x:
return 0
return divide_helper(x-y, y) +1
当 y 小于 x 时,我们应该返回 0,您可以将其视为基本情况。 在其他情况下,我们只将来自较低递归的值加 1
场景#1:x 负,y 正; 绝对值(x) > 绝对值(y)
让我们以 x = -8 和 y = 5 为例来查看您的代码。代码将 go 放入本节:
def divide(x, y): # x = -8 , y = 5
if x < 0 or y < 0: #satisfies x < 0 as -8 < 0
return 0 - 1 - divide(0 - x, y) # -1 -divide(8,5)
这将发送回调用以将 function 与值 8,5 相除。 它将跳过所有 if 语句和 go 到下面给出的值为 8、5 的 else 语句中。
def divide(x, y):
else:
return divide_helper(x, y, 0)
让我们假设 divide_helper function 给您正确的结果,您将收到正 (+) 结果 1 因为 8 可以除以 5 一次 (1)
将此结果返回给调用的 function,您将得到:
def divide(x, y): # x = -8 , y = 5
if x < 0 or y < 0:
return 0 - 1 - divide(0 - x, y) # return -1 -1 as [result of divide(8,5) = 1]
您现在将返回 -2 而不是 -1,这是不正确的。
修复#1:
if x < 0 or y < 0:
return -divide(-x, y) #remove (0 - 1)
但是,它并没有解决标志问题
场景#2:x 正,y 负; 绝对值(x) > 绝对值(y)
让我们用另一个 x = 8 和 y = -5 的例子再看一下代码。 这次代码将 go 变成了一段:
def divide(x, y): # x = 8 , y = -5
if x < 0 or y < 0: #satisfies y < 0 as -5 < 0
return 0 - 1 - divide(0 - x, y) # -1 -divide(-8,-5)
这经历了相同的过程,最终得到了我们上面讨论的相同修复。 同样,这并没有解决标志问题
现在让我们回顾一下计算商的第二个 function:
def divide_helper(x, y, temp):
if y >= x:
return x
else:
return divide_helper(x - y, y, temp + 1)
让我们考虑 x = 5, y = 8 的示例。代码满足 if 语句并返回 x 为 5。这是不正确的。 它应该返回 0,因为 5 不能除以 8。因此,对于 x < y 的值,这个 function 是不正确的。
同样,如果 x = 5 且 y = 5,它将返回 5。这也是不正确的。 它应该返回 1,因为 x 除以 y 仅一次 (1)。
现在让我们看看 x = 8, y = 5。在这种情况下,它进入 else 语句,调用 divide_helper function,x = 3, y = 5。(x = xy = 8-5 = 3)。 这次它返回 3。这又是不正确的。
查看您的代码,我认为您想返回 temp 而不是 x。
修复#2:
def divide_helper(x, y, temp):
if y > x: #changed to only check if y > x
#When y=x, you need to increment temp by 1
return temp
elif x == y: #check explicitly for x==y and increment temp by 1
return temp+1
else:
return divide_helper(x - y, y, temp + 1)
这两个修复程序应该可以解决您的分歧问题。 但是,这并不能解决符号问题。 如果 x 是 + 并且 y 是 - 或者如果 x 是 - 并且 y 是 +,它仍然会返回 + 而不是 -。 更新和修复的代码如下所示:
def divide(x, y):
if x == 0 or y == 0:
return 0
if x < 0 and y < 0:
return divide(-x, -y)
if x < 0 or y < 0:
return divide(-x, y)
else:
return divide_helper(x, y, 0)
def divide_helper(x, y, temp):
if y > x:
return temp
elif x == y:
return temp+1
else:
return divide_helper(x - y, y, temp + 1)
虽然上面的代码确实解决了商问题(不包括符号问题),但代码调用 function 的次数太多了。 在某些情况下,本可以避免调用。 以下是解决此问题的另一种方法。 看看有没有帮助。
在下面的方法中,代码不会多次调用 function。 它还解决了符号问题。
这是代码的完整实现。 有几个场景需要解决。 您不需要 2 个函数来计算结果。 相反,您可以使用循环来迭代和递增计数器。 此外,重要的是要注意每个输入参数的符号。 这将决定结果(商)符号。 如你所知 (+ 和 +) = +; (+ 和 -) = -; (- 和 +) = -; 和 (- 和 -) = +。
我们还知道,如果分子为 0 或分子小于分母,则答案将为 0。如果分母为 0,我将强制结果为 0(而不是无穷大)。 对于所有其他情况,我们需要通过循环迭代来计算值。
每行带有注释的完整代码如下:
def div_check(x, y):
#if either x or y is 0, return 0
if 0 in (x,y): return 0
#set the sign to positive (+)
s = 1
#if x & y are both negative numbers, result sign will be positive (+)
if (x < 0) and (y < 0): s = 1
#If x is negative and y is positive or the other way around, result sign will be negative (-)
if ((x < 0) and (y > 0)) or ((y < 0) and (x > 0)): s = -1
#convert x and y to absolute value so we can compute division
x,y = abs(x), abs(y)
#if x is less than y, then result is 0
if x < y : return 0
#initialize result r to zero
r = 0
#iterate through the loop each time x is greater than or equal to y while incrementing counter r by 1
while x >= y:
x-=y
r+=1
#multiply the sign s to result r to give the final result
return s*r
print ('(2,5) ',div_check(2,5))
print ('(-2,-5) ',div_check(-2,-5))
print ('(-2,5) ',div_check(-2,5))
print ('(-5,2) ',div_check(-5,2))
print ('(5,-2) ',div_check(5,-2))
print ('(-5,-2) ',div_check(-5,-2))
print ('(0,5) ',div_check(0,5))
print ('(2,0) ',div_check(2,0))
print ('(13,3) ',div_check(13,3))
print ('(-25,5) ',div_check(-25,5))
print ('(-39,-13)',div_check(-39,-13))
print ('(24,-7) ',div_check(24,-7))
print ('(3,14) ',div_check(3,14))
print ('(14,3) ',div_check(14,3))
#This also works for floats. The quotient will be integer
print ('(14.5,3.5)',div_check(14.5,3.5))
print ('(25.6,4.2)',div_check(25.6,-4.2))
output 将是:
(2,5) 0
(-2,-5) 0
(-2,5) 0
(-5,2) -2
(5,-2) -2
(-5,-2) 2
(0,5) 0
(2,0) 0
(13,3) 4
(-25,5) -5
(-39,-13) 3
(24,-7) -3
(3,14) 0
(14,3) 4
(14.5,3.5) 4
(25.6,4.2) -6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.