Series33. 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 last 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):
position = -1
for j in range(0,N):
x = random.randrange(0,4)
print(x,end="; ")
if x == 2:
position = j
print()
print("Position of the last 2:",position)
print()