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
\$\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

1 2
3
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)

| improve this answer | |
\$\endgroup\$
  • 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\$ – Jo King Jan 15 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\$ – sporeball Jan 18 at 21:26
1
\$\begingroup\$

Ruby, 40 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
  • 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 Nov 24 '19 at 14:09
1
\$\begingroup\$

TCL, 179 bytes

rename string d;proc a s {set b zyxwvutsrqponmlkjihgfedcba;if {$s=={}} return;if {[set c [d first [d index $s 0] $b]]<0} {puts {}} {puts [d range $b $c end]};a [d replace $s 0 0]}

Try it online!

ungolfed version

proc a {s} {
    set b zyxwvutsrqponmlkjihgfedcba
    if {$s=={}} return


    if {[set c [string first [string index $s 0] $b]]<0} then {
            puts {} # if it's a space print a newline
    } else {
        puts [string range $b $c end] # print the substring of $b starting at the character
    }

    #recurse on the string minus the front character
    a [string replace $s 0 0]
}

a [concat $argv]
| improve this answer | |
\$\endgroup\$
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)  
| improve this answer | |
\$\endgroup\$
1 2
3

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.