Biorąc pod uwagę skończoną listę, zwróć listę wszystkich jej prefiksów, w tym pustą listę, w porządku rosnącym według ich długości.
(Zasadniczo implementacja funkcji Haskell inits
.)
Detale
- Lista wprowadzania zawiera liczby (lub inny typ, jeśli jest to wygodniejsze).
- The output must be a list of lists.
- The submission can, but does not have to be a function, any default I/O can be used.
- There is a CW answer for all trivial solutions.
Example
[] -> [[]]
[42] -> [[],[42]]
[1,2,3,4] -> [[], [1], [1,2], [1,2,3], [1,2,3,4]]
[4,3,2,1] -> [[], [4], [4,3], [4,3,2], [4,3,2,1]]
Odpowiedzi:
Haskell, 20 bytes
Edit: Yet a byte shorter with a completely different scan.
An anonymous function slightly beating the trivial import.
Try it online!
=<<
for the abbreviation(scanr(\_->init)=<<id) l = scanr(\_->init) l l
.l
from right to left, collecting intermediate results with the function\_->init
.init
to the initial value of the scan, which is alsol
.źródło
brainfuck,
2112 bytes-9 bytes thanks to Arnauld suggesting the separator
ÿ
instead of newlinesTry it online!
Takes bytes through STDIN with no null bytes and prints a series of prefixes separated by the
ÿ
character with a leadingÿ
character. For example, for the inputPrefixes
, the output isÿÿPÿPrÿPreÿPrefÿPrefiÿPrefixÿPrefixeÿPrefixes
.For readability, here's a version with newlines instead.
Explanation:
źródło
JavaScript (ES6), 33 bytes
Try it online!
How?
źródło
CW for all trivial entries
Clean, 19 bytes
Haskell version works in Clean too.
Try it online!
Haskell, 22 bytes
Try it online!
Prolog (SWI), 6 bytes
Try it online!
źródło
Jelly, 3 bytes
Try it online!
How it works
źródło
Japt, 4 bytes
Try it online!
Explanation:
źródło
Perl 6, 13 bytes
Try it online!
To explain:
In Perl 6 you can wrap an operator in square brackets as an alternate way to write a list reduction.
[+] @array
returns the sum of the elements in@array
,[*] @array
returns the product, etc. You can also precede the operator with a backslash to make a "triangular" reduction, which some languages call "scan." So[\+] @array
returns a list consisting of the first element of@array
, then the sum of the first two elements, then the sum of the first three elements, etc.Here
[\,] @_
is a triangular reduction over the input array@_
using the list construction operator,
. So it evaluates to a lists of lists: the first element of@_
, the first two elements of@_
, etc. That's almost what's needed, but the problem calls for a single empty list first. So the first element of the return list is a literal empty list(),
, then the reduction over the input list is flattened into the rest of the return list with|
.źródło
Python 2, 32 bytes
Try it online!
źródło
R,
4039 bytesTry it online!
-1 byte thanks to digEmAll
The output of R's
list
type is a bit weird; it uses sequential indexing, so for instance, the output forlist(1,2)
isTaking input as a vector instead gives a neater output format, although then the inputs are not technically
list
s.źródło
JavaScript, 36 bytes
Try it online!
źródło
Mathematica,
2221 bytes-1 byte thanks to Misha Lavrov!
Pure function. Takes a list as input and returns a list of lists as output. I believe this is the shortest possible solution.
źródło
{}~FoldList@Append~#&
.Husk, 2 bytes
Gets all the
ḣ
eads and then prependsΘ
(in this case[]
):Try it online!
(needs type annotation for empty list: Try it online!)
źródło
J, 5 bytes
Try it online!
źródło
PowerShell, 65 bytes
Try it online!
PowerShell helpfully unrolls lists-of-lists when the default
Write-Output
happens at program completion, so you get one item per line. Tack on a-join','
to see the list-of-lists better, by converting the inner lists into strings.(Ab)uses the fact that attempting to output an empty array (e.g.,
@()
) results in no output, so an empty array input just has''
as the output, since the$a[0..$_]
will result in nothing. It will also throw out some spectacular error messages.źródło
K (ngn/k), 8 bytes
Try it online!
źródło
,\(,()),
in K4. Joining enlisted null along enlisted input? howsitwork?()
is an empty list.(,()),x
prepends it tox
. finally,\
does a concat-scan. thex
is omitted to form a composition. note that the trailing,
is dyadic, so it's "concat", not "enlist".1_',\0,
but my parser isn't smart enough to handle this...Common Lisp, 39 bytes
Try it online!
Explanation
źródło
F#, 53 bytes
I've actually got two pretty similar answers for this, both the same length. They both take a generic sequence
s
as a parameter.First solution:
Try it online!
Seq.take
takes the firstn
elements of the sequence.Seq.init
creates a new sequence with a count (in this case) of the length of sequences
plus 1, and for each element in the sequence takes the firstn
elements ins
.Second solution:
Similar to before, except it creates a sequence from 0 to the length of
s
. Then takes that number of elements froms
.Try this online too!
źródło
fun s->Seq.map(fun n->Seq.take n s){0..Seq.length s}
saves 1 byteMATL,
1512 bytes3 bytes saved thanks to @Giuseppe
Try it at MATL Online.
Due to the way that MATL displays the output, you can't explicitly see the empty array in the cell array. Here is a version that shows the output a little more explicitly.
Explanation
źródło
v
instead of[]
. And doesn't:
use1
as the default first argument? So this could bevin:"G@:)]Xh
for 12 bytes.SWI PROLOG 22 bytes
i(X,Y):-append(X,_,Y).
źródło
Charcoal, 6 bytes
Try it online! Link is to verbose version of code. Explanation:
It's possible at a cost of 1 byte to ask Charcoal to print an
n+1
-element array which includes the input as its last element, but the output is the same, although the cursor position would be different if you then went on to print something else.źródło
05AB1E, 3 bytes
Explanation:
Try it online!
źródło
RAD, 7 bytes
Try it online!
This also works in Dyalog APL as a function.
How?
This works the same for both APL and RAD, given their close relation.
(⊂⍬)
the empty array,
prepended to,\
the prefixes (which exclude the empty array.)źródło
Groovy, 37 bytes
Try it online!
źródło
{it.inits().reverse()}
will work once we get groovy 2.5 on TIOJapt, 5 bytes
Try it online!
źródło
brainfuck, 43 bytes
Take a list of non-null characters as input and returns all prefixes separated by newline. Requires double-infinite or wrapping tape.
Try it online!
źródło
C# (Visual C# Interactive Compiler), 39 bytes
Try it online!
źródło
System.Linq;
in your bytecount. And it seems some of your output logic is in your outputting of the arrays. Because empty array just returns empty array.System.Linq
, I don't have to include this in the byte count. My submission would be considered a different language than say.NET Core
. github.com/dotnet/roslyn/wiki/C%23-Interactive-Walkthrough - You mention printing which is a separate issue, I'd like to get clarity on this first.Array
vsIList
vsIEnumerable
.F# (Mono), 45 bytes
Try it online!
I am not totally sure if this is valid, but it seems like it follows the same "anonymous lambda" syntax that I've seem used in several other languages.
źródło
Java 8+,
8677 bytes-9 bytes thanks to Kevin Cruijssen (getting rid of the import)!
Try it online!
Alternative, 65 bytes
The following will print the results to stdout (due to Olivier Grégoire):
Try it online
źródło
java.util.stream.IntStream
directly and drop the import.x->{for(int i=0;i<=x.size();)System.out.println(x.subList(0,i++));}
(67 bytes). This prints instead of using streams. Printing is usually the shortest way to output complex structures.System.out.print
since the output is still unambiguous.Brachylog, 9 bytes
Try it online!
Explanation
źródło
Ruby,
3129 bytesTry it online!
Explanation:
źródło