A series or sequence of numbers is usually the product of a function and can either be infinite or finite.
Task:
In this kata we will only consider finite series and you are required to return a code according to the type of sequence:
Code Example
0 unordered [3,5,8,1,14,3]
1 strictly increasing [3,5,8,9,14,23]
2 not decreasing [3,5,8,8,14,14]
3 strictly decreasing [14,9,8,5,3,1]
4 not increasing [14,14,8,8,5,3]
5 constant [8,8,8,8,8,8]
You can expect all the inputs to be non-empty and completely numerical arrays/lists - no need to validate the data; do not go for sloppy code, as rather large inputs might be tested.
Solution:
def sequence_classifier(arr):
if len(set(arr)) == 1: return 5
elif sorted(arr) == arr and len(arr) == len(set(arr)): return 1
elif sorted(arr) == arr: return 2
elif sorted(arr[::-1]) == arr[::-1] and len(arr) == len(set(arr)): return 3
elif sorted(arr[::-1]) == arr[::-1]: return 4
else: return 0
print(sequence_classifier([3,5,8,8,14,23]))
Sequence classifier Solutions | Codewars
No comments :
Post a Comment