Given the input of n and value. The code is supposed to nest the single element list with value repeated n times. The final output should be a multilevel nested list with single repeated values in all sublists.
This is code-golf, so the shortest code in bytes wins!
Test cases:
n = 1, value = 'a': ['a']
n = 2, value = 'a': ['a', ['a']]
n = 3, value = 'a': ['a', ['a', ['a']]]
n = 4, value = 'a': ['a', ['a', ['a', ['a']]]]
n = 5, value = 'a': ['a', ['a', ['a', ['a', ['a']]]]]
n = 6, value = 'a': ['a', ['a', ['a', ['a', ['a', ['a']]]]]]
31 Answers 31
-
\$\begingroup\$ Wow! Good thought!! \$\endgroup\$U13-Forward– U13-Forward2021年12月20日 07:10:59 +00:00Commented Dec 20, 2021 at 7:10
-
1\$\begingroup\$ It looks like you can just do
lambda n,v:eval('[v,'*n+']'*n)\$\endgroup\$xnor– xnor2021年12月20日 07:28:08 +00:00Commented Dec 20, 2021 at 7:28 -
\$\begingroup\$ @xnor Haha! I honestly have no idea how I didn't see that... \$\endgroup\$dingledooper– dingledooper2021年12月20日 07:30:36 +00:00Commented Dec 20, 2021 at 7:30
-
\$\begingroup\$ Even cooler now! \$\endgroup\$U13-Forward– U13-Forward2021年12月20日 09:28:52 +00:00Commented Dec 20, 2021 at 9:28
05AB1E, 3 bytes
F‚Ù
Explanation:
F # Loop the first input amount of times:
‚ # Pair the (implicit) second input-value with the current list
Ù # And uniquify it (which only does something in the first iteration,
# transforming the pair of values to a single wrapped value)
# (after the loop, the result is output implicitly)
-
\$\begingroup\$ 3 bytes! So far I think you're the winner. \$\endgroup\$U13-Forward– U13-Forward2021年12月27日 10:29:21 +00:00Commented Dec 27, 2021 at 10:29
Vyxal r, 4 bytes
(0"U
The joys of golfing language :)
Explained
(0"U
( # n times:
0 # push the value
"U # pair and uniquify - a port of the 05ab1e answer
-
\$\begingroup\$ Wow... 4 bytes!! \$\endgroup\$U13-Forward– U13-Forward2021年12月20日 03:08:21 +00:00Commented Dec 20, 2021 at 3:08
-
\$\begingroup\$ That's so short... how- \$\endgroup\$12944qwerty– 12944qwerty2021年12月20日 03:08:30 +00:00Commented Dec 20, 2021 at 3:08
Python, 34 bytes
f=lambda x,n:n and[x,f(x,n-1)][:n]
Thanks to loopy walt for this one.
Old answers:
Python, 36 bytes
f=lambda x,n:[x][n>1:]or[x,f(x,n-1)]
Python, (削除) 36 (削除ここまで) 35 bytes
f=lambda x,n:n*[1]and[[x]+f(x,n-1)]
Outputs as a singleton list, but that feels like cheating here.
Python, 39 bytes
f=lambda x,n:[x]+(~-n*[1]and[f(x,n-1)])
Python, 37 bytes
f=lambda x,n:x+(~-n*[1]and[f(x,n-1)])
Inputs as a singleton list.
-
\$\begingroup\$ Wow! Very clever! \$\endgroup\$U13-Forward– U13-Forward2021年12月20日 03:07:37 +00:00Commented Dec 20, 2021 at 3:07
-
\$\begingroup\$ You can go zero-based and save two bytes in the topmost lambda (dropping
>1). \$\endgroup\$loopy walt– loopy walt2021年12月20日 06:11:02 +00:00Commented Dec 20, 2021 at 6:11 -
1\$\begingroup\$ Well, they don't say no, do they? How about this
f=lambda x,n:[x,n<2or f(x,n-1)][:n], then (35 bytes)? ato.pxeger.com/… \$\endgroup\$loopy walt– loopy walt2021年12月20日 10:21:02 +00:00Commented Dec 20, 2021 at 10:21 -
1\$\begingroup\$ 34 if you are ok with requiring n>0. ato.pxeger.com/… And I'm not dingledooper :-) \$\endgroup\$loopy walt– loopy walt2021年12月20日 23:05:01 +00:00Commented Dec 20, 2021 at 23:05
-
1\$\begingroup\$ @loopywalt sorry, your avatars are too similar ;) \$\endgroup\$pxeger– pxeger2021年12月20日 23:12:32 +00:00Commented Dec 20, 2021 at 23:12
Proton, 25 bytes
n=>v=>((a=>[v,a])*n)([v])
n=>v=>((a=>[v,a])*n)([v]) This language is stupid
n=> Given n
v=> and v
(a=>[v,a]) pair v with the current accumulator
*n n times
( )([v]) and call that on [v]
-
\$\begingroup\$ Thanks, very cool. "this language" is stupid lol \$\endgroup\$U13-Forward– U13-Forward2021年12月20日 04:15:10 +00:00Commented Dec 20, 2021 at 4:15
-
\$\begingroup\$ @U12-F̉͋̅̾̇orward yep - I made this language, so I know better than anyone just how stupid it is :P \$\endgroup\$2021年12月20日 06:09:45 +00:00Commented Dec 20, 2021 at 6:09
-
\$\begingroup\$ Wow! Really! Very interesting. Cool. \$\endgroup\$U13-Forward– U13-Forward2021年12月20日 07:12:36 +00:00Commented Dec 20, 2021 at 7:12
Wolfram Mathematica, (削除) 30 (削除ここまで) 21 bytes
Print[""@@#~Nest~##]&
-9 bytes from @att!
Sample I/O:
Print[""@@#~Nest~##]&@@{a,3}
[a[a[a]]]
Print[""@@#~Nest~##]&@@{17,3}
[17[17[17]]]
Print[""@@#~Nest~##]&@@{Pi,6}
\$[\pi [\pi [\pi [\pi [\pi [\pi ]]]]]]\$
-
1
-
\$\begingroup\$ (with separator only works if
valueis a string) \$\endgroup\$att– att2021年12月20日 05:10:01 +00:00Commented Dec 20, 2021 at 5:10
R, 46 bytes
function(n,v)Reduce(list,rep(v,n-1),list(v),T)
Non-recursive approach. Pretty-printing and test harness taken from Dominic van Essen who insisted I post this as my own.
-
\$\begingroup\$ Nice; and based on the OP's reply to ZaMoC's comment/question, you can probably leave-out the
,Tto save 2 bytes. \$\endgroup\$Dominic van Essen– Dominic van Essen2021年12月20日 19:53:40 +00:00Commented Dec 20, 2021 at 19:53
BQN, 9 bytes
∾⟜⋈ ́⥊⟜⋈⟜⋈
Pins and bowties FTW
Anonymous tacit function that takes two arguments and returns a nested list. This is why the list formatting looks weird. Run it online!
Explanation
The left argument is the count; the right argument is the value. The example uses a left argument of 2 and a right argument of 0.
∾⟜⋈ ́⥊⟜⋈⟜⋈
⟜⋈ Wrap the right argument in a list: ⟨ 0 ⟩, and then
⟜⋈ Wrap that list in a list: ⟨ ⟨ 0 ⟩ ⟩, and then
⥊ Reshape to a length equaling the left argument: ⟨ ⟨ 0 ⟩ ⟨ 0 ⟩ ⟩
́ Right-fold that list on this function:
⟜⋈ Wrap the right argument in a list: ⟨ ⟨ 0 ⟩ ⟩, and then
∾ Concatenate with the left argument: ⟨ 0 ⟩ ∾ ⟨ ⟨ 0 ⟩ ⟩ → ⟨ 0 ⟨ 0 ⟩ ⟩
Husk, 13 bytes
+J',R1e'[2R']
Not an ideal challenge for a language that doesn't support ragged lists...
+ # join together:
R1 # arg1 repeats of
e # 2-element list of
'[ # '[' and
2 # arg2,
J', # joined by commas,
# and
R'] # arg1 repeats of ']'
Jelly, 5 bytes
W{Ɱṭ/
Value on left, \$n\$ on right
W The value wrapped in a singleton list
{Ɱ for each 1..n.
ṭ/ Reduce by Funky Reverse AppendTM.
-
\$\begingroup\$ Just curious, how do you type those characters? :) \$\endgroup\$U13-Forward– U13-Forward2021年12月20日 04:15:30 +00:00Commented Dec 20, 2021 at 4:15
-
1\$\begingroup\$ @U12-F̉͋̅̾̇orward abrudz.github.io/lb/jelly \$\endgroup\$Unrelated String– Unrelated String2021年12月20日 04:36:22 +00:00Commented Dec 20, 2021 at 4:36
-
\$\begingroup\$ Wow!.... Interesting \$\endgroup\$U13-Forward– U13-Forward2021年12月20日 05:18:50 +00:00Commented Dec 20, 2021 at 5:18
-
1
-
1\$\begingroup\$ @Giuseppe - that's a different answer. Post it. \$\endgroup\$Dominic van Essen– Dominic van Essen2021年12月20日 16:23:02 +00:00Commented Dec 20, 2021 at 16:23
-
\$\begingroup\$ If you insist! \$\endgroup\$Giuseppe– Giuseppe2021年12月20日 19:05:52 +00:00Commented Dec 20, 2021 at 19:05
-
1\$\begingroup\$ Welcome to Code Golf! Nice first answer. \$\endgroup\$2021年12月20日 23:15:54 +00:00Commented Dec 20, 2021 at 23:15
-
\$\begingroup\$ The order is reversed. \$\endgroup\$att– att2021年12月20日 07:06:35 +00:00Commented Dec 20, 2021 at 7:06
-
\$\begingroup\$ @att I guess we will wait for OP to decide... \$\endgroup\$ZaMoC– ZaMoC2021年12月20日 08:20:36 +00:00Commented Dec 20, 2021 at 8:20
-
1\$\begingroup\$ The challenge specification clearly shows which order it should be in, which was OP's decision when they made the challenge in the first place. \$\endgroup\$2021年12月20日 09:18:00 +00:00Commented Dec 20, 2021 at 9:18
-
\$\begingroup\$ @hyper-neutrino OP says my answer is valid. Please check the main comments \$\endgroup\$ZaMoC– ZaMoC2021年12月20日 09:43:23 +00:00Commented Dec 20, 2021 at 9:43
MathGolf, 5 bytes
a*Åαç
Outputs reversed (e.g. [[["a"],"a"],"a"] instead of ["a",["a",["a"]]]), which is allowed based on the comments under the challenge.
Explanation:
a # Wrap the (implicit) input-string into a list
# e.g. "abc" → ["abc"]
* # Repeat it the (implicit) input-integer amount of times
# e.g. 5 → ["abc","abc","abc","abc","abc"]
Å # For-each over these strings,
# using the following 2 characters as inner code-block:
α # Wrap the top two values into a list
# (which will wrap with the implicit loop-index 0 in the first iteration)
# e.g. [0,"abc"] in the first iteration
# [["abc"],"abc"] in the second iteration
# [[["abc"],"abc"],"abc"] in the third, etc.
ç # Remove all 0s from the list with a falsey filter
# (only relevant for the first iteration: [0,"abc"] → ["abc"])
# (after the loop, the entire stack is output implicitly as result)
HBL, 11 bytes
1,(?(-.)(1('?(-.),)?)?
Explanation
A recursive function:
1,(?(-.)(1('?(-.),)?)?
1 Cons
, the second argument with:
(?(-.) If the first argument decremented is truthy (> 1):
(1 ) Cons
('? ) Recursive call with
(-.) First argument decremented
, Second argument unchanged
? with nil (empty list)
Else (the first argument is 1):
? Nil (empty list)
I.e., if the first argument is 1, we get (cons arg2 nil), which simply creates a singleton list containing the second argument; and if the first argument is greater than 1, we get (cons arg2 (cons [recursive-call] nil)), which wraps the result of the recursive call in a singleton list and then prepends the second argument to it.
APL+WIN, 28 bytes
Prompts for n followed by value
( ̄1↓∊n⍴⊂'[',⎕,','),(n←⎕)⍴']'
-
\$\begingroup\$ From the image it looks as if the outermost list (=box?) doesn't contain an 'a' alement... \$\endgroup\$Dominic van Essen– Dominic van Essen2021年12月20日 20:03:46 +00:00Commented Dec 20, 2021 at 20:03
-
\$\begingroup\$ @DominicvanEssen Thanks for picking that up. I could not find an easy fix so went with a new approach \$\endgroup\$Graham– Graham2021年12月20日 21:23:50 +00:00Commented Dec 20, 2021 at 21:23
-
\$\begingroup\$ Hmm... don't you now have an unwanted comma in the innermost list...? \$\endgroup\$Dominic van Essen– Dominic van Essen2021年12月20日 21:52:04 +00:00Commented Dec 20, 2021 at 21:52
-
\$\begingroup\$ @DominicvanEssen Thanks again. Fixed. Old age seems to be getting the better of me ;( \$\endgroup\$Graham– Graham2021年12月20日 22:14:27 +00:00Commented Dec 20, 2021 at 22:14
PHP, (削除) 56 (削除ここまで) 55 bytes
function($n,$a){for($r=[$a];--$n;)$r=[$a,$r];return$r;}
Like often, PHP makes the worst score, akin only to C, but dirtier and with lots of $
EDIT: -1 byte, these dollars allow us some trickery with the parser after all
PowerShell Core, 41 bytes
$n,$a=$args
1..$n|%{$r=$a,@($r)|?{$_}}
$r
Please note that this does not work for n = 1, for some reason PowerShell treats it as a string. Even when forcing to return an array.
Let me know if not OK and I'll withdraw this answer!
-
1\$\begingroup\$ Thanks for the answer, it's ok! doesn't matter much \$\endgroup\$U13-Forward– U13-Forward2021年12月20日 04:17:45 +00:00Commented Dec 20, 2021 at 4:17
-
1\$\begingroup\$ Or you could add an extra code for an if else for whether
nis 1. Then just return the value in a singular list \$\endgroup\$U13-Forward– U13-Forward2021年12月20日 04:18:34 +00:00Commented Dec 20, 2021 at 4:18 -
\$\begingroup\$ I tried but PowerShell doesn't want it: With an explicit .Net list for example \$\endgroup\$Julian– Julian2021年12月20日 19:52:50 +00:00Commented Dec 20, 2021 at 19:52
Charcoal, 15 bytes
FN≔⟦+⟦η⟧υ⟧υ⭆1⊟υ
Try it online! Link is to verbose version of code. Explanation:
FN
Repeat n times...
≔⟦+⟦η⟧υ⟧υ
... prepend value to the initially predefined empty list, then wrap that in another list.
⭆1⊟υ
Unwrap the very last wrapper list and pretty-print it.
Pip -p, (削除) 14 (削除ここまで) 13 bytes
-1 byte by porting Neil's Charcoal answer
Lal:[lPEb]l@0
Explanation
Lal:[lPEb]l@0
l is empty list; a,b are command-line args
La Loop a times:
b b
PE Prepended to
l Current list
[ ] Wrap that result in a singleton list
l: Assign back to l
l After the loop, l is our desired result wrapped in a singleton list
@0 So get the first element
and autoprint it, formatted as a list (-p flag)
JavaScript (ES6), 24 bytes
-2 thanks to @tsh
n=>g=x=>--x?[n,g(x)]:[n]
-
1\$\begingroup\$ Why not
--xinstead. \$\endgroup\$tsh– tsh2021年12月20日 08:25:59 +00:00Commented Dec 20, 2021 at 8:25
Retina, 31 bytes
L$`¶
$`*$([$', )$`*]
, (]+)$
1ドル
Try it online! Takes n and value on separate lines. Explanation:
L$`¶
Match the newline between n and value. This puts n in $` and value in $'.
$`*$([$', )$`*]
Wrap value in n lists.
, (]+)$
1ドル
Remove the trailing comma in the innermost list.
C (clang), 56 bytes
i;f(*x,n){for(i=~n;++i<n;)printf(i<0?",[%s"-i/n:"]",x);}
Takes a string literal as value and n , prints to std out the result.
for(i=~n;++i<n;) - iterate from -n to n
printf(i<0? - select format string:
",[%s"-i/n..x) * new nest, -i/n to skip comma at first stage
:"]" * close nest
```
-
2\$\begingroup\$ This doesn't work for
n=1\$\endgroup\$pxeger– pxeger2021年12月20日 03:18:25 +00:00Commented Dec 20, 2021 at 3:18 -
\$\begingroup\$ @pxeger Edited mine! \$\endgroup\$U13-Forward– U13-Forward2021年12月20日 03:23:07 +00:00Commented Dec 20, 2021 at 3:23
-
\$\begingroup\$ 36 bytes now... \$\endgroup\$U13-Forward– U13-Forward2021年12月20日 09:30:20 +00:00Commented Dec 20, 2021 at 9:30
-
3\$\begingroup\$ You can't remove the
f=because this is a recursive function which requires itself to be assigned tof\$\endgroup\$pxeger– pxeger2021年12月20日 19:21:11 +00:00Commented Dec 20, 2021 at 19:21 -
\$\begingroup\$ @pxeger dingledooper did \$\endgroup\$U13-Forward– U13-Forward2021年12月21日 01:23:36 +00:00Commented Dec 21, 2021 at 1:23
nalways greater than zero? If so, can we taken0-indexed? \$\endgroup\$0\$\endgroup\$