Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 "נחמן חמן מן ן".

share|improve this question

18 Answers 18

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.

share|improve this answer
1  
Exactly the same solution works for the same reason in K. – JohnE Jan 8 at 16:18

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.

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

Retina, 11 7 bytes

.
 $`$0

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

Try it online!

share|improve this answer

Pyth, 3

._z

Prefix builtin does the trick.

Test Suite

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

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
share|improve this answer
5  
Is using the phrase "Try it online!" and not linking to Try it online! morally acceptable? :P – Martin Ender Jan 8 at 17:03
3  
@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 – ETHproductions Jan 8 at 17:06

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
]
share|improve this answer
8  
You can post them as separate answers. – nicael Jan 8 at 16:54
15  
You should post them as separate answers. – Timwi Jan 8 at 17:06
12  
You must post them as separate answers. – nicael Jan 8 at 17:06
16  
You WILL post them as separate answers. – Timwi Jan 8 at 17:07
4  
You CONVINCED me to post them as separate answers. – YoYoYonnY Jan 8 at 22:30

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.
}*
share|improve this answer
    
I totally expected CJam to be shorter than that. – Timwi Jan 8 at 16:20
    
@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. – Martin Ender Jan 8 at 16:22
    
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 – FryAmTheEggman Jan 8 at 18:30

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.

share|improve this answer

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.

share|improve this answer

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;
}
share|improve this answer

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);
    }
}
share|improve this answer

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
share|improve this answer

Mathematica, 29 bytes

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

TODO: explanation

share|improve this answer

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.

share|improve this answer

𝔼𝕊𝕄𝕚𝕟, 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
share|improve this answer

Javascript ES6, 29 bytes

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

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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