锡兰,86字节
Object m(Integer+l)=>let(c=l.paired.map(([x,y])=>x<=>y))[if(!smaller in c)equal in c];
该函数将输入作为它的参数,并返回零个或一个布尔值的元组- [false]
用于单调严格递减,[true]
为单调非增的,但不是严格递减,并[]
为上述的无。
可以这样使用:
shared void run() {
print("Monotone strictly decreasing:");
print(m(7, 5, 4, 3, 1));
print(m(42, 41));
print(m(5));
print("Monotone non-increasing, but not strictly decreasing:");
print(m(27, 19, 19, 10, 3));
print(m(6, 4, 2, 2, 2));
print(m(9, 9, 9, 9));
print("None of the above:");
print(m(1, 2, 3, 2));
print(m(10, 9, 8, 7, 12));
print(m(4, 6, 4, 4, 2));
}
输出:
Monotone strictly decreasing:
[false]
[false]
[false]
Monotone non-increasing, but not strictly decreasing:
[true]
[true]
[true]
None of the above:
[]
[]
[]
未经评论的版本:
// Let's decrease the monotony!
//
// Question: http://codegolf.stackexchange.com/q/101036/2338
// My Answer: http://codegolf.stackexchange.com/a/101309/2338
// Define a function which takes a non-empty list `l` of Integers)
// (which arrive as a tuple) and returns an Object (actually
// a `[Boolean*]`, but that is longer.)
Object m(Integer+ l) =>
// the let-clause declares a variable c, which is created by taking
// pairs of consecutive elements in the input, and then comparing
// them. This results in lists like those (for the example inputs):
// { larger, larger, larger, larger }
// { larger }
// {}
// { larger, equal, larger, larger }
// { larger, larger, equal, equal }
// { equal, equal, equal }
// { smaller, smaller, larger }
// { larger, larger, larger, smaller }
// { smaller, larger, equal, larger }
let (c = l.paired.map( ([x,y]) => x<=>y) )
// now we analyze c ...
// If it contains `smaller`, we have an non-decreasing sequence.
// We return `[]` in this case (an empty tuple).
// Otherwise we check whether `equal` is in the list, returning
// `[true]` (a non-strictly decreasing sequence) if so,
// and `[false]` (a strictly decreasing sequence) otherwise.
[if(!smaller in c) equal in c];