衣衫褴褛的列表是(有限深度)列表,其中每个元素要么是正整数,要么是衣衫褴褛的列表。
如果一个衣衫褴褛的列表包含所有正整数或所有正确的层次分明的列表,则它将被正确地分层。
例如,[1,2,3,6]被正确地分层,因为它是一个仅包含正整数的列表。[[1,2],[[1]],[[6],[[]]]]也有适当的分层,因为它有3个元素,它们本身都是正确的分层列表。请注意,所有的列表不一定都是相同的深度。
列表[[1,2],[[6],4]]没有正确的分层,因为它的一个元素是一个粗糙的列表,它包含了列表和正整数的混合物。
通过任何自然格式获取一张破烂的列表,并确定它是否是一个正确的分层列表。您应该输出两个一致的值中的一个,第一个如果输入是一个正确的分层列表,则第二个不是。
这是密码-高尔夫,所以您的目标是将源代码的大小最小化(以字节为单位)。
[] -> Yes
[1,2,3,6] -> Yes
[[1,2],[[1]],[[6],[[]]]] -> Yes
[[]] -> Yes
[1,[2]] -> No
[[1,2],[[6],4]] -> No
[[1,[2]],[[3],4]] -> No
[1,[]] -> No发布于 2022-08-29 20:58:45
-1多亏了@emanresuA
返回false表示有效,返回true表示无效。
a=>/\d,\[|],\d/.test(JSON.stringify(a))如果我们简单地以字符串作为输入,则为23个字节:
a=>/\d,\[|],\d/.test(a)返回false表示有效,返回true表示无效。
f=(a,q)=>a.some(b=>q-(q=!b.at)||!q&&f(b))或者:
f=(a,q)=>a.some(b=>q-(q=!b.at)|f(q?[]:b))发布于 2022-08-30 06:56:17
d"Ð1åi˜Që®δ.V"©.Vß_输出0表示真实,1输出falsey。
如果挑战使用默认的truthy/falsey定义而不是覆盖元,那么它可能是2 字节数而不是误读:
¸P将错误†作为falsey值,或者不给出真实值。
在网上试试或验证所有测试用例 (有点..。falsey测试用例中包含垃圾输出,没有后续换行符。不知道如何正确地尝试-捕捉05AB1E中的内部药剂错误。)
d # Convert each value in the (implicit) input-list to a 1
# (with a >=0 check)
"..." # Push the recursive string defined below
© # Store it in variable `®` (without popping)
.V # Evaluate and execute it as 05AB1E code
ß # Pop and push the flattened minimum
# (0 for falsey; 1 or "" for truthy)
_ # Check that this value is equal to 0 (1 if 0; 0 otherwise)
# (after which the result is output implicitly)
Ð # Triplicate the current list
1åi # If it contains a 1 as item:
˜Q # Check if the list is unchanged when flattened (thus all are 1s,
# without inner lists)
ë # Else:
δ # Map over each inner list:
® .V # And do a recursive call to `®`
¸ # Wrap the (implicit) input-list into a list
P # Take the product of each inner-most list,
# which will error if an inner list contains both integers and lists
# (implicitly output the result if it didn't error)根据导致错误的整数的深度,( ArithmeticError: bad argument in arithmetic expression或UndefinedError: protocol Enumerable not implemented for int. )是(†)错误。
https://codegolf.stackexchange.com/questions/251420
复制相似问题