FINDMELI Problem with Python code

Please help me understand why the following python code is being judged as “Wrong answer”

n,k=input().split()
n=int(n)
k=int(n)
inp = list(int(num) for num in input().strip().split())[:n]
find=-1
for x in inp:
    if x==k:
        find=1
print(find)

Following C++ code was successfully submitted

#include <iostream>
#include<vector>
using namespace std;

int main() {
	int n,k,temp;
	cin>>n>>k;
	vector<int> input;
	for(int i=0;i<n;i++){
	    cin>>temp;
	    input.push_back(temp);
	}
	int find=-1;
	for(const int &i:input)
	    if(i==k)
	        find=1;
	cout<<find;
	return 0;
}

Copy-paste error: you have k = int(n)

2 Likes

Thankyou very much. I feel silly for not spotting that :sweat_smile: .

Also to avoid this interim string and conversion issue you could write

n, k = map(int, input().split())

or if you don’t like map:

n, k = (int(a) for a in input().split())

Another Python usage observation: your search can just use the in boolean without looping:

find = -1
if k in inp:
    find = 1

Which could use an inline conditional:

find = 1 if k in inp else -1

at which point you maybe think about just printing it without storing

print (1 if k in inp else -1)
2 Likes

Thank you :grinning_face_with_smiling_eyes:. It is really helpful. Can you please provide some sources where I can learn the in-built data structures of python like map? I have found a lot of material on C++ STL but not much on python. Anyway thanks again for all the help and tips.

Python documentation is public and while it isn’t an “introduction to programming” or anything, it is a very useful reference.

For Python 3.6:

Thankyou very much for this.