简体   繁体   English

如何在不使用 // 的情况下划分 python?

[英]How to divide in python without using //?

This is the code that I already have and it just need to be tweaked.这是我已经拥有的代码,只需要对其进行调整。 I think the formulas may not all be correct.我认为这些公式可能并不都是正确的。 I only need the quotient.我只需要商。

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) should give me 2 divide(-5,-2)应该给我 2

divide(13,3) should give me 4 divide(13,3)应该给我 4

Returning x is wrong, it has no point, i rewrote that function:返回x是错误的,没有意义,我重写了function:

def divide_helper(x,y):
    if y>x:
        return 0
    return divide_helper(x-y, y) +1

When y is less than x, we should return 0, you can treat this as base case.当 y 小于 x 时,我们应该返回 0,您可以将其视为基本情况。 In other cases, we add just 1 to the value coming from the lower recursion在其他情况下,我们只将来自较低递归的值加 1

Code Review:代码审查:

Scenario #1: x negative, y positive;场景#1:x 负,y 正; abs(x) > abs(y)绝对值(x) > 绝对值(y)

Let's review your code with example x = -8 and y = 5. The code will go into this section:让我们以 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)

This will send the call back to divide function with values 8,5.这将发送回调用以将 function 与值 8,5 相除。 It will skip all if statements and go into the below given else statement with values 8, 5.它将跳过所有 if 语句和 go 到下面给出的值为 8、5 的 else 语句中。

def divide(x, y):
    else:
       return divide_helper(x, y, 0)

Let's assume divide_helper function gives you the correct result, you will receive a positive (+) result 1 because 8 can be divided by 5 once (1)让我们假设 divide_helper function 给您正确的结果,您将收到正 (+) 结果 1 因为 8 可以除以 5 一次 (1)

Taking this result back to the called function, you will get:将此结果返回给调用的 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]

You are now going to return -2 instead of -1 which is incorrect.您现在将返回 -2 而不是 -1,这是不正确的。

Fix#1:修复#1:

if x < 0 or y < 0:
    return -divide(-x, y) #remove (0 - 1)

However, it does not address the sign issue但是,它并没有解决标志问题

Scenario #2: x positive, y negative;场景#2:x 正,y 负; abs(x) > abs(y)绝对值(x) > 绝对值(y)

Let's look at the code again with another example of x = 8 and y = -5.让我们用另一个 x = 8 和 y = -5 的例子再看一下代码。 This time the code will go into section:这次代码将 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)

This goes through the same process and ends up with the same fix we discussed above.这经历了相同的过程,最终得到了我们上面讨论的相同修复。 Again, this does not address the sign issue同样,这并没有解决标志问题

Now let's review the second function that calculates the quotient:现在让我们回顾一下计算商的第二个 function:

def divide_helper(x, y, temp):
    if y >= x:
        return x
    else:
        return divide_helper(x - y, y, temp + 1)

Let's consider the example of x = 5, y = 8. The code satisfies the if statement and returns x which is 5. That's incorrect.让我们考虑 x = 5, y = 8 的示例。代码满足 if 语句并返回 x 为 5。这是不正确的。 It should return 0 as 5 cannot be divided by 8. Therefore this function is incorrect for values where x < y.它应该返回 0,因为 5 不能除以 8。因此,对于 x < y 的值,这个 function 是不正确的。

Similarly, if x = 5 and y = 5, it will return 5. That is also incorrect.同样,如果 x = 5 且 y = 5,它将返回 5。这也是不正确的。 It should have returned 1 as x divides by y only once (1).它应该返回 1,因为 x 除以 y 仅一次 (1)。

Now let's look at x = 8, y = 5. In this case, it goes into the else statement calling divide_helper function with x = 3, y = 5. (x = xy = 8-5 = 3).现在让我们看看 x = 8, y = 5。在这种情况下,它进入 else 语句,调用 divide_helper function,x = 3, y = 5。(x = xy = 8-5 = 3)。 This time it returns 3. This is again incorrect.这次它返回 3。这又是不正确的。

Looking at your code, I think you wanted to return temp instead of x.查看您的代码,我认为您想返回 temp 而不是 x。

Fix#2:修复#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)

These two fixes should address your divide problem.这两个修复程序应该可以解决您的分歧问题。 However, this does not address the sign issue.但是,这并不能解决符号问题。 If x is + and y is - or if x is - and y is +, it will still return + instead of -.如果 x 是 + 并且 y 是 - 或者如果 x 是 - 并且 y 是 +,它仍然会返回 + 而不是 -。 The updated and fixed code is as shown below:更新和修复的代码如下所示:

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)

While the above code does solve the quotient problem (excluding the sign issue), the code is calling the function too many times.虽然上面的代码确实解决了商问题(不包括符号问题),但代码调用 function 的次数太多了。 In some cases, the call could have been avoided.在某些情况下,本可以避免调用。 Below is an alternate approach to solve this.以下是解决此问题的另一种方法。 See if it helps.看看有没有帮助。

Alternate Approach:替代方法:

In the below approach, the code does not call the function too many times.在下面的方法中,代码不会多次调用 function。 It also addresses the sign problem.它还解决了符号问题。

Here's the full implementation of the code.这是代码的完整实现。 There are a few scenarios to address.有几个场景需要解决。 You don't need 2 functions to calculate the results.您不需要 2 个函数来计算结果。 Instead you can use a loop to iterate and increment the counter.相反,您可以使用循环来迭代和递增计数器。 Also, it is important to keep tab of the sign for each of the input parameters.此外,重要的是要注意每个输入参数的符号。 This will decide the resultant (quotient) sign.这将决定结果(商)符号。 As you know (+ and +) = +;如你所知 (+ 和 +) = +; (+ and -) = -; (+ 和 -) = -; (- and +) = -; (- 和 +) = -; and (- and -) = +.和 (- 和 -) = +。

We also know that if numerator is 0 or numerator is less than denominator, the answer will be 0. If the denominator is 0, I am forcing a result of 0 (instead of infinity).我们还知道,如果分子为 0 或分子小于分母,则答案将为 0。如果分母为 0,我将强制结果为 0(而不是无穷大)。 For all other cases, we need to compute the value by iterating through the loop.对于所有其他情况,我们需要通过循环迭代来计算值。

The full code with comments for each line is as follows:每行带有注释的完整代码如下:

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

The output of this will be: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM