47
\$\begingroup\$

Disclaimer: This challenge inspired by me spending the morning debugging recursive functions, which have been frying my brain a little.

Here's an example of recursion, from a letter, we keep going to the previous letter of the alphabet, printing out each one as we go, until we hit the letter a, then we print that and stop. We do this for each letter in a string and there's a pretty pattern at the end of it.

The task

  • Start with a string of characters
    • The input string may only contain the lower-case letters a-z and the space character.
  • For each letter, produce a line of text (terminal output or variable or whatever)
    • Print out the letter
    • Print out the previous letter (on the same line)
    • Print out the previous letter (on the same line)
    • Print out the previous letter (on the same line)
    • ...
    • If the letter is 'a', print it out and move to the next line.
    • If it's a space, print out an empty line (or one just containing the space character.

The rules

  • It's golf, try and make your code short.
  • Any language you like.
  • Please include a link to an online interpreter.
  • The output should be human-readable (e.g. I can't work it out from a list of bytes.
  • Follow the rules of the standard loopholes ¯\_(ツ)_/¯
  • Recursion is not mandated by the rules, but it's probably necessary.

Test Cases

Input: 'abcdefghijklmnopqrstuvwxyz' Output:

a
ba
cba
dcba
edcba
fedcba
gfedcba
hgfedcba
ihgfedcba
jihgfedcba
kjihgfedcba
lkjihgfedcba
mlkjihgfedcba
nmlkjihgfedcba
onmlkjihgfedcba
ponmlkjihgfedcba
qponmlkjihgfedcba
rqponmlkjihgfedcba
srqponmlkjihgfedcba
tsrqponmlkjihgfedcba
utsrqponmlkjihgfedcba
vutsrqponmlkjihgfedcba
wvutsrqponmlkjihgfedcba
xwvutsrqponmlkjihgfedcba
yxwvutsrqponmlkjihgfedcba
zyxwvutsrqponmlkjihgfedcba

Input: 'zyxwvutsrqponmlkjihgfedcba'

zyxwvutsrqponmlkjihgfedcba
yxwvutsrqponmlkjihgfedcba
xwvutsrqponmlkjihgfedcba
wvutsrqponmlkjihgfedcba
vutsrqponmlkjihgfedcba
utsrqponmlkjihgfedcba
tsrqponmlkjihgfedcba
srqponmlkjihgfedcba
rqponmlkjihgfedcba
qponmlkjihgfedcba
ponmlkjihgfedcba
onmlkjihgfedcba
nmlkjihgfedcba
mlkjihgfedcba
lkjihgfedcba
kjihgfedcba
jihgfedcba
ihgfedcba
hgfedcba
gfedcba
fedcba
edcba
dcba
cba
ba
a

Input: 'hello world' Output:

hgfedcba
edcba
lkjihgfedcba
lkjihgfedcba
onmlkjihgfedcba

wvutsrqponmlkjihgfedcba
onmlkjihgfedcba
rqponmlkjihgfedcba
lkjihgfedcba
dcba
\$\endgroup\$
14
  • 2
    \$\begingroup\$ May we output a list of strings instead of a single string with linefeeds? \$\endgroup\$
    – Arnauld
    Nov 21, 2019 at 13:10
  • 16
    \$\begingroup\$ I don't think a single answer uses recursion \$\endgroup\$
    – Jo King
    Nov 22, 2019 at 1:39
  • 3
    \$\begingroup\$ “Human readable” prohibits most of the languages popular here! \$\endgroup\$
    – WGroleau
    Nov 22, 2019 at 2:50
  • 2
    \$\begingroup\$ @WGroleau Ah, I think I've spotted what's gone wrong here. I didn't require that the code should be human-readable, only the result. \$\endgroup\$
    – AJFaraday
    Nov 22, 2019 at 9:13
  • 3
    \$\begingroup\$ Recursion is never necessary. At the very worst, you can just use stack data structure to have your own "recursion" in a flat loop, ending when the stack is empty. \$\endgroup\$ Nov 22, 2019 at 11:41

76 Answers 76

33
\$\begingroup\$

Dyalog APL Extended, 4 bytes

⌽∘⍳¨

Try it online!

 ∘    the function composition of
  ⍳     iota - 'a'..input for alphabet chars, empty array for space
⌽       and after, reverse
   ¨  applied to each
\$\endgroup\$
5
  • 34
    \$\begingroup\$ We have peaked as a species. \$\endgroup\$
    – TRose
    Nov 21, 2019 at 22:16
  • 3
    \$\begingroup\$ Answers in obscure languages usually make the language name a link to documentation. Could you add that? \$\endgroup\$ Nov 22, 2019 at 1:01
  • 3
    \$\begingroup\$ @JustinLardinois I don't think APL is exactly an obscure language, but here's the link: APL - Wikipedia. \$\endgroup\$
    – Trebor
    Nov 22, 2019 at 11:51
  • \$\begingroup\$ @JustinLardinois Added (I did manually remove the link from the TIO format when renaming the title...) The only used difference from regular Dyalog APL is the (surprisingly useful) extended . \$\endgroup\$
    – dzaima
    Nov 22, 2019 at 11:58
  • \$\begingroup\$ @Trebor APL isn't obscure, but this flavor of it is. It's not even mentioned in that Wikipedia article. \$\endgroup\$ Nov 22, 2019 at 22:17
14
\$\begingroup\$

Haskell, 26 24 bytes

-2 bytes thanks to FrownyFrog.

map(\i->reverse['a'..i])

Try it online!

Pointless (or pointfree :P) for you.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ 24 with map(\i->reverse['a'..i]) \$\endgroup\$
    – FrownyFrog
    Nov 21, 2019 at 23:04
  • 2
    \$\begingroup\$ Those darn enumFromThenTo functions and their ungolfiness. \$\endgroup\$ Nov 21, 2019 at 23:31
  • 2
    \$\begingroup\$ I want to say map(\i->[i,pred i..'a']) but it's the same size. \$\endgroup\$ Nov 22, 2019 at 2:24
  • 2
    \$\begingroup\$ I want to say map$ \i->reverse['a'..i] but it's the same size, too. \$\endgroup\$
    – A. R.
    Nov 26, 2019 at 18:03
14
\$\begingroup\$

C (gcc),  58  56 bytes

Saved 2 bytes thanks to @KritixiLithos

c;f(char*s){while(c=c&&putchar(c>96?c:10)^10?c-1:*s++);}

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ I believe the putchar call can be used in the while condition to save a byte. \$\endgroup\$
    – user41805
    Nov 22, 2019 at 10:24
  • \$\begingroup\$ @KritixiLithos Thanks! This saves 2 bytes, although I suspect there's a way to save more. \$\endgroup\$
    – Arnauld
    Nov 22, 2019 at 10:52
  • \$\begingroup\$ You can also use 0 instead of 10 and get spaces instead of newlines between the outputs. (only tested on TIO YMMV) \$\endgroup\$ Nov 22, 2019 at 10:56
  • 1
    \$\begingroup\$ @JohnHamilton The delimiter will then be NULL-bytes (\0). Although TIO shows them as spaces, most compilers will not show any output for NULL-bytes instead. 9 instead of 10 can be used with tab-delimiters though, for the same -2. \$\endgroup\$ Nov 22, 2019 at 11:03
  • \$\begingroup\$ @KevinCruijssen Yeah, you're right. It could create problems with the null character. Tab delimiter works better. \$\endgroup\$ Nov 22, 2019 at 11:05
14
\$\begingroup\$

Malbolge20 and Malbolge Unshackled polyglot, around 2MB, 107KB compressed.

Alright. I spent around 30 minutes on this answer (current record). It's really optimal answer. Albeit not so fast and not so memory-exhausting (only ~512 megabytes), it's still a Malbolge answer so please keep that noted.

The program is packed using 7zip and PPMd compression algorithm. You can download it here.

Interpreter that works the best with the program.

#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char* translation = "5z]&gqtyfr$(we4{WP)H-Zn,[%\\3dL+Q;>U!pJS72Fh"
        "OA1CB6v^=I_0/8|jsb9m<.TVac`uY*MK'X~xDl}REokN:#?G\"i@";

typedef struct Word {
    unsigned int area;
    unsigned int high;
    unsigned int low;
} Word;

void word2string(Word w, char* s, int min_length) {
    if (!s) return;
    if (min_length < 1) min_length = 1;
    if (min_length > 20) min_length = 20;
    s[0] = (w.area%3) + '0';
    s[1] = 't';
    char tmp[20];
    int i;
    for (i=0;i<10;i++) {
        tmp[19-i] = (w.low % 3) + '0';
        w.low /= 3;
    }
    for (i=0;i<10;i++) {
        tmp[9-i] = (w.high % 3) + '0';
        w.high /= 3;
    }
    i = 0;
    while (tmp[i] == s[0] && i < 20 - min_length) i++;
    int j = 2;
    while (i < 20) {
        s[j] = tmp[i];
        i++;
        j++;
    }
    s[j] = 0;
}

unsigned int crazy_low(unsigned int a, unsigned int d){
    unsigned int crz[] = {1,0,0,1,0,2,2,2,1};
    int position = 0;
    unsigned int output = 0;
    while (position < 10){
        unsigned int i = a%3;
        unsigned int j = d%3;
        unsigned int out = crz[i+3*j];
        unsigned int multiple = 1;
        int k;
        for (k=0;k<position;k++)
            multiple *= 3;
        output += multiple*out;
        a /= 3;
        d /= 3;
        position++;
    }
    return output;
}

Word zero() {
    Word result = {0, 0, 0};
    return result;
}

Word increment(Word d) {
    d.low++;
    if (d.low >= 59049) {
        d.low = 0;
        d.high++;
        if (d.high >= 59049) {
            fprintf(stderr,"error: overflow\n");
            exit(1);
        }
    }
    return d;
}

Word decrement(Word d) {
    if (d.low == 0) {
        d.low = 59048;
        d.high--;
    }else{
        d.low--;
    }
    return d;
}

Word crazy(Word a, Word d){
    Word output;
    unsigned int crz[] = {1,0,0,1,0,2,2,2,1};
    output.area = crz[a.area+3*d.area];
    output.high = crazy_low(a.high, d.high);
    output.low = crazy_low(a.low, d.low);
    return output;
}

Word rotate_r(Word d){
    unsigned int carry_h = d.high%3;
    unsigned int carry_l = d.low%3;
    d.high = 19683 * carry_l + d.high / 3;
    d.low = 19683 * carry_h + d.low / 3;
    return d;
}

// last_initialized: if set, use to fill newly generated memory with preinitial values...
Word* ptr_to(Word** mem[], Word d, unsigned int last_initialized) {
    if ((mem[d.area])[d.high]) {
        return &(((mem[d.area])[d.high])[d.low]);
    }
    (mem[d.area])[d.high] = (Word*)malloc(59049 * sizeof(Word));
    if (!(mem[d.area])[d.high]) {
        fprintf(stderr,"error: out of memory.\n");
        exit(1);
    }
    if (last_initialized) {
        Word repitition[6];
        repitition[(last_initialized-1) % 6] =
                ((mem[0])[(last_initialized-1) / 59049])
                    [(last_initialized-1) % 59049];
        repitition[(last_initialized) % 6] =
                ((mem[0])[last_initialized / 59049])
                    [last_initialized % 59049];
        unsigned int i;
        for (i=0;i<6;i++) {
            repitition[(last_initialized+1+i) % 6] =
                    crazy(repitition[(last_initialized+i) % 6],
                        repitition[(last_initialized-1+i) % 6]);
        }
        unsigned int offset = (59049*d.high) % 6;
        i = 0;
        while (1){
            ((mem[d.area])[d.high])[i] = repitition[(i+offset)%6];
            if (i == 59048) {
                break;
            }
            i++;
        }
    }
    return &(((mem[d.area])[d.high])[d.low]);
}

unsigned int get_instruction(Word** mem[], Word c,
        unsigned int last_initialized,
        int ignore_invalid) {
    Word* instr = ptr_to(mem, c, last_initialized);
    unsigned int instruction = instr->low;
    instruction = (instruction+c.low + 59049 * c.high
            + (c.area==1?52:(c.area==2?10:0)))%94;
    return instruction;
}

int main(int argc, char* argv[]) {
    Word** memory[3];
    int i,j;
    for (i=0; i<3; i++) {
        memory[i] = (Word**)malloc(59049 * sizeof(Word*));
        if (!memory) {
            fprintf(stderr,"not enough memory.\n");
            return 1;
        }
        for (j=0; j<59049; j++) {
            (memory[i])[j] = 0;
        }
    }
    Word a, c, d;
    unsigned int result;
    FILE* file;
    if (argc < 2) {
        // read program code from STDIN
        file = stdin;
    }else{
        file = fopen(argv[1],"rb");
    }
    if (file == NULL) {
        fprintf(stderr, "File not found: %s\n",argv[1]);
        return 1;
    }
    a = zero();
    c = zero();
    d = zero();
    result = 0;
    while (!feof(file)){
        unsigned int instr;
        Word* cell = ptr_to(memory, d, 0);
        (*cell) = zero();
        result = fread(&cell->low,1,1,file);
        if (result > 1)
            return 1;
        if (result == 0 || cell->low == 0x1a || cell->low == 0x04)
            break;
        instr = (cell->low + d.low + 59049*d.high)%94;
        if (cell->low == ' ' || cell->low == '\t' || cell->low == '\r'
                || cell->low == '\n');
        else if (cell->low >= 33 && cell->low < 127 &&
                (instr == 4 || instr == 5 || instr == 23 || instr == 39
                    || instr == 40 || instr == 62 || instr == 68
                    || instr == 81)) {
            d = increment(d);
        }
    }
    if (file != stdin) {
        fclose(file);
    }
    unsigned int last_initialized = 0;
    while (1){
        *ptr_to(memory, d, 0) = crazy(*ptr_to(memory, decrement(d), 0),
                *ptr_to(memory, decrement(decrement(d)), 0));
        last_initialized = d.low + 59049*d.high;
        if (d.low == 59048) {
            break;
        }
        d = increment(d);
    }
    d = zero();

    unsigned int step = 0;
    while (1) {
        unsigned int instruction = get_instruction(memory, c,
                last_initialized, 0);
        step++;
        switch (instruction){
            case 4:
                c = *ptr_to(memory,d,last_initialized);
                break;
            case 5:
                if (!a.area) {
                    printf("%c",(char)(a.low + 59049*a.high));
                }else if (a.area == 2 && a.low == 59047
                        && a.high == 59048) {
                    printf("\n");
                }
                break;
            case 23:
                a = zero();
                a.low = getchar();
                if (a.low == EOF) {
                    a.low = 59048;
                    a.high = 59048;
                    a.area = 2;
                }else if (a.low == '\n'){
                    a.low = 59047;
                    a.high = 59048;
                    a.area = 2;
                }
                break;
            case 39:
                a = (*ptr_to(memory,d,last_initialized)
                        = rotate_r(*ptr_to(memory,d,last_initialized)));
                break;
            case 40:
                d = *ptr_to(memory,d,last_initialized);
                break;
            case 62:
                a = (*ptr_to(memory,d,last_initialized)
                        = crazy(a, *ptr_to(memory,d,last_initialized)));
                break;
            case 81:
                return 0;
            case 68:
            default:
                break;
        }

        Word* mem_c = ptr_to(memory, c, last_initialized);
        mem_c->low = translation[mem_c->low - 33];

        c = increment(c);
        d = increment(d);
    }
    return 0;
}

It works!

It works!

\$\endgroup\$
1
  • \$\begingroup\$ Is MBASM what you use to assemble Malbolge programs? \$\endgroup\$
    – user92753
    Feb 8, 2022 at 20:36
8
\$\begingroup\$

PowerShell, 37 28 bytes

-9 bytes thanks to mazzy

$args|%{-join($_..'a'-le$_)}

Try it online!

Takes input via splatting and uses char ranges introduced in PS v6. The range is then filtered out by only taking elements that are less than the current character. This in turns means decreasing ranges are unaffected (e.g. 'z'..'a') whereas increasing ranges (e.g. ' '..'a') will filter out everything except the space.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ you can save some bytes Try it online! \$\endgroup\$
    – mazzy
    Nov 21, 2019 at 15:17
  • 1
    \$\begingroup\$ @mazzy I will never in my life remember to filter a range. Thanks \$\endgroup\$
    – Veskah
    Nov 21, 2019 at 15:26
7
\$\begingroup\$

Python 3, 65 bytes

lambda s:print([''.join(map(chr,range(ord(c),96,-1)))for c in s])

Outputs a list of strings as clarified in @Arnauld's comment.

If we could assume that a string s existed with the content it would be 56 bytes.

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I didn't see this before I posted my very similar edit for a second method in my answer. You beat me by managing to lose the second map and keeping it looking nice :) +1 \$\endgroup\$
    – ElPedro
    Nov 21, 2019 at 17:27
  • 3
    \$\begingroup\$ btw, you can get it down to 58 as you don't need the print inside the lambda. It's allowed to just return the value then print it from outside. \$\endgroup\$
    – ElPedro
    Nov 21, 2019 at 17:35
7
\$\begingroup\$

Sed (with -r switch), 65 61 60 59 characters

s/./&zyxwvutsrqponmlkjihgfedcba \n/g
s/(.).*\1/\1/gm
s/ //g

Thanks to:

  • Kritixi Lithos for finding an unnecessary capturing (-3 characters)
  • Kritixi Lithos for anchoring to line start not anchoring instead of word start (-1 2 characters)
  • Kritixi Lithos for combining the 2 solutions advantages (2 characters)

Sample run:

bash-5.0$ sed -r 's/./&zyxwvutsrqponmlkjihgfedcba \n/g;s/(.).*\1/\1/gm;s/ //g' <<< 'cg cc'
cba
gfedcba

cba
cba

Try it online!

Sed (with -r switch), 53 52 characters

s/./&zyxwvutsrqponmlkjihgfedcba \n/g
s/(.).*\1/\1/gm

In case trailing spaces are acceptable.

Try it online!

\$\endgroup\$
6
  • \$\begingroup\$ The capturing group in the first substitution can be removed, and the second substitution can be shortened by a bit. Additionally you can find another way to treat spaces (note the rules allow printing a space instead of a blank line when the char is a space) to golf it some more. \$\endgroup\$
    – user41805
    Nov 21, 2019 at 18:10
  • \$\begingroup\$ Thank you, @KritixiLithos. I was so happy I got rid of initial 77 character version with looping, that I forgot to recheck some details. \$\endgroup\$
    – manatwork
    Nov 21, 2019 at 18:47
  • \$\begingroup\$ One more byte can be removed from the second substitution (60b), and you can use the approach by the 53-byte solution to get a valid solution shorter than the now 60 byte solution. \$\endgroup\$
    – user41805
    Nov 22, 2019 at 6:55
  • \$\begingroup\$ Thank you, @KritixiLithos. No I idea why I kept anchoring that expression. \$\endgroup\$
    – manatwork
    Nov 22, 2019 at 18:37
  • \$\begingroup\$ Maybe you missed the second part of my comment, you can have s/ // at the end of the 52b solution to get a valid 58b solution \$\endgroup\$
    – user41805
    Nov 23, 2019 at 16:09
6
\$\begingroup\$

JavaScript (Node.js), 56 bytes

Takes input as a list of characters. Returns a list of strings.

s=>s.map(g=c=>(c|=(B=Buffer)(c)[0])>96?B([c--])+g(c):'')

Try it online!

Commented

s =>                 // s[] = input characters
  s.map(g = c =>     // for each character c in s[], using the recursive function g:
    ( c |=           //   update c:
        (B = Buffer) //     on the first iteration, c is a character and Buffer(c)[0]
        (c)[0]       //     returns its ASCII code; on later iterations, c is an
                     //     integer and Buffer(c) creates a buffer filled with NUL bytes
    ) > 96 ?         //     if c is greater than 96:
      B([c--])       //       append the character of ASCII code c and decrement c
      + g(c)         //       append the result of a recursive call
    :                //     else:
      ''             //       stop recursion
  )                  // end of map()
\$\endgroup\$
6
\$\begingroup\$

R, 63 62 61 bytes

-16 bytes thanks to Giuseppe

-7 more bytes, again, thanks to Giuseppe

-1 byte adding a line break in place of \n

-1 byte thanks to Robin Ryder

for(l in utf8ToInt(scan(,""))-96)cat(letters[l:0],'
',sep="")

Try it online!

R, 86 bytes (my original answer)

x=utf8ToInt(scan(,''));s=sapply;cat(gsub(" .+"," ",s(s(x,`:`,97),intToUtf8)),sep="\n")

Ungolfed:

x=utf8ToInt(scan(,''))                                 #Takes input, converts to ASCII
s=sapply;                                              #Alias sapply function to save a byte
                     s(x,`:`,97)                       #Create vector from 97 to respective ASCII value
                   s(           ,intToUtf8)            #Convert to character
    gsub(" .+"," ",                        )           #Removes everything after space
cat(                                        ,sep="\n") #Outputs to console

Try it online!

Could likely be golfed.

NOTE: It does not use recursion. I do not know whether that would be shorter or not. I'll experiment later.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ 80 bytes \$\endgroup\$
    – Giuseppe
    Nov 21, 2019 at 19:37
  • \$\begingroup\$ Just when I felt confident I had found a solid answer, @Giuseppe comes back again with something better! As expected... \$\endgroup\$
    – Sumner18
    Nov 21, 2019 at 19:52
  • 2
    \$\begingroup\$ ooh, or 63 bytes -- I'm just very excited to get back into golfing!!!! \$\endgroup\$
    – Giuseppe
    Nov 21, 2019 at 20:36
  • 1
    \$\begingroup\$ Aha, I found 1 more byte! I don't feel as useless anymore. Haha \$\endgroup\$
    – Sumner18
    Nov 21, 2019 at 20:44
  • 1
    \$\begingroup\$ 61 bytes \$\endgroup\$ Nov 22, 2019 at 13:08
6
\$\begingroup\$

C (gcc), 56, 53 bytes

c;f(char*s){for(c=*s;c;)putchar(c<97?c=*++s,10:c--);}

Try it online!

Now down to 53 bytes! It can probably be golfed more though...

\$\endgroup\$
2
  • \$\begingroup\$ You can put your putchar call as the second statement of your for loop, since putchar returns the value of the character just written i.e. c. Saves you a grand total of one byte. \$\endgroup\$
    – Orion
    Nov 23, 2019 at 17:32
  • \$\begingroup\$ Thanks for the insight. I don't think it works though, since c=*++s,10 actually returns 10. Please correct me if I'm wrong though! \$\endgroup\$ Nov 23, 2019 at 20:00
5
\$\begingroup\$

Perl 6, 24 bytes

*.comb>>.&{[R~] 'a'..$_}

Try it online!

Returns a list of strings.

Explanation

*.comb  # Split into characters
      >>.&{            }  # Map to
                'a'..$_   # Range 'a' to current character
           [R~]           # Reverse concat
\$\endgroup\$
5
\$\begingroup\$

K (oK), 17 14 bytes

-3 bytes thanks to streetster

-3 bytes thanks to ngn

`c$96-!:'0&96-

Try it online!

\$\endgroup\$
6
  • 1
    \$\begingroup\$ You dont need to wrap it in a lambda; f:{`c$97+|!0|x-96}' \$\endgroup\$
    – mkst
    Nov 23, 2019 at 19:48
  • 1
    \$\begingroup\$ @streetster Yes, of course! Thank you! \$\endgroup\$ Nov 23, 2019 at 19:54
  • 1
    \$\begingroup\$ !x works for negative ints too, giving us a reverse for free: {`c$96-!0&96-x}' \$\endgroup\$
    – ngn
    Nov 24, 2019 at 15:22
  • 1
    \$\begingroup\$ without the lambda: `c$96-!:'0&96- \$\endgroup\$
    – ngn
    Nov 24, 2019 at 15:27
  • 1
    \$\begingroup\$ it depends on the version of k. afaik in k5 !-n was "permutations", in k6 (and ngn/k and oK) !-n was -n-!n, and in other versions it's an error. \$\endgroup\$
    – ngn
    Nov 24, 2019 at 18:45
5
\$\begingroup\$

Jelly,  8  7 bytes

A monadic link returning a list of strings.

ḲOr97ỌK

Try it online!

ḲOr97ỌK   - a monadic link taking a string, e.g. "ab c"
Ḳ         - split at spaces                 --> ["ab", "c"]
 O        - get ASCII codes                 --> [[97, 98], [99]]
  r97     - build descending range to 97    --> [[[97], [98, 97]], [[99, 98, 97]]]
     Ọ    - turn back into ASCII            --> [["a", "ba"], ["cba"]]
      K   - join with spaces                --> ["a", "ba", " ", "cba"]
\$\endgroup\$
1
  • 2
    \$\begingroup\$ You asked if we may output a list of lines - since we may you can safely remove the trailing Y (add ÇY to the footer to show it run as a full program) - I posted a different 7 too :) \$\endgroup\$ Nov 25, 2019 at 18:33
5
\$\begingroup\$

GolfScript, 12 bytes

{),97>-1%n}%

Try it online!

Explanation

{         }% # Map over the implicit input
 ),          # Generate range to 0x00
   97>       # Remove all that is less than 97
      -1%    # Reverse the string
         n   # Add a newline
\$\endgroup\$
5
\$\begingroup\$

05AB1E, 14 12 5 bytes

AAηí‡

-2 bytes by outputting as a list of strings.
-7 bytes (byte-count more than halved) thanks to @Grimy.

Input as a list of characters.

Try it online or verify all test cases.

Or alternatively:

AÂ.s‡

Try it online or verify all test cases.

Explanation:

A      # Push the lowercase alphabet
 Aη    # Push a list of prefixes of the lowercase alphabet
   í   # Reverse each prefix
    ‡  # Transliterate all characters of the alphabet to these reversed prefixes in
       # the (implicit) input-list
       # (after which the resulting string-list is output implicitly)

A      # Push the lowercase alphabet
 Â     # Bifurcate it; short for Duplicate & Reverse copy
  .s   # Get the suffixes of that reversed alphabet
    ‡  # Transliterate all characters of the alphabet to these reversed suffixes in
       # the (implicit) input-list
       # (after which the resulting string-list is output implicitly)
\$\endgroup\$
4
  • 2
    \$\begingroup\$ k followed by è is just the built-in. This yields a 6: εAAηí‡. And if we take input as a list of characters, we can omit the ε for a 5, though that might be pushing it, since output has to be a list of lines, not characters. \$\endgroup\$
    – Grimmy
    Nov 21, 2019 at 19:54
  • \$\begingroup\$ @Grimmy "k followed by è is just the built-in." I'm such an idiot.. And taking a string input as a list of characters is allowed by default, since in most languages a string simply is a CharacterSequence. So thanks for more than halving my byte-count.. :D \$\endgroup\$ Nov 21, 2019 at 22:24
  • \$\begingroup\$ Alternate 5-byter: AÂ.s‡ \$\endgroup\$
    – Grimmy
    Nov 29, 2019 at 13:09
  • 1
    \$\begingroup\$ I think you mean "suffixes" for .s? \$\endgroup\$
    – emanresu A
    Jun 13, 2023 at 20:18
4
\$\begingroup\$

Ruby, 41 characters

->s{s.chars{|c|puts [*?a..c].reverse*''}}

or

->s{s.gsub(/./){[*?a..$&].reverse*''+$/}}

No recursion here.

Sample run:

irb(main):001:0> ->s{s.chars{|c|puts [*?a..c].reverse*''}}['cgcc']
cba
gfedcba
cba
cba

Try it online!

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

PostgreSQL, 119 characters

\prompt s
select(select string_agg(chr(s),'')from generate_series(ascii(c),97,-1)s)from regexp_split_to_table(:'s','')c

(Those verbose function names are killing me…)

Sample run:

bash-5.0$ psql -Atf pointlessly-recurse.sql <<< 'hello world'
hgfedcba
edcba
lkjihgfedcba
lkjihgfedcba
onmlkjihgfedcba

wvutsrqponmlkjihgfedcba
onmlkjihgfedcba
rqponmlkjihgfedcba
lkjihgfedcba
dcba
\$\endgroup\$
4
\$\begingroup\$

V (vim), 15 bytes

\Óçá/A¬za
͈…±

Try it online!

Hexdump:

00000000: 5cd3 e7e1 2f41 ac7a 610d cd88 8185 b1    \.../A.za......

Fun fact: Recursion is the only form of looping that V has, yet this submission doesn't use any.

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

Befunge-98 (PyFunge), 18 bytes

#@~#;:'``j;:,1-;a,

Try it online!

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

PHP, 88 bytes

<?php
for(;$i<strlen($s=$argv[1]);){for($j=ord($s[$i++]);$j>96;)echo chr($j--);echo"
";}

Try it online!

Thanks to Shaggy for -21 bytes! :)

\$\endgroup\$
3
  • 4
    \$\begingroup\$ Welcome to PPCG. A few quick savings for you: tio.run/… \$\endgroup\$
    – Shaggy
    Nov 22, 2019 at 12:28
  • \$\begingroup\$ Missed a few, sorry: tio.run/##K8go@P/fxr4go4ArLb9Iw1olyza/… \$\endgroup\$
    – Shaggy
    Nov 22, 2019 at 22:45
  • \$\begingroup\$ Also, if you can figure out how to get shorttags working on TIO, you can save another 4. I can never remember! \$\endgroup\$
    – Shaggy
    Nov 22, 2019 at 22:45
4
\$\begingroup\$

Wren, 103 bytes

Hopefully this beats the Lua answer.

Fn.new{|x|
for(i in x)System.print(i==" "?"":(i.bytes[0]..97).map{|j|String.fromCodePoint(j)}.join())
}

Try it online!

Wren, 159 bytes

I am not having enough fun.

Fn.new{|x|
for(i in 122..97)x=x.replace(String.fromCodePoint(i),String.fromCodePoint(i)+String.fromCodePoint(i-1))
return x.replace("`","
").replace(" ","
")
}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Unfortunately, this fails the 'hello world' case. There's a special case for the space character, which should just step on to the next line. \$\endgroup\$
    – AJFaraday
    Nov 21, 2019 at 14:22
4
\$\begingroup\$

Jelly, 7 bytes

Øa>Ðḟ)U

A monadic link accepting a list of characters which yields a list of lists of characters.

Try it online! (adds newlines, since as a full program Jelly will print a smashed version of the lists)

How?

Øa>Ðḟ)U - Link: list of characters, S
     )  - for each (character, c, in S):
Øa      -   yield the lowercase alphabet ['a', 'b', ..., 'z']
   Ðḟ   -   filter discard those for which:
  >     -     greater than (c)?
      U - reverse each resulting list (each line)
\$\endgroup\$
4
\$\begingroup\$

Brainfuck, 240 chars

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

Try it online!

Explanation:

, takes an input for the length of the input for the loop

[>,[>+>+<<-]> takes an input and duplicates it

------------------------------------------------------------------------------------------------ takes 97 from the ascii value of the input to iterate over

[>.-<-] outputs the ascii character and then takes one away from the pointer, ready for the next loop

++++++++++.----------> outputs the newline

------------------------------------------------------------------------------------------------<<<-] resets everything ready for another iteration

\$\endgroup\$
3
  • \$\begingroup\$ Can be golfed down quite a bit by just using loops for the reset to 0 and subtracting a constant value. This also fixes the problem, that your answer ignores the first letter. 66 bytes But it also has the problem, that it fails when you put in a space (like your solution) \$\endgroup\$
    – david
    Dec 12, 2019 at 10:16
  • \$\begingroup\$ 89 bytes including the check for space. Other than that, basically the same solution you used. \$\endgroup\$
    – david
    Dec 12, 2019 at 11:02
  • \$\begingroup\$ Thanks @david, forgot about that! \$\endgroup\$ Dec 13, 2019 at 11:47
4
\$\begingroup\$

Lua, 94 bytes

function r(l)return l:find"[` ]"and"\n"or l..r(l.char(l:byte()-1))end
print((...):gsub(".",r))

Try it online!

Only works if we can have redundant output, else +1 byte.

Explanation

Creates a recursive function r that returns what each letter should turn into. When the input letter of this function is a space or the character before a (which is `) then this should not be printed, but a new line instead.

Replace each character in the input string (the first command-line argument) with the output of the function r called with this character.

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

Japt -mR, 6 bytes

Takes input as an array of characters.

;CÔk>U

Try it

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

SNOBOL4 (CSNOBOL4), 94 bytes

	I =INPUT
S	I LEN(1) . X REM . I	:F(END)
	Y =
	&LCASE ARB . Y X
	OUTPUT =X REVERSE(Y)	:(S)
END

Try it online!

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

Julia 1.0, 36 29 bytes

f(s)=[join(i:-1:'a') for i=s]

Try it online!

Note: Outputs as an array of strings.

To display in a nice output format, use, for example, println.(f("hello world"))

\$\endgroup\$
1
  • \$\begingroup\$ 26 bytes by using an anonymous function and broadcast over a generator: s->join.(i:-1:'a' for i=s) \$\endgroup\$
    – TimD
    Nov 22, 2019 at 7:21
3
\$\begingroup\$

Poetic, 482 bytes

transmit a letter or two,a-z
enter in letters from a-z+space
i did a print,i say a letter&go down
i am moving in a cycle
i get to letter a,and maybe to space
i then create lines as i output the ten,a ASCII N/L char
i figure x>y,and i do admire a way i check it
if indeed x>y,then i rerun;if not,i finish it to output N/L char
i write it out&i follow it up until no char i input is here
o,et cetera as i run
i do admit,i do run relatively longish
o,i say,i do loads of BS deriving it

Try it online!

Not sure why no one's posted a pure brainfuck solution yet, the brainfuck program I made this poem from amounts to only 108 bytes.

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

Brachylog, 13 bytes

ẹ{;Ẓ↔⟨a₁h⟩|}ᵐ

Try it online!

This feels... bad.

ẹ{         }ᵐ    For each element of the input,
      a₁         output the suffix of
   Ẓ             the lowercase alphabet reversed
  ; ↔⟨  h⟩       for which the first element is the input,
          |      or the input if there is no such suffix.

A 9-byte generator is possible if spaces are completely ignored: ∋;Ẓ↔⟨a₁h⟩. Accounting for spaces brings it up to the same byte count:

Brachylog, 13 bytes

∋{;Ẓ↔⟨a₁h⟩!|}

Try it online!

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

Scala, 53 bytes

def f(s:String)=s.map(c=>('a'to c).reverse.mkString) 

Try it online!

Note: return the list of strings. To do the actual output, use .map(println) or so

\$\endgroup\$
1
  • \$\begingroup\$ Save 10 bytes by using lambdas: (_: String).map('a'.to(_).reverse.mkString) Try it online! \$\endgroup\$
    – Aly
    Nov 23, 2019 at 19:12

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.