Disorder Test X57031


Statement
 

pdf   zip   main.py

In a strictly increasing ordered sequence of integers x0,,xrx_0,\ldots,x_r (such as 5, 8, 12, 20 for example) each element is strictly larger than the previous one: xi>xi1x_i > x_{i-1} for all i>0i > 0, that is, xi1xi<0x_{i-1} - x_i < 0.

A kk-ordered sequence is a sequence where xi1xi<kx_{i-1}-x_i < k for all i>0i > 0. Thus, a strictly increasing sequence is 0-ordered, and a sequence that is increasing but maybe not strictly (like -3, -1 -1, 4, 4, 7 for example) is 1-ordered.

Larger values of kk represent bounded disorder: kk bounds how smaller than its predecessor each element can be.

Write a function disorder_test(k,ls)disorder\_test(k, ls) that receives a nonnegative integer kk and a list lsls of integers and checks whether lsls is kk-ordered, that is, returns True if lsls is kk-ordered, and False otherwise.

Observation

Only the function will be evaluated. If your submission includes a main program (e.g. with testmod), it must be either commented out or inside a condition "if __name__ == ’__main__’"

Sample session
>>> disorder_test(0, [-2, -1, 0, 3, 7, 15, 32])
True
>>> disorder_test(1, [-2, -2, -1, 0, 3, 7, 15, 15, 32])
True
>>> disorder_test(0, [-2, -2, -1, 0, 3, 7, 15, 15, 31, 31])
False
>>> disorder_test(1, [-2, -2, -1, 0, 3, 7, 15, 15, 31, 31])
True
>>> disorder_test(0, [-2, -1, 0, 3, 2, 2])
False
>>> disorder_test(1, [-2, -1, 0, 3, 2, 2])
False
>>> disorder_test(2, [-2, -1, 0, 3, 2, 2])
True
Information
Author
ProAl
Language
English
Official solutions
Python
User solutions
Python