-2

If I don't use 'map' in the below code while taking input for variable 's' & 'd' then it shows a runtime error.I didn't get the reason behind it as I hoped that input could had been taken without 'map'.

t= int(input())
for x in range(t):
    n =int(input())
    
    s=list(map(int,input().split()))
    d=list(map(int,input().split()))
    
    count = 0
    for i in range(n): 
            if s[i]==d[i]:
                count += 1
    
    print(count)

Testcase- Input

2--- #(t)

4----#(n)
1 2 3 4 ---#(s)
2 1 3 4-----#(d)

output -

2

#output is 2 because only 2 digits (3&4) match in line s&d in values and index position

luk2302
  • 55,258
  • 23
  • 97
  • 137
Pranav167
  • 5
  • 4

2 Answers2

0

I'd say the significance of map is that it is an iterator that returns a result without using a loop.

Without a result you get a runtime error because python doesnt know what to work with. You can leave it out for alternatives, but those are (mostly) less efficient and require more runtime.

Eric Dirla
  • 81
  • 7
0

map applies a function to each element of an interable (list, tuple etc). In your case it applies int (converts elements into integers) to each character of the list returned by split (e.g. ['1', '2', '3', '4']).

But your code works just fine without converting the elements into integers. In this case it is comparing characters as follows:

t = int(input())
for x in range(t):
    n = int(input())

    s = list(input().split())
    d = list(input().split())

    count = 0
    for i in range(n):
        if s[i] == d[i]:
            count += 1

    print(count)

I tested it with the same input you provided.

bwdm
  • 793
  • 7
  • 17