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
-
2\$\begingroup\$ May we output a list of strings instead of a single string with linefeeds? \$\endgroup\$Arnauld– Arnauld2019年11月21日 13:10:17 +00:00Commented Nov 21, 2019 at 13:10
-
16\$\begingroup\$ I don't think a single answer uses recursion \$\endgroup\$Jo King– Jo King2019年11月22日 01:39:25 +00:00Commented Nov 22, 2019 at 1:39
-
3\$\begingroup\$ "Human readable" prohibits most of the languages popular here! \$\endgroup\$WGroleau– WGroleau2019年11月22日 02:50:10 +00:00Commented 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– AJFaraday2019年11月22日 09:13:31 +00:00Commented 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\$Tomáš Zato– Tomáš Zato2019年11月22日 11:41:50 +00:00Commented Nov 22, 2019 at 11:41
76 Answers 76
Dyalog APL Extended, 4 bytes
⌽∘⍳ ̈
∘ the function composition of
⍳ iota - 'a'..input for alphabet chars, empty array for space
⌽ and after, reverse
̈ applied to each
-
34\$\begingroup\$ We have peaked as a species. \$\endgroup\$TRose– TRose2019年11月21日 22:16:51 +00:00Commented 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\$user2752467– user27524672019年11月22日 01:01:12 +00:00Commented 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– Trebor2019年11月22日 11:51:32 +00:00Commented 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– dzaima2019年11月22日 11:58:51 +00:00Commented 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\$user2752467– user27524672019年11月22日 22:17:17 +00:00Commented Nov 22, 2019 at 22:17
Haskell, (削除) 26 (削除ここまで) 24 bytes
-2 bytes thanks to FrownyFrog.
map(\i->reverse['a'..i])
Pointless (or pointfree :P) for you.
-
2\$\begingroup\$ 24 with
map(\i->reverse['a'..i])\$\endgroup\$FrownyFrog– FrownyFrog2019年11月21日 23:04:02 +00:00Commented Nov 21, 2019 at 23:04 -
2\$\begingroup\$ Those darn
enumFromThenTofunctions and their ungolfiness. \$\endgroup\$totallyhuman– totallyhuman2019年11月21日 23:31:14 +00:00Commented 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\$Joseph Sible-Reinstate Monica– Joseph Sible-Reinstate Monica2019年11月22日 02:24:54 +00:00Commented 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\$OpenAI was the last straw– OpenAI was the last straw2019年11月26日 18:03:30 +00:00Commented Nov 26, 2019 at 18:03
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++);}
-
\$\begingroup\$ I believe the
putcharcall can be used in the while condition to save a byte. \$\endgroup\$user41805– user418052019年11月22日 10:24:34 +00:00Commented 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– Arnauld2019年11月22日 10:52:35 +00:00Commented 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\$John Hamilton– John Hamilton2019年11月22日 10:56:43 +00:00Commented 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.9instead of10can be used with tab-delimiters though, for the same -2. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2019年11月22日 11:03:20 +00:00Commented 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\$John Hamilton– John Hamilton2019年11月22日 11:05:09 +00:00Commented Nov 22, 2019 at 11:05
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,[%\3円dL+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!
-
\$\begingroup\$ Is MBASM what you use to assemble Malbolge programs? \$\endgroup\$user92753– user927532022年02月08日 20:36:13 +00:00Commented Feb 8, 2022 at 20:36
PowerShell, (削除) 37 (削除ここまで) 28 bytes
-9 bytes thanks to mazzy
$args|%{-join($_..'a'-le$_)}
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.
-
2\$\begingroup\$ you can save some bytes Try it online! \$\endgroup\$mazzy– mazzy2019年11月21日 15:17:25 +00:00Commented Nov 21, 2019 at 15:17
-
1\$\begingroup\$ @mazzy I will never in my life remember to filter a range. Thanks \$\endgroup\$Veskah– Veskah2019年11月21日 15:26:16 +00:00Commented Nov 21, 2019 at 15:26
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.
-
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
mapand keeping it looking nice :) +1 \$\endgroup\$ElPedro– ElPedro2019年11月21日 17:27:49 +00:00Commented Nov 21, 2019 at 17:27 -
3
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
Sed (with -r switch), (削除) 53 (削除ここまで) 52 characters
s/./&zyxwvutsrqponmlkjihgfedcba \n/g
s/(.).*1円/1円/gm
In case trailing spaces are acceptable.
-
\$\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– user418052019年11月21日 18:10:55 +00:00Commented 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– manatwork2019年11月21日 18:47:29 +00:00Commented 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– user418052019年11月22日 06:55:47 +00:00Commented Nov 22, 2019 at 6:55
-
\$\begingroup\$ Thank you, @KritixiLithos. No I idea why I kept anchoring that expression. \$\endgroup\$manatwork– manatwork2019年11月22日 18:37:40 +00:00Commented 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– user418052019年11月23日 16:09:40 +00:00Commented Nov 23, 2019 at 16:09
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):'')
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()
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="")
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
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.
-
1
-
\$\begingroup\$ Just when I felt confident I had found a solid answer, @Giuseppe comes back again with something better! As expected... \$\endgroup\$Sumner18– Sumner182019年11月21日 19:52:02 +00:00Commented Nov 21, 2019 at 19:52
-
2
-
1\$\begingroup\$ Aha, I found 1 more byte! I don't feel as useless anymore. Haha \$\endgroup\$Sumner18– Sumner182019年11月21日 20:44:53 +00:00Commented Nov 21, 2019 at 20:44
-
1\$\begingroup\$ 61 bytes \$\endgroup\$Robin Ryder– Robin Ryder2019年11月22日 13:08:58 +00:00Commented Nov 22, 2019 at 13:08
C (gcc), (削除) 56 (削除ここまで), 53 bytes
c;f(char*s){for(c=*s;c;)putchar(c<97?c=*++s,10:c--);}
Now down to 53 bytes! It can probably be golfed more though...
-
\$\begingroup\$ You can put your
putcharcall as the second statement of yourforloop, sinceputcharreturns the value of the character just written i.e.c. Saves you a grand total of one byte. \$\endgroup\$Orion– Orion2019年11月23日 17:32:12 +00:00Commented Nov 23, 2019 at 17:32 -
\$\begingroup\$ Thanks for the insight. I don't think it works though, since
c=*++s,10actually returns 10. Please correct me if I'm wrong though! \$\endgroup\$dingledooper– dingledooper2019年11月23日 20:00:31 +00:00Commented Nov 23, 2019 at 20:00
Perl 6, 24 bytes
*.comb>>.&{[R~] 'a'..$_}
Returns a list of strings.
Explanation
*.comb # Split into characters
>>.&{ } # Map to
'a'..$_ # Range 'a' to current character
[R~] # Reverse concat
K (oK), (削除) 17 (削除ここまで) 14 bytes
-3 bytes thanks to streetster
-3 bytes thanks to ngn
`c96ドル-!:'0&96-
-
1\$\begingroup\$ You dont need to wrap it in a lambda;
f:{`c97ドル+|!0|x-96}'\$\endgroup\$mkst– mkst2019年11月23日 19:48:42 +00:00Commented Nov 23, 2019 at 19:48 -
1\$\begingroup\$ @streetster Yes, of course! Thank you! \$\endgroup\$Galen Ivanov– Galen Ivanov2019年11月23日 19:54:10 +00:00Commented Nov 23, 2019 at 19:54
-
1\$\begingroup\$
!xworks for negative ints too, giving us a reverse for free:{`c96ドル-!0&96-x}'\$\endgroup\$ngn– ngn2019年11月24日 15:22:22 +00:00Commented Nov 24, 2019 at 15:22 -
1\$\begingroup\$ without the lambda:
`c96ドル-!:'0&96-\$\endgroup\$ngn– ngn2019年11月24日 15:27:15 +00:00Commented Nov 24, 2019 at 15:27 -
1\$\begingroup\$ it depends on the version of k. afaik in k5
!-nwas "permutations", in k6 (and ngn/k and oK)!-nwas-n-!n, and in other versions it's an error. \$\endgroup\$ngn– ngn2019年11月24日 18:45:31 +00:00Commented Nov 24, 2019 at 18:45
Jelly, (削除) 8 (削除ここまで) 7 bytes
A monadic link returning a list of strings.
ḲOr97ỌK
Ḳ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"]
-
2\$\begingroup\$ You asked if we may output a list of lines - since we may you can safely remove the trailing
Y(addÇYto the footer to show it run as a full program) - I posted a different 7 too :) \$\endgroup\$Jonathan Allan– Jonathan Allan2019年11月25日 18:33:18 +00:00Commented Nov 25, 2019 at 18:33
GolfScript, 12 bytes
{),97>-1%n}%
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
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)
-
2\$\begingroup\$
kfollowed 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– Grimmy2019年11月21日 19:54:34 +00:00Commented Nov 21, 2019 at 19:54 -
\$\begingroup\$ @Grimmy "
kfollowed 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– Kevin Cruijssen2019年11月21日 22:24:52 +00:00Commented Nov 21, 2019 at 22:24 -
\$\begingroup\$ Alternate 5-byter:
AÂ.s‡\$\endgroup\$Grimmy– Grimmy2019年11月29日 13:09:11 +00:00Commented Nov 29, 2019 at 13:09 -
1\$\begingroup\$ I think you mean "suffixes" for
.s? \$\endgroup\$emanresu A– emanresu A2023年06月13日 20:18:17 +00:00Commented Jun 13, 2023 at 20:18
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
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
V (vim), 15 bytes
\Óçá/A¬za
Í
±
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.
PHP, 88 bytes
<?php
for(;$i<strlen($s=$argv[1]);){for($j=ord($s[$i++]);$j>96;)echo chr($j--);echo"
";}
Thanks to Shaggy for -21 bytes! :)
-
4
-
\$\begingroup\$ Missed a few, sorry: tio.run/##K8go@P/fxr4go4ArLb9Iw1olyza/… \$\endgroup\$Shaggy– Shaggy2019年11月22日 22:45:06 +00:00Commented 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– Shaggy2019年11月22日 22:45:48 +00:00Commented Nov 22, 2019 at 22:45
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())
}
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(" ","
")
}
-
\$\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– AJFaraday2019年11月21日 14:22:18 +00:00Commented Nov 21, 2019 at 14:22
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)
Brainfuck, 240 chars
,[>,[>+>+<<-]>------------------------------------------------------------------------------------------------[>.-<-]++++++++++.---------->------------------------------------------------------------------------------------------------<<<-]
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
-
\$\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– david2019年12月12日 10:16:39 +00:00Commented Dec 12, 2019 at 10:16
-
-
\$\begingroup\$ Thanks @david, forgot about that! \$\endgroup\$brainfuck best language– brainfuck best language2019年12月13日 11:47:18 +00:00Commented Dec 13, 2019 at 11:47
Lua, 94 bytes
function r(l)return l:find"[` ]"and"\n"or l..r(l.char(l:byte()-1))end
print((...):gsub(".",r))
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.
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
Julia 1.0, (削除) 36 (削除ここまで) 29 bytes
f(s)=[join(i:-1:'a') for i=s]
Note: Outputs as an array of strings.
To display in a nice output format, use, for example, println.(f("hello world"))
-
\$\begingroup\$ 26 bytes by using an anonymous function and broadcast over a generator:
s->join.(i:-1:'a' for i=s)\$\endgroup\$TimD– TimD2019年11月22日 07:21:12 +00:00Commented Nov 22, 2019 at 7:21
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
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.
Brachylog, 13 bytes
ẹ{;Ẓ↔⟨a1h⟩|}m
This feels... bad.
ẹ{ }m For each element of the input,
a1 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: ∋;Ẓ↔⟨a1h⟩. Accounting for spaces brings it up to the same byte count:
Brachylog, 13 bytes
∋{;Ẓ↔⟨a1h⟩!|}
-
\$\begingroup\$ Without handling spaces, a 9 byte generator is possible:
∋;Ẓ↔⟨a1h⟩\$\endgroup\$Unrelated String– Unrelated String2019年11月22日 01:45:35 +00:00Commented Nov 22, 2019 at 1:45
Scala, 53 bytes
def f(s:String)=s.map(c=>('a'to c).reverse.mkString)
Note: return the list of strings. To do the actual output, use .map(println) or so
-
\$\begingroup\$ Save 10 bytes by using lambdas:
(_: String).map('a'.to(_).reverse.mkString)Try it online! \$\endgroup\$Aly– Aly2019年11月23日 19:12:20 +00:00Commented Nov 23, 2019 at 19:12