First Adjacent Different Type
Python
Medium
4 views
Problem Description
Read n tokens, find the first adjacent pair (i,i+1) which have different inferred type using rules: BOOL, INT, FLOAT, WORD. Output i (1-based) or -1 if all adjacent types are same.
Input Format
First line n. Second line n tokens.
Output Format
One integer index or -1.
Sample Test Case
Input:
6
10 20 3.0 4.5 6.0 7.0
Official Solution
import sys,re
p=sys.stdin.read().strip().split()
if not p: sys.exit(0)
n=int(p[0])
a=p[1:1+n]
def typ(s):
if s=='True' or s=='False':
return 0
if re.fullmatch(r'[+-]?\\d+',s):
return 1
if re.fullmatch(r'[+-]?(?:\\d+\\.\\d*|\\d*\\.\\d+)',s):
return 2
return 3
ans=-1
prev=typ(a[0])
for i in range(1,n):
cur=typ(a[i])
if cur!=prev:
ans=i
break
prev=cur
sys.stdout.write(str(ans if ans==-1 else ans))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!