17
\$\begingroup\$

Actually not inspired neither by Atbash Self Palindromes nor by Generalized Gematria Calculator.

Given a string s of length n, output the Revu'a sequence, which is the first character of s, the first two characters of s, ... the first n–2 characters of s, the first n–1 characters of s, the entire s.

The string will only consist of Unicode (any encoding you want) characters that have strong directionality and are found in the range 0x0000 through 0xFFFF. However, no directionality control characters will occur. All characters in any given string will have the same directionality.

You may return in array notation ["t","te","tes","test"], as a space-separated string "t te tes test", as multi-line text
t
te
tes
test
, a pre-formatted array

t
te
tes
test
, or anything similar. Amounts of leading, separating, and trailing spacing is not important, and neither are trailing newline. Ask if in doubt.

Right-to-Left input must result in Right-to-Left output in proper order:
Input: "נחמן"
Output: "נ נח נחמ נחמן" or

נ
נח
נחמ
נחמן
, or ["נ","נח","נחמ","נחמן"]. Among invalid results are "נחמן נחמ נח נ" ,"ן מן חמן נחמן", and "נחמן חמן מן ן".

\$\endgroup\$
2
  • \$\begingroup\$ 1) shouldn't it be ["ן",‎ "נח",‎ "נחם",‎ "נחמן"]? ‎(2) bonus points if when given that particular string, you finish off with "מאומן". \$\endgroup\$ Commented May 2, 2021 at 18:55
  • \$\begingroup\$ @NoLongerBreathedIn I didn't want to bother people with converting letterforms. And sure, if you implement "m'uman" you'll get cookies ;-) \$\endgroup\$
    – Adám
    Commented May 2, 2021 at 18:57

34 Answers 34

17
\$\begingroup\$

Dyalog APL, 2 bytes

,\

Cumulative reduce by concatenate. Try it here.

The formatting of the output is nicer when you prefix a , but it clearly shows the correct order without.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Exactly the same solution works for the same reason in K. \$\endgroup\$
    – JohnE
    Commented Jan 8, 2016 at 16:18
  • \$\begingroup\$ @JohnE Does K handle Unicode? \$\endgroup\$
    – Adám
    Commented Sep 8, 2016 at 5:52
13
\$\begingroup\$

JavaScript (ES6), 27 26 25 bytes

Saved one byte thanks to @nicael and @MartinBüttner, one thanks to @Neil

x=>x.replace(/.?/g,"$` ")

Takes advantage of some built-in features of JS's .replace function. Specifically, in the replacement, $` becomes everything preceding the matched character. Using the regex /.?/g rather than /./g means it also matches the empty string at the end.

\$\endgroup\$
3
  • \$\begingroup\$ I saved another byte: f=x=>x.replace(/.?/g,"$ ")`. You get an extra leading space but that's allowed. \$\endgroup\$
    – Neil
    Commented Jan 9, 2016 at 13:09
  • \$\begingroup\$ @Neil Thanks, I had no clue that would work! \$\endgroup\$ Commented Jan 10, 2016 at 22:59
  • \$\begingroup\$ Ugh, I forgot to quote my ` properly, but I see you worked out what I meant. \$\endgroup\$
    – Neil
    Commented Jan 11, 2016 at 9:46
6
\$\begingroup\$

Retina, 11 7 bytes

.
 $`$0

Output is space-separated, with a leading space and a trailing linefeed.

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ For posterity, it's portable to Perl for 5 more bytes : perl -pE 's/./$$&\n/g'`. (I'm 11 months late, I know) \$\endgroup\$
    – Dada
    Commented Nov 8, 2016 at 21:21
6
\$\begingroup\$

Japt, 10 4 bytes

I didn't realize that a cumulative reduce would be so useful in this case. :-)

UŒ+

Outputs as an array, comma-separated by default. If this is not allowed, use this 6-byte code instead:

U¬å+ ·

Try it online!

How it works

      // Implicit: U = input string
U¬    // Split U into chars.
  å+  // Cumulative reduce: loop through each item in the array, concatenating it to the total.
      // ["t","e","s","t"] => ["t","te","tes","test"].
      // Implicit: output last expression
\$\endgroup\$
2
  • 7
    \$\begingroup\$ Is using the phrase "Try it online!" and not linking to Try it online! morally acceptable? :P \$\endgroup\$ Commented Jan 8, 2016 at 17:03
  • 3
    \$\begingroup\$ @MartinBüttner I'd been using that phrase in Japt answers for about a month before Dennis trademarked it. I feel I should have some moral right to keep using it :P \$\endgroup\$ Commented Jan 8, 2016 at 17:06
6
\$\begingroup\$

Brainfuck, 40 bytes

My console doesn't support Right-to-Left characters, but I don't think it will work :c

++++++++++>,[>,]<[<]>[[<+>-]<[<]>[.>]>]

Ungolfed:

++++++++++> # Store 10 (Newline)
,[>,]       # Store input
<[<]>       # Goto first character
[           # While next character
  [<+>-]    # Copy character to the left
  <[<]>     # Goto first character
  [.>]      # Print all charaters
  >         # Go to next character
]
\$\endgroup\$
6
  • 14
    \$\begingroup\$ You can post them as separate answers. \$\endgroup\$
    – nicael
    Commented Jan 8, 2016 at 16:54
  • 21
    \$\begingroup\$ You should post them as separate answers. \$\endgroup\$
    – Timwi
    Commented Jan 8, 2016 at 17:06
  • 18
    \$\begingroup\$ You must post them as separate answers. \$\endgroup\$
    – nicael
    Commented Jan 8, 2016 at 17:06
  • 22
    \$\begingroup\$ You WILL post them as separate answers. \$\endgroup\$
    – Timwi
    Commented Jan 8, 2016 at 17:07
  • 12
    \$\begingroup\$ You CONVINCED me to post them as separate answers. \$\endgroup\$
    – yyny
    Commented Jan 8, 2016 at 22:30
4
\$\begingroup\$

05AB1E, 1 byte

η

Builtins ftw ¯\_(ツ)_/¯

Outputs as a list of prefixes.

Try it online or verify all test cases.

Explanation:

η  # Push a list of prefixes of the (implicit) input-string
   # (and output this list implicitly as result)
\$\endgroup\$
4
\$\begingroup\$

Python, 35

f=lambda s:f(s[:-1])+[s]if s else[]

Couldn't find a way to use and/or to simplify the recursion because [] is falsy.

Recursive solution, returns a list of strings.

Try it online

\$\endgroup\$
4
\$\begingroup\$

Prolog (SWI), 60 49 bytes

Code:

p(X):-findall(S,atom_prefix(X,S),[_|R]),write(R).

Explained:

atom_prefix with X set to input and S as a variable gives 1 prefix of the atom X starting with the empty atom.

findall gets all solutions and puts them in a list.

[_|R] throws away the head (the empty atom) and stores the tail in R

Examples:

p('נחמן').
[נ, נח, נחמ, נחמן]

p('test').
[t, te, tes, test]

Try it online here

Edit: saved 11 bytes by only storing the tail in R.

\$\endgroup\$
3
\$\begingroup\$

Pyth, 3

._z

Prefix builtin does the trick.

Test Suite

\$\endgroup\$
3
\$\begingroup\$

GNU Sed, 21

Score includes +1 for -E option to sed:

:
s/^(\S+)\S/\1 &/
t

Works for LTR, but not RTL - I missed that bit.. Actually it does work, the RTL was just not rendering correctly in my terminal. It works fine with IO viewed in a sensible text editor (e.g. emacs). It also works in Ideone:

Try it online.

\$\endgroup\$
3
\$\begingroup\$

Brachylog, 5 bytes

@[@w\

Try it online!

Explanation

@[       Take a prefix of the input
  @w     Write this prefix to STDOUT followed by a linebreak
    \    False: try another prefix

Right-to-left strings seem to work properly, even though I never even considered them.

\$\endgroup\$
2
  • \$\begingroup\$ Why non-competing? \$\endgroup\$
    – Adám
    Commented Nov 8, 2016 at 21:58
  • \$\begingroup\$ @Adám @[ and @w were implemented necessarily after April/May 2016. One could find the exact date on the Github commits but it surely isn't before this challenge was submitted. \$\endgroup\$
    – Fatalize
    Commented Nov 8, 2016 at 22:01
2
\$\begingroup\$

CJam, 9 bytes

l{N2$@+}*

Output is linefeed-separated.

Test it here.

Explanation

l     e# Read input.
{     e# Fold this block over the input, which is effectively a foreach-loop which skips
      e# the first character...
  N   e#   Push a linefeed.
  2$  e#   Copy the previous string.
  @   e#   Pull up the current character.
  +   e#   Concatenate.
}*
\$\endgroup\$
3
  • \$\begingroup\$ I totally expected CJam to be shorter than that. \$\endgroup\$
    – Timwi
    Commented Jan 8, 2016 at 16:20
  • \$\begingroup\$ @Timwi There is neither a "get all prefixes/suffixes" built-in nor a higher-order function for generalised accumulation, so even if this isn't optimal I doubt it can be beaten significantly. \$\endgroup\$ Commented Jan 8, 2016 at 16:22
  • \$\begingroup\$ Ll{+_p}/; is same length, posting because I'm not sure if someone with more experience might be able to golf it more, and also maybe fix the quotes thing :P \$\endgroup\$ Commented Jan 8, 2016 at 18:30
2
\$\begingroup\$

JavaScript, 36 bytes

x=>[...x].map((c,i)=>x.slice(0,i+1))

Demo:

a=x=>[...x].map((c,i)=>x.slice(0,i+1));
document.write(
  a("test")+"<br>"+
  a("נחמן")
)

The principle is to map and output the slice of string from the first char to the every char in the word. Surprisingly, this works perfectly for the RTL strings too, no optimization needed.

\$\endgroup\$
2
\$\begingroup\$

My console doesn't support Right-to-Left characters, but I don't think it will work :c

C, 74 bytes (2nd entry)

char m[2<<9];i;main(){do{m[i]=getchar();printf("%s ",m);}while(m[i++]>0);}

Ungolfed:

#include <stdio.h>

// char, because `printf("%s", str);` expects a array of characters.
char str[2<<9];
int  str_len = 0;
int main(void) {
    do {
        str[str_len]=getchar();
        printf("%s ", str);
    } while(m[i++]>0);
    return 0;
}
\$\endgroup\$
2
\$\begingroup\$

My console doesn't support Right-to-Left characters, but I don't think it will work :c

C, 105 bytes (3th entry)

m[2<<9];i;j;k;main(){while((m[i++]=getchar())<0);for(;j<i;j++,putchar(10))for(k=0;k<j;k++)putchar(m[k]);}

Ungolfed:

#include <stdio.h>

int str[2<<9];
int str_len = 0;
int main(void) {
    do {
        str[str_len] = getchar();
    } while(str[str_len++] != EOF);
    int i;
    for(i=0; i<str_len; i++) {
        int j;
        for(j=0; j<i; j++) {
          putchar(str[j]);
        }
        putchar(10);
    }
}
\$\endgroup\$
2
\$\begingroup\$

TI-BASIC, 18 bytes

For(X,1,10^(9
Disp sub(Ans,1,X
End

Not technically valid: TI-BASIC doesn't support Unicode.

Name this prgmA, and input using Ans.

Program recursion would be shorter, but there would be no way to initialize the variables. Therefore, we display a substring of the input at each iteration. The input is never overwritten, since Disp doesn't return a value.

Eventually, the program terminates with an error after printing the whole string.

\$\endgroup\$
2
\$\begingroup\$

Python, 54 Bytes

b='';y=input()
for a in range(len(y)):b+=y[a];print(b)
\$\endgroup\$
2
\$\begingroup\$

Java 8, 95 92 58 bytes

s->{for(int i=0;;)System.out.println(s.substring(0,++i));}

Try it online.

Explanation:

s->{                        // Method with String parameter and no return-type
  for(int i=0;              //  Index-integer, starting at 0
      ;)                    //  Loop indefinitely:
    System.out.println(     //   Print with trailing newline:
      s.substring(0,++i     //    Increase the index by 1 first with `++i`
                       ));} //    And print an input-substring in the range [0,i)

Stops the infinite loop with a StringIndexOutOfBoundsException after everything has been printed to STDOUT already, which is allowed according to the meta.

\$\endgroup\$
1
\$\begingroup\$

MATL, 8 bytes

Uses current version (8.0.0) of language/compiler

jtn1X"YR

Example

>> matl
 > jtn1X"YR
 >
> test
t
te
tes
test

Explanation

j           % input string
tn          % duplicate and get length, say "N"
1X"         % repeat string N times vertically. Gives a char matrix
YR          % lower triangular part of matrix. Implicitly print
\$\endgroup\$
1
\$\begingroup\$

Mathematica, 29 bytes

#<>#2&~FoldList~Characters@#&

TODO: explanation

\$\endgroup\$
1
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 7 chars / 16 bytes

ᴉⓜᵖ ᵴ˖$

Try it here (Firefox only).

There's probably a builtin for this somewhere - I just haven't found it.

Explanation

ᴉⓜᵖ ᵴ˖$ // implicit: ᴉ=split input, ᵴ=empty string
ᴉⓜ      // map over ᴉ
   ᵖ ᵴ˖$ // push ᵴ+=(mapped item char)
         // implicit stack output, separated by newlines
\$\endgroup\$
1
\$\begingroup\$

Javascript ES6, 29 bytes

(a,b='')=>[...a].map(x=>b+=x)

This ain't winning anything, but it's a simple solution.

\$\endgroup\$
1
\$\begingroup\$

Pyth, 11 Bytes

Vlz=k+k@zNk

Try It Out

Explanation

(z=input)
(k="")
V        for N in Range(
lz       length of z):
=k+k@zN     k=k+z[N]
k           print(k)
\$\endgroup\$
1
\$\begingroup\$

Python, 32 bytes

f=lambda s:s and f(s[:-1])+" "+s

Recursive function that outputs a space-separated string with a leading space.

A 34-byte program (Python 2):

s=""
for c in input():s+=c;print s
\$\endgroup\$
1
\$\begingroup\$

PowerShell v2+, 28 bytes

[char[]]$args[0]|%{($o+=$_)}

Takes input $args[0], casts it as a char-array, pipes the characters into a loop |%{...}. Each iteration, we accumulate onto $o via += the current character $_. That expression is encapsulated in parens so a copy is placed on the pipeline. At end of execution, the pipeline is flushed via Write-Output which puts a newline between elements.

PS C:\Tools\Scripts\golfing> .\spell-out-the-revua "נחמן"
נ
נח
נחמ
נחמן

PS C:\Tools\Scripts\golfing> .\spell-out-the-revua "PPCG"
P
PP
PPC
PPCG
\$\endgroup\$
1
\$\begingroup\$

V, 5 bytes

òÄ$xh

Try it online!

Explanation:

ò       " Recursively:
 Ä      "   Duplicate this line
  $     "   Move to the end of this line
   x    "   Delete one character
    h   "   Move one character to the right, which will throw an error when the line is one character long
\$\endgroup\$
1
\$\begingroup\$

jq, 17 bytes

.[:range(length)]

Try it online!

-10 from ovs.

\$\endgroup\$
1
  • \$\begingroup\$ .[:range(length)] works for 17 bytes. There is a way to save 2 more bytes, but I'll leave that to you ;) \$\endgroup\$
    – ovs
    Commented Sep 10, 2021 at 12:41
0
\$\begingroup\$

PHP, 59 Bytes

for(;$i++<mb_strlen($argn);)echo"\n".mb_substr($argn,0,$i);

Online Version

\$\endgroup\$
0
\$\begingroup\$

Unwanted, Unnecessary, Opportunistic, 9 bytes

E<+S0;*P 
\$\endgroup\$
0
\$\begingroup\$

.+?, 18 bytes

^((.+).)
\2\n\1
$

Explanation

^((.+).)   Replace the first line with
\2         The first line missing the last character
  \n       A newline
    \1     The first line
$          Repeat until the regex doesn't match (when first line has 1 character)

Try it online! (The second command line argument is input)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.