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
x86-16 machine code, IBM PC DOS, (削除) 27 (削除ここまで) 23 bytes
Binary:
00000000: b408 cd21 b40e cd10 483c 617d f9b0 0acd ...!....H<a}....
00000010: 10b0 0dcd 10eb e9 .......
Test and build using xxd -r from above.
Unassembled:
IN_LOOP:
B4 08 MOV AH, 08H ; DOS read char without echo function
CD 21 INT 21H ; load next char from STDIN
B4 0E MOV AH, 0EH ; BIOS write char teletype output function
CHR_LOOP:
CD 10 INT 10H ; write to console
48 DEC AX ; move to next char
3C 61 CMP AL, 'a' ; end with an 'a'
7D F9 JGE CHR_LOOP ; keep looping descending chars
B0 0A MOV AL, 0AH ; CR char
CD 10 INT 10H ; write to console
B0 0D MOV AL, 0DH ; LF char
CD 10 INT 10H ; write to console
EB E9 JMP IN_LOOP ; keep looping until ^C/^Break
I/O:
Input via STDIN or interactive input, output to console. Will run until EOF or ^C/^Break.
Iterative approach. Of course, it's 8 bytes just to write a newline character...
Python 3, (削除) 76 (削除ここまで) (削除) 60 (削除ここまで) 53 bytes
(thanks to @Jo King & @sporeball)
for a in input():print(*map(chr,range(ord(a),96,-1)))
-
1
-
1
-
\$\begingroup\$ 7 bytes by outputting 'list of strings instead of single string with linefeeds' \$\endgroup\$Dominic van Essen– Dominic van Essen2020年10月19日 10:06:41 +00:00Commented Oct 19, 2020 at 10:06
Vyxal jr, 6 bytes
ƛkzẆḢṅ
Flags are unintentional. -5 thanks to tybocopperkettle.
ƛ # For each character
Ẇ # Split, keeping the delimeter
kz # The lowercase alphabet, backwards
Ẇ # On that character
Ḣ # Remove the first string (all letters before that character)
ṅ # Concatenate the remaining strings.
-
\$\begingroup\$ 10 bytes using a different approach. Also your code seems to error, but it does work on vyxal.pythonanywhere.com. \$\endgroup\$tybocopperkettle– tybocopperkettle2023年06月13日 09:05:42 +00:00Commented Jun 13, 2023 at 9:05
-
\$\begingroup\$ 8 bytes with the
rflag \$\endgroup\$tybocopperkettle– tybocopperkettle2023年06月13日 09:24:42 +00:00Commented Jun 13, 2023 at 9:24
Fig, \16ドル\log_{256}(96)\approx\$ 13.17 bytes
M'+[\/ +x]\xczcn
I haven't done any Fig (or any code golf for that matter) in ages so this can probably be golfed a lot...
Explanation:
M # Map over each character in the input
' # a function from the following code:
\ # split
cz # the reversed alphabet
x # on the current character,
] # get the last item from that list (e.g. "gfedcba" for "h"),
+ # add that to
x # the current character, (we now have "hgfedcba")
\ # split that on
/ # spaces, (if the char is a space we need to remove the whole reversed alphabet which would've just been appended to it)
[ # get the first item from that,
+ # append
cn # a newline.
# Implicitly print joined by nothing.
-
1\$\begingroup\$ I'm afraid you missed a small detail: the sequences of letters have to be in descending order — from the letter used in input down to letter "a". \$\endgroup\$manatwork– manatwork2019年11月24日 14:09:30 +00:00Commented Nov 24, 2019 at 14:09
Pushy, 22 bytes
N@$'&32!?&97-:t';;T'..
N@$'&32!?&97-:t';;T'.. N \ Set output delimiter to empty string @ \ Reverse input $ \ While there are characters left on stack: ' \ Print the top character &32!? ; \ if not a space: &97-: ; \ char(top) - 97 times do: t' \ Decrement character and print T' \ Print a newline .. \ Clear up stack (pops items we are done with)
Standard ML (MLton), 62 bytes
fun$c=if#"a">c then""else str c^ $(Char.pred c);map$o explode;
Defines an anonymous function which is implicitly bound to it, so use as e.g. it "hello world": Try it online! Based on Thoma's SML answer.
Explanation
fun down c = if #"a" > c then "" else str c ^ t(Char.pred c)
val it = map down o explode
The function down takes a character c and recursively (!) builds the string of previous letters. The point-free function bound to it first explodes a given string into a list of characters and applies down to each of them.
PowerShell, 47 bytes
$args|% T*y|%{($_,(-join($_..'a')))[!!($_%32)]}
PowerShell 7, 38 bytes
$args|% T*y|%{$_%32?-join($_..'a'):$_}
No TIO link because TIO does not support PS7 yet.
PowerShell (Splatted Input), 41 bytes
Takes the input as an array of characters; not sure if this is legal for this challenge, so I've broken it into a separate section.
$args|%{($_,(-join($_..'a')))[!!($_%32)]}
PowerShell 7 (Splatted Input), 32 bytes
Takes the input as an array of characters; not sure if this is legal for this challenge, so I've broken it into a separate section.
$args|%{$_%32?-join($_..'a'):$_}
No TIO link because TIO does not support PS7 yet.
-
1\$\begingroup\$ Try it online ? \$\endgroup\$mazzy– mazzy2021年02月18日 05:11:49 +00:00Commented Feb 18, 2021 at 5:11
Thunno 2, 4 bytes
ẠḲṣṆ
Port of Kevin Cruijssen's (bottom) 05AB1E answer.
Thunno 2, 5 bytes
ẠDƒṃṆ
Port of Kevin Cruijssen's (top) 05AB1E answer.
Explanation
ẠḲṣṆ # Implicit input
Ạ # Lowercase alphabet
Ḳ # Bifurcate
ṣ # Suffixes
Ṇ # Transliterate
# Implicit output
ẠDƒṃṆ # Implicit input
Ạ # Lowercase alphabet
Dƒ # Duplicate and get prefixes
ṃ # Reverse each item
Ṇ # Transliterate
# Implicit output
Python, 73 Bytes
lambda x:'\n'.join(''.join(chr(y)for y in range(ord(z),96,-1))for z in x)
Anonymous lambda function that returns a string. If you want one that works for only capital letters, you only need to change 96 to 64.