给定一个非负整数n,打印P(P(...P({})))的结果,其中P's的个数是n (P是幂集函数)。
0 => {}
1 => P({})
2 => P(P({}))
3 => P(P(P({})))
n => P(f(n-1))input result
0 {}
1 {{}}
2 {{},{{}}}
3 {{},{{}},{{{}}},{{},{{}}}}
...结果应该只包含括号/括号/大括号和逗号、空格和换行符。
发布于 2022-06-10 09:41:14
ŒP¡使用标准I/O规则;从STDIN获取输入
解释:
¡ repeatedly apply
ŒP the power set
to nothing
which defaults to 0
which is turned into the range [1..0]
which is []如果坚持使用{{},{{}}}格式:
ŒP¡ŒṘ“[{]}”y这可能会更短,但我对高尔夫无聊的格式化代码不太感兴趣。
解释:
ŒP¡ŒṘ“[{]}”y
¡ repeatedly apply
ŒP the power set
ŒṘ convert to string representation
y translate
“[{]}” square brackets to curly braces发布于 2022-06-10 09:23:42
发布于 2022-06-10 12:20:57
或12
¾?(ṗ¾?(ṗ
¾ Global array, empty at the start
? Get input n
( Start loop n times
ṗ Powerset具有大括号格式的
(多亏了@lyxal)
¾?(ṗ)S¾S‛{}Ŀ¾?(ṗ)S¾S‛{}Ŀ
¾?(ṗ) The boring stuff
S Stringify the power-setted list
¾ Empty list -> `[]`
‛{} Curly braces
Ŀ Transliterate, replace `[]` with the curly bracesP用它的python表示形式打印列表。在网上试试!
https://codegolf.stackexchange.com/questions/248445
复制相似问题