Series37. An integer K and K sets of nonzero integers are given. Each set contains at least two elements, the sign of its completion is the number 0. Find the number of sets whose elements increase or decrease.

Solution in Python 3:

import random

K = random.randrange(1,9)

print("K = ",K)

N = 10
count_inc = 0
count_dec = 0
for i in range(0,K):
x_prev = random.randrange(1,N)
print(x_prev,end="; ")
x = random.randrange(1,N)
print(x,end="; ")

if x > x_prev:
flag_inc = True
else:
flag_inc = False
if x < x_prev:
flag_dec = True
else:
flag_dec = False

while True:
x_prev = x
x = random.randrange(0,N)
print(x,end="; ")
if x == 0:
break
if not (flag_inc and x > x_prev):
flag_inc = False
if not (flag_dec and x < x_prev):
flag_dec = False
print()
if flag_inc:
count_inc += 1
if flag_dec:
count_dec += 1
print("Number of increasing series: ",count_inc)
print("Number of decreasing series: ",count_dec)
print("Number of increasing or decreasing series: ",count_inc + count_dec)