简体   繁体   中英

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. Therefore your code must be making tthe while loop multiple times therefor printing multiple times.

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.

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.
Then the elif statement nums[mid] > nums[hi] evaluates to True, as 7 > 3 . So lo is set to mid+1 (4), and the loop starts again.

On the second loop we have
lo: 4 , hi: 7 , mid: 5 , mid_number: 1
The if statement is evaluated as True:
mid > 0 and nums[mid]<nums[mid-1]
evaluates as 5 > 0 and 1 < 8
So return mid is called and the function returns 5.

Use If instead of 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]

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]

you will get some extra lines because of your algorithm until while loop does not matches your given condition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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