0
def main():

    n = int(input().strip())

    a = list(map(int, input().rstrip().split()))

    b = list(map(int, input().rstrip().split()))
    a.sort()
    b.sort()
    total=0
    i=0
    j=0
    while i!=len(a):
        while j!=len(b):
            # print('a[i]', a[i],'b[j]',b[j])  
            if a[i]<b[j]:
            # print('in a[i]<b[j]')
                i+=1
                break
            if a[i]==b[j]:
                total+=1
                i+=1
                j+=1
            # print('total is ',total)
                break
            else:
            # print('in else')
                j+=1
    if total==n:
        print(n)
    else:
        print(total+1)

main()

Input:

4
1 2 3 4
1 2 3 3

The program keeps asking for input even after i press the "enter" button on my keyboard. while for other types of input it works perfectly fine

18
  • did you press enter key after entering arrays a and b? Commented Sep 1, 2021 at 6:48
  • yes, i did @AlbinPaul Commented Sep 1, 2021 at 6:49
  • Put your input in question also. And then you can also read it from file like python program.py < input.txt Commented Sep 1, 2021 at 6:50
  • 2
    It is not asking for more input. It is stuck in an infinite loop. You should think carefully about your algorithm, and perhaps use a debugger to study the behaviour of the code. Commented Sep 1, 2021 at 6:56
  • 1
    It seems to be going into an infinite loop, not requesting additional input. Probably add a few print statements to see what's going on when you run it. Commented Sep 1, 2021 at 6:56

2 Answers 2

1

Your program is not taking any more inputs, it just runs in an infinite loop in your while statement because of the last digits of a and b namely 4 and 3.

For the first 3 digits 1 2 3, the value of i and j are both 3.

Then while comparing the last digits 4 and 3, it isn't True for this

if a[i]<b[j]:

Nor this

if a[i]==b[j]:

Thus it executed else which only increments j making i=3 and j=4.

Now you're stuck because it will never go inside the inner while loop (since j is already 4) which means it will never increment i again thus will never reach the length of 4.

    while i!=len(a):
        while j!=len(b):

Want proof? switch your inputs for a and b

1
1 2 3 3
1 2 3 4
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help, I understand where I'm going wrong
1
    def getInput(n): #another method.
    a = list()
    for x in range(n):
        a.append(input().rstrip().split())
    return a

def main():

    n = int(input().strip())
    a = getInput(n)
    b = getInput(n)
    a.sort()
    b.sort()
    total=0
    i=0
    j=0
    while i!=len(a):
        while j!=len(b):
            # print('a[i]', a[i],'b[j]',b[j])  
            if a[i]<b[j]:
            # print('in a[i]<b[j]')
                i+=1
                break
            if a[i]==b[j]:
                total+=1
                i+=1
                j+=1
            # print('total is ',total)
                break
            else:
            # print('in else')
                j+=1
    if total==n:
        print(n)
    else:
        print(total+1)

main()

The first problem is that your question is quite imprecise. You've said it stops taking input but have also expressed the idea that it runs infinitely. These cannot both be true given what we have here.

The problem with getting input was that you called map() with int and list().rstrip().split(), as 1st and 2nd arguments when the Python library states that the function requires the method you want to call as 1st arg and the 2nd must be an iterable, or a collection as per:

https://docs.python.org/3/library/functions.html#map

So if you wanted to use map() you also needed to have a list full of items because map() uses the provided function on each value in the given list. in other words: you needed data to run map() on prior to calling it.

I am assuming that you're implementing an algorithm and the print()'s executed properly when I ran it with them uncommented. My solution above should at least allow you to get back to developing your algorithm.

I simply deferred the call to a method defined called getInput() which takes the number of values to take (n) and returns the list of them retrieved from the input.

1 Comment

Thank you for your explanation, I got a clearer understanding of the part where I was going wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.