2 ms·
This is the easiest one to read, thanks for that. If the intention of posting it was an invitation for review, here's a quick one. Making the first entry a spe
by xorcist 5y ago
This is the easiest one to read, thanks for that. If the intention of posting it was an invitation for review, here's a quick one.
Making the first entry a special case is probably a good idea, but it must also take into account when the first sub_arr is also the only:
>>> monotonic([1, 2])
[]
There's also the fact that equality is considered a valid increase both not a valid decrease. One might expect that either it is valid as both or none. This is not necessarily wrong but may give surprising results:
>>> monotonic([2, 1, 1, 1, 1, 2])
[[1, 2], [1, 1], [1, 1], [1, 2]]
Otherwise a readable and straightforward implementation of a somewhat underspecified problem.
- alecst 5y agoThanks. That was absolutely my intention and I'm grateful you replied. I amended this solution by changing strict > to >=, which seems to have fixed some of the problematic behavior.
- xorcist 5y agoAccepting equality is probably desirable, but as the test for ascending is first it will have precedence. If we accept both we probably need to defer the decision to find which it is. Otherwise there will be an inconsistency like: >>> monotonic([2, 2, 1]) [[2, 2], [1]] >>> monotonic([1, 1, 2]) [[1, 1, 2]] As a separate detail, zeros are regarded false in Python: >>> monotonic([1, 2, 1, 0]) [[1, 2], [1]]