Disorder Test X41326


Statement
 

pdf   zip   main.py

thehtml

In a strictly increasing ordered sequence of integers x0,…,xr (such as 5, 8, 12, 20 for example) each element is strictly larger than the previous one: xi > xi−1 for all i > 0, that is, xi−1xi < 0.

A k-ordered sequence is a sequence where xi−1xi < k for all i > 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 k represent bounded disorder: k bounds how smaller than its predecessor each element can be.

Write a function disorder_test(k, ls) that receives a nonnegative integer k and a list ls of integers and checks whether ls is k-ordered, that is, returns True if ls is k-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
Language
English
Official solutions
Python
User solutions
Python