Find First Index
Python
Easy
2 views
Problem Description
Read n integers and a target x. Output the first index (0-based) where x appears, else -1.
Input Format
Line1 n. Line2 n integers. Line3 x.
Output Format
One integer index.
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if len(lines)<3: sys.exit(0)
n=int(lines[0].strip())
a=list(map(int,lines[1].split()[:n]))
x=int(lines[2].strip())
ans=-1
for i,v in enumerate(a):
if v==x:
ans=i
break
sys.stdout.write(str(ans))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!