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
asked Nov 21, 2019 at 12:25
\$\endgroup\$
14
  • 2
    \$\begingroup\$ May we output a list of strings instead of a single string with linefeeds? \$\endgroup\$ Commented Nov 21, 2019 at 13:10
  • 16
    \$\begingroup\$ I don't think a single answer uses recursion \$\endgroup\$ Commented Nov 22, 2019 at 1:39
  • 3
    \$\begingroup\$ "Human readable" prohibits most of the languages popular here! \$\endgroup\$ Commented 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\$ Commented 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\$ Commented Nov 22, 2019 at 11:41

76 Answers 76

1 2
3
2
\$\begingroup\$

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.

enter image description here

Iterative approach. Of course, it's 8 bytes just to write a newline character...

answered Jan 15, 2020 at 22:47
\$\endgroup\$
2
\$\begingroup\$

Python 3, (削除) 76 (削除ここまで) (削除) 60 (削除ここまで) 53 bytes

(thanks to @Jo King & @sporeball)

for a in input():print(*map(chr,range(ord(a),96,-1)))

Try it online! (original 76 bytes solution)

Try it online! (53 bytes solution)

answered Jan 15, 2020 at 22:04
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can move the second for loop inside the print to save on using the second print, 68 bytes. And then map for even shorter, 60 bytes \$\endgroup\$ Commented Jan 15, 2020 at 22:36
  • 1
    \$\begingroup\$ Having spaces between letters is allowed, so if you omit sep, you can get it down to 53 bytes. \$\endgroup\$ Commented Jan 18, 2020 at 21:26
2
\$\begingroup\$

Husk, (削除) 8 (削除ここまで) 6 bytes

-2 bytes thanks to Dominic van Essen!

†`...'aw

Try it online!

answered Oct 18, 2020 at 23:23
\$\endgroup\$
1
  • \$\begingroup\$ 7 bytes by outputting 'list of strings instead of single string with linefeeds' \$\endgroup\$ Commented Oct 19, 2020 at 10:06
2
\$\begingroup\$

Factor, 35 bytes

[ [ dup 97 min [a,b] print ] each ]

Pointlessly indeed.

Attempt This Online!

answered Jun 16, 2022 at 10:29
\$\endgroup\$
2
\$\begingroup\$

Arturo, 27 bytes

$=>[map&'c->@c..min@[c`a`]]

Try it!

Outputs a list of lists of characters.

answered Jun 13, 2023 at 2:21
\$\endgroup\$
2
\$\begingroup\$

Vyxal jr, 6 bytes

ƛkzẆḢṅ

Try it Online!

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.
answered May 22, 2021 at 3:03
\$\endgroup\$
2
  • \$\begingroup\$ 10 bytes using a different approach. Also your code seems to error, but it does work on vyxal.pythonanywhere.com. \$\endgroup\$ Commented Jun 13, 2023 at 9:05
  • \$\begingroup\$ 8 bytes with the r flag \$\endgroup\$ Commented Jun 13, 2023 at 9:24
2
\$\begingroup\$

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...

Try it online!

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.
answered Jun 13, 2023 at 22:20
\$\endgroup\$
1
\$\begingroup\$

Ruby, 40 bytes

ARGV[0].chars.map{|c|print *'a'..c;puts}

Try it online!

answered Nov 24, 2019 at 2:36
\$\endgroup\$
1
  • 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\$ Commented Nov 24, 2019 at 14:09
1
\$\begingroup\$

Pushy, 22 bytes

N@$'&32!?&97-:t';;T'..

Try it online!

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) 
answered Dec 15, 2019 at 20:07
\$\endgroup\$
1
\$\begingroup\$

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.

answered Feb 17, 2021 at 13:12
\$\endgroup\$
0
1
\$\begingroup\$

PowerShell, 47 bytes

$args|% T*y|%{($_,(-join($_..'a')))[!!($_%32)]}

Try it online!

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)]}

Try it online!

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.

answered Feb 17, 2021 at 14:07
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Try it online ? \$\endgroup\$ Commented Feb 18, 2021 at 5:11
1
\$\begingroup\$

Zsh, 49 bytes

>\ 
for c (${(s"")1})<$c||printf %s {$c..a}&&echo

Attempt This Online!

answered Jun 16, 2022 at 7:15
\$\endgroup\$
1
\$\begingroup\$

Thunno 2, 4 bytes

ẠḲṣṆ

Attempt This Online!

Port of Kevin Cruijssen's (bottom) 05AB1E answer.

Thunno 2, 5 bytes

ẠDƒṃṆ

Attempt This Online!

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
answered Jun 11, 2023 at 13:47
\$\endgroup\$
1
\$\begingroup\$

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.

tsrqponmlkjihgfedcbarqponmlkjihgfedcbayxwvutsrqponmlkjihgfedcbaihgfedcbatsrqponmlkjihgfedcbaonmlkjihgfedcbanmlkjihgfedcbalkjihgfedcbaihgfedcbanmlkjihgfedcbaedcba

answered Jun 11, 2023 at 14:08
\$\endgroup\$
1
\$\begingroup\$

Ruby -n, 35 bytes

$_.chars{puts [*?a.._1].reverse*""}

Attempt This Online!

answered Jun 11, 2023 at 19:17
\$\endgroup\$
1
\$\begingroup\$

Rockstar, 111 bytes

listen to S
cut S
while S
cast roll S in C with 36
O's ""
while C-9
cast C+87 in N
let O be+N
let C be-1
say O

Try it (Code will need to be pasted in)

answered Jun 12, 2023 at 10:05
\$\endgroup\$
1 2
3

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.