40
\$\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
| improve this question | | | | |
\$\endgroup\$
  • 2
    \$\begingroup\$ May we output a list of strings instead of a single string with linefeeds? \$\endgroup\$ – Arnauld Nov 21 '19 at 13:10
  • 12
    \$\begingroup\$ I don't think a single answer uses recursion \$\endgroup\$ – Jo King Nov 22 '19 at 1:39
  • 3
    \$\begingroup\$ “Human readable” prohibits most of the languages popular here! \$\endgroup\$ – WGroleau Nov 22 '19 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 '19 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\$ – Tomáš Zato - Reinstate Monica Nov 22 '19 at 11:41

64 Answers 64

32
\$\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
| improve this answer | | | | |
\$\endgroup\$
  • 31
    \$\begingroup\$ We have peaked as a species. \$\endgroup\$ – TRose Nov 21 '19 at 22:16
  • 3
    \$\begingroup\$ Answers in obscure languages usually make the language name a link to documentation. Could you add that? \$\endgroup\$ – Justin Nov 22 '19 at 1:01
  • 2
    \$\begingroup\$ @JustinLardinois I don't think APL is exactly an obscure language, but here's the link: APL - Wikipedia. \$\endgroup\$ – Trebor Nov 22 '19 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 '19 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\$ – Justin Nov 22 '19 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.

| improve this answer | | | | |
\$\endgroup\$
  • 2
    \$\begingroup\$ 24 with map(\i->reverse['a'..i]) \$\endgroup\$ – FrownyFrog Nov 21 '19 at 23:04
  • 2
    \$\begingroup\$ Those darn enumFromThenTo functions and their ungolfiness. \$\endgroup\$ – totallyhuman Nov 21 '19 at 23:31
  • 2
    \$\begingroup\$ I want to say map(\i->[i,pred i..'a']) but it's the same size. \$\endgroup\$ – Joseph Sible-Reinstate Monica Nov 22 '19 at 2:24
  • 1
    \$\begingroup\$ I want to say map$ \i->reverse['a'..i] but it's the same size, too. \$\endgroup\$ – Andrew Ray Nov 26 '19 at 18:03
12
\$\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!

| improve this answer | | | | |
\$\endgroup\$
  • \$\begingroup\$ I believe the putchar call can be used in the while condition to save a byte. \$\endgroup\$ – user41805 Nov 22 '19 at 10:24
  • \$\begingroup\$ @KritixiLithos Thanks! This saves 2 bytes, although I suspect there's a way to save more. \$\endgroup\$ – Arnauld Nov 22 '19 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\$ – John Hamilton Nov 22 '19 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\$ – Kevin Cruijssen Nov 22 '19 at 11:03
  • \$\begingroup\$ @KevinCruijssen Yeah, you're right. It could create problems with the null character. Tab delimiter works better. \$\endgroup\$ – John Hamilton Nov 22 '19 at 11:05
9
\$\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!

| improve this answer | | | | |
\$\endgroup\$
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.

| improve this answer | | | | |
\$\endgroup\$
  • 2
    \$\begingroup\$ you can save some bytes Try it online! \$\endgroup\$ – mazzy Nov 21 '19 at 15:17
  • 1
    \$\begingroup\$ @mazzy I will never in my life remember to filter a range. Thanks \$\endgroup\$ – Veskah Nov 21 '19 at 15:26
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()
| improve this answer | | | | |
\$\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.

| improve this answer | | | | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 80 bytes \$\endgroup\$ – Giuseppe Nov 21 '19 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 '19 at 19:52
  • 2
    \$\begingroup\$ ooh, or 63 bytes -- I'm just very excited to get back into golfing!!!! \$\endgroup\$ – Giuseppe Nov 21 '19 at 20:36
  • 1
    \$\begingroup\$ Aha, I found 1 more byte! I don't feel as useless anymore. Haha \$\endgroup\$ – Sumner18 Nov 21 '19 at 20:44
  • 1
    \$\begingroup\$ 61 bytes \$\endgroup\$ – Robin Ryder Nov 22 '19 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...

| improve this answer | | | | |
\$\endgroup\$
  • \$\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 '19 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\$ – dingledooper Nov 23 '19 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
| improve this answer | | | | |
\$\endgroup\$
5
\$\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!

| improve this answer | | | | |
\$\endgroup\$
  • 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 '19 at 17:27
  • 2
    \$\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 '19 at 17:35
5
\$\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!

| improve this answer | | | | |
\$\endgroup\$
  • \$\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 '19 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 '19 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 '19 at 6:55
  • \$\begingroup\$ Thank you, @KritixiLithos. No I idea why I kept anchoring that expression. \$\endgroup\$ – manatwork Nov 22 '19 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 '19 at 16:09
5
\$\begingroup\$

K (oK), 17 14 bytes

-3 bytes thanks to streetster

-3 bytes thanks to ngn

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

Try it online!

| improve this answer | | | | |
\$\endgroup\$
  • 1
    \$\begingroup\$ You dont need to wrap it in a lambda; f:{`c$97+|!0|x-96}' \$\endgroup\$ – streetster Nov 23 '19 at 19:48
  • 1
    \$\begingroup\$ @streetster Yes, of course! Thank you! \$\endgroup\$ – Galen Ivanov Nov 23 '19 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 '19 at 15:22
  • 1
    \$\begingroup\$ without the lambda: `c$96-!:'0&96- \$\endgroup\$ – ngn Nov 24 '19 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 '19 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"]
| improve this answer | | | | |
\$\endgroup\$
  • 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\$ – Jonathan Allan Nov 25 '19 at 18:33
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!

| improve this answer | | | | |
\$\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
| improve this answer | | | | |
\$\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.

| improve this answer | | | | |
\$\endgroup\$
4
\$\begingroup\$

Befunge-98 (PyFunge), 18 bytes

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

Try it online!

| improve this answer | | | | |
\$\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! :)

| improve this answer | | | | |
\$\endgroup\$
  • 4
    \$\begingroup\$ Welcome to PPCG. A few quick savings for you: tio.run/… \$\endgroup\$ – Shaggy Nov 22 '19 at 12:28
  • \$\begingroup\$ Missed a few, sorry: tio.run/##K8go@P/fxr4go4ArLb9Iw1olyza/… \$\endgroup\$ – Shaggy Nov 22 '19 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 '19 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!

| improve this answer | | | | |
\$\endgroup\$
  • \$\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 '19 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)
| improve this answer | | | | |
\$\endgroup\$
4
\$\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 prefixes of that reversed alphabet
    ‡  # Transliterate all characters of the alphabet to these reversed prefixes in
       # the (implicit) input-list
       # (after which the resulting string-list is output implicitly)
| improve this answer | | | | |
\$\endgroup\$
  • 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 '19 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\$ – Kevin Cruijssen Nov 21 '19 at 22:24
  • \$\begingroup\$ Alternate 5-byter: AÂ.s‡ \$\endgroup\$ – Grimmy Nov 29 '19 at 13:09
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

| improve this answer | | | | |
\$\endgroup\$
  • \$\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 '19 at 10:16
  • \$\begingroup\$ 89 bytes including the check for space. Other than that, basically the same solution you used. \$\endgroup\$ – david Dec 12 '19 at 11:02
  • \$\begingroup\$ Thanks @david, forgot about that! \$\endgroup\$ – brainfuck best language Dec 13 '19 at 11:47
3
\$\begingroup\$

Japt -mR, 6 bytes

Takes input as an array of characters.

;CÔk>U

Try it

| improve this answer | | | | |
\$\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!

| improve this answer | | | | |
\$\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"))

| improve this answer | | | | |
\$\endgroup\$
  • \$\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 '19 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.

| improve this answer | | | | |
\$\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!

| improve this answer | | | | |
\$\endgroup\$
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

| improve this answer | | | | |
\$\endgroup\$
  • \$\begingroup\$ Save 10 bytes by using lambdas: (_: String).map('a'.to(_).reverse.mkString) Try it online! \$\endgroup\$ – Soren Nov 23 '19 at 19:12
3
\$\begingroup\$

IBM/Lotus Notes Formula, 116 bytes

a:="zyxwvutsrqponmlkjihgfedcba";p:="";@For(x:=1;x<=@Length(i);x:=x+1;c:=@Right(@Left(i;x);1);p:=p:(c+@Right(a;c)));p

I have never found a practical use for @For except when golfing.

enter image description here

| improve this answer | | | | |
\$\endgroup\$
3
\$\begingroup\$

Bash, 73 63 57 bytes

while read -n1 a;do [ - $a]&&echo||eval echo {$a..a};done

Try it online!

-6 bytes thanks to Kritixi Lithos

| improve this answer | | | | |
\$\endgroup\$
  • 1
    \$\begingroup\$ You can skip out on fold with read -n1 a. Also apparently [ = $a] can replace [ -z $a] \$\endgroup\$ – user41805 Nov 22 '19 at 10:26
  • \$\begingroup\$ Beautiful. Tyvm. \$\endgroup\$ – Jonah Nov 22 '19 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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