编写一个程序来确定一个正整数的周期序列是否具有这样的属性:对于序列中的每一个整数n,在两个连续出现的n之间从来没有比n更多的其他整数。
例如,2, 3, 5, 2, 3, 6, 2, 3, 5, 2, 3, 6, ...确实具有以下属性:每一对连续出现的2最多在它们之间有两个整数(例如2, 3, 5, 2和2, 3, 6, 2;每一对连续出现的3在它们之间最多有三个整数;对于5和6,相同)。
但是,2, 3, 5, 2, 3, 4, 2, 3, 5, 2, 3, 4, ...没有这个属性:两个连续出现的4,即4, 2, 3, 5, 2, 3, 4,在它们之间有四个以上的整数。
输入:正整数周期序列的合理表示。例如,像{2, 3, 5, 2, 3, 6}这样的有限列表可以表示上面的第一个无限序列2, 3, 5, 2, 3, 6, 2, 3, 5, 2, 3, 6, ...。(在这个问题上,问题可以用有限列来描述,而不是无限周期列表。)
输出:真实/虚假的价值。
真实的例子:
{1}
{8, 9}
{2, 3, 4}
{5, 5, 3, 3, 6}
{2, 3, 5, 2, 3, 6}
{6, 7, 3, 5, 3, 7}
{9, 4, 6, 7, 4, 5}
{1, 1, 1, 1, 1, 100, 1}
{1, 9, 1, 8, 1, 7, 1, 11}Falsy示例:
{1, 2, 3}
{2, 3, 9, 5}
{3, 5, 4, 4, 6}
{2, 3, 5, 2, 3, 4}
{3, 5, 7, 5, 9, 3, 7}
{5, 6, 7, 8, 9, 10, 11}
{1, 9, 1, 8, 1, 6, 1, 11}这是古德道夫,所以最短的代码获胜。鼓励所有语言的答案。
发布于 2017-03-25 12:13:27
ṣZL
;çЀ<‘P;çЀ<‘P Main link. Argument: A (array)
; Concatenate A with itself.
çD€ For each n in A, call the helper with left arg. A + A and right arg. n.
‘ Increment all integers in A.
< Perform element-wise comparison of the results to both sides.
P Take the product of the resulting Booleans.
ṣZL Helper link. Left argument: A. Right argument: n
ṣ Split A at all occurrences of n.
Z Zip to transpose rows and columns.
L Length; yield the number of rows, which is equal to the number of columns
of the input to Z.发布于 2017-03-25 19:50:12
ṙJḣ"‘ŒpċṙJḣ"‘Œpċ Main link. Argument: A (array)
J Yield the indicies of A, i.e., [1, ..., len(A)].
ṙ Rotate; yield A, rotated 1, ..., and len(A) units rotated to the left.
‘ Increment; add 1 to all elements of A.
ḣ" Head zipwith; truncate the n-th rotation to length A[n]+1.
Œp Take the Cartesian product of all resulting truncated rotations.
ċ Count the number of times A appears in the result.发布于 2017-03-25 12:02:57
a(L,[H|R]):-nth0(X,R,H),H>=X,a(L,R);length(R,N),nth0(X,L,H),H>=N+X,a(L,R).
a(_,[]).清单应输入两次:
a([1,2,3],[1,2,3]).如果这被认为是不可接受的,则可以添加谓词。
a(L):-a(L,L).这增加了14个字节。
在网上试试
注:您可以通过将查询与“;”(或)分隔,同时测试不同的假案例,并使用“,”(和)分隔不同的真实情况。
例如,使用OP示例:
a([1],[1]),
a([8, 9],[8, 9]),
a([2, 3, 4],[2, 3, 4]),
a([5, 5, 3, 3, 6],[5, 5, 3, 3, 6]),
a([2, 3, 5, 2, 3, 6],[2, 3, 5, 2, 3, 6]),
a([6, 7, 3, 5, 3, 7],[6, 7, 3, 5, 3, 7]),
a([9, 4, 6, 7, 4, 5],[9, 4, 6, 7, 4, 5]),
a([1, 1, 1, 1, 1, 100, 1],[1, 1, 1, 1, 1, 100, 1]),
a([1, 9, 1, 8, 1, 7, 1, 11],[1, 9, 1, 8, 1, 7, 1, 11]).和
a([1, 2, 3],[1, 2, 3]);
a([2, 3, 9, 5],[2, 3, 9, 5]);
a([3, 5, 4, 4, 6],[3, 5, 4, 4, 6]);
a([2, 3, 5, 2, 3, 4],[2, 3, 5, 2, 3, 4]);
a([3, 5, 7, 5, 9, 3, 7],[3, 5, 7, 5, 9, 3, 7]);
a([5, 6, 7, 8, 9, 10, 11],[5, 6, 7, 8, 9, 10, 11]);
a([1, 9, 1, 8, 1, 6, 1, 11],[1, 9, 1, 8, 1, 6, 1, 11]).https://codegolf.stackexchange.com/questions/113858
复制相似问题