简体   繁体   English

获取一个打印语句两次打印一个值的不同输出,python

[英]Getting a print statement printed twice with different outputs for one value, python

I have a code I'm trying to run.我有一个我正在尝试运行的代码。 But after running the code I get two print value for one print statement and a single test case.但是在运行代码后,我得到了一个打印语句和一个测试用例的两个打印值。

The output I am getting:我得到的输出:

PS D:\VS Code\Python> & C:/Users/zyx/AppData/Local/Programs/Python/Python310/python.exe "d:/VS Code/Python/dump/dump 3.py"
lo: 0 , hi: 7 , mid: 3 , mid_number: 7
lo: 4 , hi: 7 , mid: 5 , mid_number: 1
True
PS D:\VS Code\Python> 

The only output I should be getting is我应该得到的唯一输出是

PS D:\VS Code\Python> & C:/Users/zyx/AppData/Local/Programs/Python/Python310/python.exe "d:/VS Code/Python/dump/dump 3.py"
lo: 0 , hi: 7 , mid: 3 , mid_number: 7
True
PS D:\VS Code\Python> 

How can I get remove this extra print statement error?我怎样才能删除这个额外的打印语句错误? Here's the code:这是代码:

test1 = {
    'input': {
        'nums': [4, 5, 6, 7, 8, 1, 2, 3]
    },
    'output': 5
}

def count_rotations_binary(nums):
    lo = 0
    hi = len(nums)-1
    
    while lo<=hi:
        mid = (lo+hi)//2
        mid_number = nums[mid]
        
        print("lo:", lo, ", hi:", hi, ", mid:", mid, ", mid_number:", mid_number)

        if mid > 0 and nums[mid]<nums[mid-1]:
            return mid
        
        elif nums[mid] > nums[hi]:
            lo = mid + 1  
        
        else:
            hi = mid - 1
    
    return 0

print(count_rotations_binary(**test1['input']) == test1['output'])

Its a while loop, so it can run multiple times.它是一个while循环,所以它可以运行多次。 Therefore your code must be making tthe while loop multiple times therefor printing multiple times.因此,您的代码必须多次进行 while 循环,以便多次打印。

This isn't an error.这不是错误。 For the input you have provided, and your binary search logic to find the starting position of the number sequence, 2 loops (and therefore 2 print statements) is expected.对于您提供的输入,以及用于查找数字序列起始位置的二进制搜索逻辑,需要 2 个循环(因此需要 2 个打印语句)。

During the first pass through the loop we have the following values:在第一次通过循环期间,我们有以下值:
lo: 0 , hi: 7 , mid: 3 , mid_number: 7

Then we get to the binary search logic:然后我们进入二分查找逻辑:

        if mid > 0 and nums[mid]<nums[mid-1]:
            return mid
        
        elif nums[mid] > nums[hi]:
            lo = mid + 1  
        
        else:
            hi = mid - 1

The if statement is False as 7 < 6 is False. if语句为 False,因为7 < 6为 False。
Then the elif statement nums[mid] > nums[hi] evaluates to True, as 7 > 3 .然后 elif 语句nums[mid] > nums[hi]评估为 True,如7 > 3 So lo is set to mid+1 (4), and the loop starts again.所以 lo 设置为mid+1 (4),循环再次开始。

On the second loop we have在第二个循环中,我们有
lo: 4 , hi: 7 , mid: 5 , mid_number: 1
The if statement is evaluated as True: if 语句被评估为 True:
mid > 0 and nums[mid]<nums[mid-1]
evaluates as 5 > 0 and 1 < 8评估为5 > 0 and 1 < 8
So return mid is called and the function returns 5.所以return mid被调用,函数返回 5。

Use If instead of while that's it.使用If而不是while that's it。 use this.用这个。

if lo<=hi:

instead of this.而不是这个。

while lo<=hi:

Reason: It can check conditions a single time and give an output no matter how long your data is present here:原因:它可以一次检查条件并给出输出,无论您的数据在这里存在多长时间:

'nums': [4, 5, 6, 7, 8, 1, 2, 3] 'nums': [4, 5, 6, 7, 8, 1, 2, 3]

Fact: if you cannot change your program and add some extra data here:事实:如果您无法更改程序并在此处添加一些额外数据:

'nums': [4, 5, 6, 7, 8, 1, 2, 3,5,6,8,4] 'nums': [4, 5, 6, 7, 8, 1, 2, 3,5,6,8,4]

you will get some extra lines because of your algorithm until while loop does not matches your given condition.由于您的算法,您将获得一些额外的行,直到 while 循环与您的给定条件不匹配。

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

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