Run Length of Numbers
Python
Medium
2 views
Problem Description
Read n integers. Compress as pairs value count for consecutive equal values. Output pairs in new lines.
Input Format
First line n. Second line n integers.
Output Format
Lines: value count.
Official Solution
import sys
p=sys.stdin.read().strip().split()
if not p: sys.exit(0)
n=int(p[0])
a=list(map(int,p[1:1+n]))
if n==0:
sys.stdout.write('')
else:
out=[]
cur=a[0]
cnt=1
for v in a[1:]:
if v==cur:
cnt+=1
else:
out.append(f'{cur} {cnt}')
cur=v
cnt=1
out.append(f'{cur} {cnt}')
sys.stdout.write('\
'.join(out))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!