Series32. Integers K, N and K sets of integers with N elements in each set are given. For each set, output the sequence number of its first element equal to 2, or the number 0 if there are no 2-s in this set.

Solution in Python 3:

import random

K = random.randrange(1,7)
N = random.randrange(1,7)

print("K = ",K)
print("N = ",N)

for i in range(0,K):
flag = True
position = -1
for j in range(0,N):
x = random.randrange(0,5)
print(x,end="; ")
if flag and x == 2:
flag = False
position = j
print()
print("Position of first 2:",position)
print()