43
\$\begingroup\$

Description

Output the rhyme scheme for a very long Terza Rima.

Input

None.

Output

ABA
BCB
CDC
DED
EFE
FGF
GHG
HIH
IJI
JKJ
KLK
LML
MNM
NON
OPO
PQP
QRQ
RSR
STS
TUT
UVU
VWV
WXW
XYX
YZY

Rules

You can pick between separating stanzas with whitespace or newlines, so either:

ABA BCB...

OR

ABA
BCB
...

A single trailing whitespace allowed per line and one trailing newline allowed.

Output can be either uppercase or lowercase.

This is , so the shortest code in bytes for each language wins.

\$\endgroup\$
9
  • 4
    \$\begingroup\$ Is a list of lines okay? \$\endgroup\$ Dec 18, 2017 at 16:23
  • 6
    \$\begingroup\$ According to en.wikipedia.org/wiki/Terza_rima your ending is wrong. It should end with either Z or ZZ. \$\endgroup\$
    – Chris
    Dec 18, 2017 at 18:28
  • \$\begingroup\$ Can there be additional output beyond the rhyme scheme? This might save me a few bytes. \$\endgroup\$
    – NK1406
    Dec 18, 2017 at 23:02
  • \$\begingroup\$ @NK1406 Nope sorry. \$\endgroup\$
    – LiefdeWen
    Dec 19, 2017 at 7:13
  • 1
    \$\begingroup\$ @totallyhuman String array is fine. \$\endgroup\$
    – LiefdeWen
    Dec 19, 2017 at 7:14

90 Answers 90

25
\$\begingroup\$

JavaScript (ES6), 51 50 49 bytes

Saved 1 byte thanks to @l4m2

f=(n=45358)=>n%63?f(n-1333)+n.toString(36)+' ':''

Try it online!

How?

We start with n = 45358 (yzy in base-36). We subtract 1333 from n at each iteration (111 in base-36). We stop as soon as n MOD 63 = 0, because 12033 (9a9 in base-36) is the first value for which this condition is fulfilled, and 63 is the smallest modulo with such a property.

Decimal | Base-36 | MOD 63
--------+---------+-------
  45358 |   yzy   |   61
  44025 |   xyx   |   51
  42692 |   wxw   |   41
  41359 |   vwv   |   31
  40026 |   uvu   |   21
  38693 |   tut   |   11
  37360 |   sts   |    1
  36027 |   rsr   |   54
  34694 |   qrq   |   44
  33361 |   pqp   |   34
  32028 |   opo   |   24
  30695 |   non   |   14
  29362 |   mnm   |    4
  28029 |   lml   |   57
  26696 |   klk   |   47
  25363 |   jkj   |   37
  24030 |   iji   |   27
  22697 |   hih   |   17
  21364 |   ghg   |    7
  20031 |   fgf   |   60
  18698 |   efe   |   50
  17365 |   ded   |   40
  16032 |   cdc   |   30
  14699 |   bcb   |   20
  13366 |   aba   |   10
  12033 |   9a9   |    0
\$\endgroup\$
8
  • \$\begingroup\$ How did you decide base36? and are you sure its optimal? \$\endgroup\$
    – LiefdeWen
    Dec 18, 2017 at 13:38
  • 2
    \$\begingroup\$ @LiefdeWen It's the lowest base to contain all the letters, so it's optimal. \$\endgroup\$ Dec 18, 2017 at 13:39
  • \$\begingroup\$ @user202729 What do you mean? He's not OP, so he's one of those people you refer to? \$\endgroup\$ Dec 18, 2017 at 13:43
  • 17
    \$\begingroup\$ As an (un)interesting side-node, 1333 = 666*2+1 and this is my 666th answer on PPCG. \$\endgroup\$
    – Arnauld
    Dec 18, 2017 at 14:04
  • 3
    \$\begingroup\$ @Arnauld please ensure you use the value 1335 somewhere in your next answer. \$\endgroup\$
    – IanF1
    Dec 19, 2017 at 6:46
17
\$\begingroup\$

C (gcc), 41 bytes

f(i){for(i='ABA';i%29;i+=65793)puts(&i);}

Try it online!

Fixed and -9 thanks to user202729. -1 thanks to Dennis and -2 thanks to Arnauld too.

\$\endgroup\$
4
  • \$\begingroup\$ 45 bytes. \$\endgroup\$
    – DELETE_ME
    Dec 18, 2017 at 14:15
  • 1
    \$\begingroup\$ (i='ABA';i<'ZZZ';i+=65793) saves a byte. \$\endgroup\$
    – Dennis
    Dec 18, 2017 at 14:32
  • \$\begingroup\$ Use 59e5 instead of 'ZZZ' saves another byte. \$\endgroup\$
    – DELETE_ME
    Dec 18, 2017 at 15:00
  • \$\begingroup\$ You can use i%29 for 41 bytes \$\endgroup\$
    – Arnauld
    Dec 18, 2017 at 15:49
12
\$\begingroup\$

Jelly, 7 bytes

ØAṡ2ŒBY

Try it online!

-1 byte thanks to Dennis

ØAṡ2ŒBY  Main Link
ØA       Alphabet
  ṡ2     Slice into overlapping slices of length 2
    ŒB   Palindromize (bounce) each
      Y  Join by newlines
\$\endgroup\$
0
9
\$\begingroup\$

brainfuck, 51 48 bytes

Saved 3 bytes thanks to @ovs.

++++++++[>+>+++>++++++++<<<-]>++>+[->+.+.-.<<.>]

Try it online!

Explanation

INITIALIZE TAPE:

0000:           (none)
0001: C_NEWLINE (10)
0002: V_COUNT   (25)
0003: V_ALPHA   (64)
++++++++[>+>+++>++++++++<<<-]>++>+

V_COUNT TIMES:              [-
    INCREMENT V_ALPHA         >+
    PRINT V_ALPHA             .
    PRINT V_ALPHA PLUS ONE    +.
    PRINT V_ALPHA             -.
    PRINT C_NEWLINE           <<.
END LOOP                    >]
\$\endgroup\$
2
  • \$\begingroup\$ @ConorO'Brien - I just noticed that my answer is pretty similar to yours. Please feel free to comment if you think it is too close and I'll remove it. \$\endgroup\$
    – ElPedro
    Dec 18, 2017 at 15:49
  • 1
    \$\begingroup\$ @ElPedro No, you're fine, there isn't much room for innovation in this challenge :) \$\endgroup\$ Dec 18, 2017 at 17:16
9
\$\begingroup\$

brainfuck, 51 49 bytes

+++++[>+++++>+++++++++++++>++<<<-]>[>.+.-.+>.<<-]

Try it online!

An attempt at an explanation...

+++++                     #Put 5 in cell 0 because that is the highest common denominator of 10, 65 and 25
[                         #Start loop
>+++++                    #Counter in cell 1 is 25 (How many lines we must print)
>+++++++++++++            #Counter in cell 2 is 65 (ASCII A)  
>++                       #Counter in cell 3 is 10 (Newline)
<<<-]                     #Decrement the outer counter until the cells have the right values (muliples of 5).
>                         #Move to the counter that says how many lines we must print.
[>.                       #Print the character in cell 2
+.                        #Add one to the character in cell 2 and print it
-.                        #Subtract one from the character in cell 2 and print it
+                         #Add one to the character in cell 2 for the next loop
>.                        #Print a new line
<<-]                      #Decrement cell 1 and run again until cell 1 is 0

-2 with thanks to @ovs

My first attempt at brainfuck so any hints gratefully received. If I was more experienced with it then I am sure I could shave a few more bytes off but I only got into it yesterday.

\$\endgroup\$
1
  • \$\begingroup\$ -1 byte \$\endgroup\$
    – Jo King
    Feb 1, 2018 at 0:42
9
\$\begingroup\$

05AB1E, 5 bytes

Aü«€û

Try it online!

-1 byte thanks to Emigna
-1 byte thanks to rule change; thanks to kalsowerus for pointing that out

Hehe, currently beats Pyth. \o/

Explanation

Aü«€û   Full Program
A       Lowercase Alphabet
 ü«     Pairwise with merge-list
   €û   For each, palindromize
\$\endgroup\$
4
  • \$\begingroup\$ You could do Aü«€û» \$\endgroup\$
    – Emigna
    Dec 18, 2017 at 14:10
  • \$\begingroup\$ @Emigna O cool, thanks! :D \$\endgroup\$
    – hyper-neutrino
    Dec 18, 2017 at 14:11
  • \$\begingroup\$ Nice, that's what I got too without looking. \$\endgroup\$ Dec 19, 2017 at 15:00
  • \$\begingroup\$ According to the comments, a list of strings is fine as output, you can remove the join. \$\endgroup\$
    – kalsowerus
    Dec 20, 2017 at 14:24
8
\$\begingroup\$

Python 2, 42 41 bytes

-1 byte thanks to Halvard Hummel

i=65;exec"print'%c'*3%(i,i+1,i);i+=1;"*25

Try it online!

\$\endgroup\$
0
7
\$\begingroup\$

APL (Dyalog), 14 bytes

25↑0 1 0⊖3/⍪⎕A

Try it online!

Please note that the added ⎕← is for TIO to output the text and is not normally required.

\$\endgroup\$
6
\$\begingroup\$

Charcoal, 11 bytes

E²⁵✂αι⁺²ι‖O

Try it online! Link is to verbose version of code. Explanation:

 ²⁵         Literal 25
E           Map over implicit range
    α       Predefined uppercase letters
   ✂ ι⁺²ι   Slice 2 characters
            Implicitly print result on separate lines
         ‖O Reflect with overlap
\$\endgroup\$
6
\$\begingroup\$

Brain-Flak, 90 bytes

((((()()()){}){}){}()){(({})<((({}((((()()()()){}){}){}){})())[()])((()()()()()){})>[()])}

Try it online!

One reason this is shorter than the other brain-flak answer is because it uses uppercase instead of lowercase characters, which have smaller ASCII values, and are therefore easier to push.

Explanation:

#Push 25
((((()()()){}){}){}())

#While true
{

    #Keep track of the number on top of the stack...
    # We'll call it 'a'
    (({})

        #Push A +...
        <((({}

        # 64 (push)
        ((((()()()()){}){}){}){})
        # + 1 (push)
        ())
        # - 1 (push)
        [()])

        # Push 10
        ((()()()()()){})>

    # Now that's all pushed, we push a - 1 to decrement the loop counter
    [()])

# Endwhile
}
\$\endgroup\$
1
  • \$\begingroup\$ I write the first version and get no upvotes you write a golf version and get +5. ??? \$\endgroup\$
    – user63187
    Dec 19, 2017 at 0:30
6
\$\begingroup\$

brainfuck, 45 37 bytes

+[[<+>>++<-]>]<<---[-----<+.+.-.<.>>]

Try it online!

Prints in uppercase, separated by spaces with a trailing space.

How it Works:

+[[<+>>++<-]>] Intialises the tape with the format n^2
               1 2 4 8 16 32 64 128 0 0'
<<---          Navigates to the desired section and tweaks the counter
               1 2 4 8 16 32 64 125<
[-----<+.+.-.<.>>] Prints the Terza Rima, using:
                 125 as the loop counter (decremented by 5 each loop)
                 64 as the current alphabetic character (incremented and printed each loop)
                 32 as the space character
\$\endgroup\$
1
  • 3
    \$\begingroup\$ This is actually really nice! Good job! \$\endgroup\$
    – Dust
    Apr 21, 2018 at 10:17
5
\$\begingroup\$

R, 51 47 bytes

L=LETTERS;cat(sprintf("%s%s%1$s",L[-26],L[-1]))

Output:

> L=LETTERS;cat(sprintf("%s%s%1$s",L[-26],L[-1]))
ABA BCB CDC DED EFE FGF GHG HIH IJI JKJ KLK LML MNM NON OPO PQP QRQ RSR STS TUT UVU VWV WXW XYX YZY
\$\endgroup\$
7
  • \$\begingroup\$ The naive way without fancy sprintf is 49 bytes \$\endgroup\$
    – Giuseppe
    Dec 18, 2017 at 16:05
  • \$\begingroup\$ @Giuseppe that would be because i didn't use sprintf correctly :) \$\endgroup\$
    – plannapus
    Dec 18, 2017 at 16:24
  • \$\begingroup\$ OK, but I found a 40 byter :) \$\endgroup\$
    – Giuseppe
    Dec 18, 2017 at 16:51
  • \$\begingroup\$ @Giuseppe ouch :) \$\endgroup\$
    – plannapus
    Dec 18, 2017 at 16:51
  • 1
    \$\begingroup\$ another [40 byter][tio.run/##K/r/… based on converting ASCII codes, in case \$\endgroup\$
    – NofP
    Dec 19, 2017 at 9:08
5
\$\begingroup\$

Java 8, 132 85 62 60 Bytes

  • 47 bytes thanks to Neil
  • 26 bytes thanks to Oliver
  • 3 bytes and much better formatting thanks to Kevin
  • Error fixed by Oliver

Golfed

a->{for(char i=64;++i<90;)System.out.println(""+i+++i--+i);}

Ungolfed

public class TerzaRima {
    interface A{
        void a(String a);
    }
    static A a = a -> {
        for (char i = 64; ++i < 90; ) System.out.println("" + i++ + i-- + i);
    };
    public static void main(String[] args){
        a.a(null);
    }
}
\$\endgroup\$
9
  • 1
    \$\begingroup\$ Printing a char array would probably be a lot shorter. \$\endgroup\$
    – Neil
    Dec 18, 2017 at 16:21
  • 1
    \$\begingroup\$ Can you make i a char too? \$\endgroup\$
    – Neil
    Dec 18, 2017 at 16:28
  • 2
    \$\begingroup\$ a->{for(char c=64;++c<90;)System.out.println(""+c++ +c--+c);} (62 bytes) \$\endgroup\$ Dec 18, 2017 at 17:32
  • 2
    \$\begingroup\$ Currently this is a snippet instead of a function or program, so you'll have to add the v->{...} as mentioned above by @OlivierGrégoire. (In case you don't know how Java 8 lambdas work, I made an explanation once before.) Also, you can remove the brackets of the loop like Olivier did, and as additional golf you can change the print to System.out.print(" "+i+++i--+i); (a space instead of a new-line, and you don't need the space at c+++c--+c). Try it here. \$\endgroup\$ Dec 19, 2017 at 8:20
  • 1
    \$\begingroup\$ Thanks @KevinCruijssen for the tips as well as the java lambdas document. That was a very easy to follow guide to implement some simple lambdas. I have updated accordingly! \$\endgroup\$ Dec 19, 2017 at 17:10
5
\$\begingroup\$

><>, 25 24 23 22 bytes

Saved 2 bytes thanks to Jo King

"!;::o1+:o$oao:'Y')0.A

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ 23 bytes by removing a quote \$\endgroup\$
    – Jo King
    Jan 2, 2018 at 15:36
  • \$\begingroup\$ Another byte \$\endgroup\$
    – Jo King
    Jan 31, 2018 at 23:54
  • \$\begingroup\$ @JoKing: Thanks again! \$\endgroup\$
    – Emigna
    Feb 1, 2018 at 7:04
5
\$\begingroup\$

Befunge-98 (FBBI), 27 23 21 bytes

-4 bytes thanks to James Holderness

j'@1+3k:,1+,,a,'Y/!1+

Try it online!

\$\endgroup\$
0
5
\$\begingroup\$

Vyxal j, 3 bytes

nzʁ

Try it Online!

n   # Lowercase alphabet
 z  # Overlapping pairs of elements
  ʁ # Mirror each
    # (j flag joins list of lines by newlines)
\$\endgroup\$
4
\$\begingroup\$

Husk, 9 bytes

ẊS:`e…"AZ

Try it online!

\$\endgroup\$
4
\$\begingroup\$

brainfuck, 41 bytes

-[[<+>--->++>>>+<<<<]>+]>>>[<<.+.-.+>.>-]

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ +1 I follow most of it but it starts with -[ Does that mean you are setting cell 0 to -1 before you start the loop? Any chance of an explanation for a brainfuck newbie if you have time? Btw, thanks for the -2 on my effort. \$\endgroup\$
    – ElPedro
    Dec 21, 2017 at 18:41
  • \$\begingroup\$ @ElPedro this depends on the brainfuck interpreter, the interpreter at tio.run stores unsigned 8-bit numbers in each cell, so the first cell gets 255 before the loop. \$\endgroup\$
    – ovs
    Dec 21, 2017 at 18:47
  • \$\begingroup\$ @ElPedro the initialization segment is actually generated by BF-Crunch. \$\endgroup\$
    – ovs
    Dec 21, 2017 at 18:51
3
\$\begingroup\$

Pyth, 7 bytes

jC[GtGG

Try it here!

Hehe, currently beats Jelly. \o/

Easily translates to the follwong 05ab1e program:

05AB1E, 7 bytes

AA¦A)ø»

Try it online!

\$\endgroup\$
3
\$\begingroup\$

J, 15 bytes

u:2$~&3\65+i.26

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This is a tricky way to make a palindrome! \$\endgroup\$ Dec 18, 2017 at 16:53
3
\$\begingroup\$

Haskell, 34 32 31 bytes

Based on totallyhuman's answer. -1 byte thanks to H.PWiz

do x<-['A'..'Y'];x:succ x:x:" "

Try it online!

\$\endgroup\$
1
3
\$\begingroup\$

Brain-Flak, 180 bytes

(()()())(({}){})(({}){})(({}){})(({}){})(({}){}())<>(()()()()())(({}){})(({}){})(()()()()(){}){({}[()])<>(({}))(({}()))(({}[()]))({}()<(()()()()()()()()()())>)<>}<>{}{}{({}<>)<>}<>

Try it online!

Thanks to DJ for getting this working

\$\endgroup\$
3
\$\begingroup\$

Haskell, 28 bytes

[[x,succ x,x]|x<-['A'..'Y']]

Try it online!

succ is such an unfortunate naming choice...

Explanation

[[x,succ x,x]|x<-['A'..'Y']]

[            |x<-          ]  -- for x in...
                 ['A'..'Y']   -- the alphabet sans Z
 [x,succ x,x]                 -- construct a string of x, the successor of x and x
\$\endgroup\$
4
  • 4
    \$\begingroup\$ I do not feel like it is completely fair to return a list of strings instead of separating them with space or newline as required. \$\endgroup\$
    – user28667
    Dec 18, 2017 at 16:13
  • \$\begingroup\$ @user28667 It's usually allowed in challenges (it probably should be on the Default IO post, if it's not already). The OP is yet to respond in this challenge specifically. This is, however, no reason to downvote. \$\endgroup\$ Dec 18, 2017 at 22:06
  • \$\begingroup\$ @totallyhuman At the moment the challenge explicitly specifies that either spaces or newlines should be used to separate the output, thus this answer is currently invalid. So strictly according to the site rules it should be deleted, or just fixed by prepending a unlines or unwords. \$\endgroup\$
    – Laikoni
    Dec 18, 2017 at 22:14
  • \$\begingroup\$ @Laikoni I seem to have ninja'd you. The challenge doesn't explicitly deny a list of lines as output. (Plus, that would invalidate a fair amount of answers.) Either way, the answer is now not invalid. \$\endgroup\$ Dec 18, 2017 at 22:18
3
\$\begingroup\$

R, 40 bytes

cat(intToUtf8(rbind(x<-65:89,x+1,x,10)))

Try it online!

One more alternative in R to Plannapus and Giuseppe's answers. Posted following their request. This solution uses ASCII code to UTF8 coding.

PS if TABs were allowed, one could replace the newline (ASCII code 10) with a tabulation (ASCII code 9), and the solution could shrink to 39 bytes:

cat(intToUtf8(rbind(x<-65:89,x+1,x,9)))

\$\endgroup\$
1
  • \$\begingroup\$ I think using 9 is perfectly fine, since it's whitespace, which is allowed by the OP. \$\endgroup\$
    – Giuseppe
    Apr 20, 2018 at 13:40
3
\$\begingroup\$

R, 40 36 bytes

cat(paste0(L<-LETTERS,L[-1],L)[-26])

Try it online!

An alternative to plannapus' answer; this turns out to be a bit golfier; I've posted it per his request.

See also this 40 byte answer by NofP which is quite nice.

\$\endgroup\$
3
\$\begingroup\$

PowerShell, 39 37 bytes

65..89|%{-join[char[]]($_,++$_,--$_)}

Try it online!

Loops from 65 to 89. Each iteration, we're constructing an integer array of (the current, one more, and the current) of the current digit, using pre-increment and pre-decrement. That's then re-cast as a char-array, and -joined together into a single string. Each string is left on the pipeline and an implicit Write-Output at program completion gives us a newline between each element for free.


Alternatively, same byte count

65..89|%{-join[char[]]($_,($_+1),$_)}

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ ($_,($_+1),$_) is an alternative same-length tuple \$\endgroup\$
    – Veskah
    Jul 18, 2018 at 2:16
3
\$\begingroup\$

Pepe, 59 56 bytes

-3 bytes thanks to u_ndefined

REeEeEEeEerEeEeeeeeERrEEEEErEEEeeREEreeerEEEEEeeEreeERee

Try it online!

Explanation:

# Prepare stacks

  # prepare stack R [Z]
  REeEeEEeEe  # push Z

  # prepare stack r [A,B,A]
  rEeEeeeeeE  # push A
  RrEEEEE     # copy and increment A (getting B)
  rEEEee      # duplicate A to end

# Start loop
REE           # create label Z

  reee        # output stack r contents
  rEEEEEeeE   # increment all

  reeE        # end line

Ree           # loop while r[p] != Z
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Replacing rEeEeeeeEe with RrEEEEE saves off 3 bytes \$\endgroup\$
    – u-ndefined
    Aug 6, 2018 at 10:41
  • \$\begingroup\$ @u_ndefined Thanks! I made this code before flags were added to Pepe. Updated the answer. \$\endgroup\$
    – RedClover
    Aug 6, 2018 at 11:02
3
\$\begingroup\$

Pip, 8 bytes

PZ_.BMPz

Try it online!

\$\endgroup\$
3
\$\begingroup\$

vJASS (Warcraft 3), 203 189 182 177 156 bytes

Using //! import zinc "<code_path>" command to exclude //! zinc and //! endzinc.


library q{integer w,e;string r="ABCDEFGHIJKLMNOPQRSTUVWXYZ",t;function onInit(){for(0<=w<25){e=w+1;t=SubString(r,w,e);BJDebugMsg(t+SubString(r,e,e+1)+t);}}}

Readable Version:

library q{
    integer Index, Increment;
    string Alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", Letter;

    function onInit(){
        /*
        *   Index from 0 to 24
        */
        for (0 <= Index < 25){
            Increment = Index + 1;

            // Slice
            Letter = SubString(Alphabets, Index, Increment);

            // Output
            BJDebugMsg(Letter + SubString(Alphabets, Increment, Increment + 1) + Letter);
        }
    }
}

Warcraft 3




Verification purposes only:
The in-game text screen has limited space. Using Preload(), we may able to see the full output.

//! zinc
library q{
    integer Index, Increment;
    string Alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", Letter;

    function onInit(){
        // Create an empty text file (Preload file).
        PreloadGenClear();
        PreloadGenStart();

        /*
        *   Index from 0 to 24
        */
        for (0 <= Index < 25){
            Increment = Index + 1;

            // Slice
            Letter = SubString(Alphabets, Index, Increment);

            // Output
            Preload(Letter + SubString(Alphabets, Increment, Increment + 1) + Letter);
        }

        // Save .txt
        PreloadGenEnd("CodeGolf\\testOutput.txt");
    }
}
//! endzinc

Preload File as an output

\$\endgroup\$
2
\$\begingroup\$

J, 18 16 bytes

2(,{.)\u:65+i.26

Changed a.{~ to u: after Conor O'Brien's and FrownyFrog's solutions

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Alternative 16 byte: u:(i.25)+/66-#:5 \$\endgroup\$
    – FrownyFrog
    Dec 18, 2017 at 18:00

Your Answer

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

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