Oracle
 sql >> Database >  >> RDS >> Oracle

Stampa di una piramide alfabetica in PL/SQL

Questo funziona:

DECLARE
    aLine VARCHAR2(100);
BEGIN
    FOR PyramidLevel IN 0..25 LOOP
        aLine := NULL;

        FOR i IN 0..PyramidLevel-1 LOOP
            aLine := aLine || CHR(i+65);
        END LOOP; 

        FOR i IN REVERSE 0..PyramidLevel LOOP
            aLine := aLine || CHR(i+65);
        END LOOP;
        aLine := LPAD(aLine, 26+PyramidLevel);       

       DBMS_OUTPUT.PUT_LINE('_'||aLine);
    END LOOP;
END;


_                         A
_                        ABA
_                       ABCBA
_                      ABCDCBA
_                     ABCDEDCBA
_                    ABCDEFEDCBA
_                   ABCDEFGFEDCBA
_                  ABCDEFGHGFEDCBA
_                 ABCDEFGHIHGFEDCBA
_                ABCDEFGHIJIHGFEDCBA
_               ABCDEFGHIJKJIHGFEDCBA
_              ABCDEFGHIJKLKJIHGFEDCBA
_             ABCDEFGHIJKLMLKJIHGFEDCBA
_            ABCDEFGHIJKLMNMLKJIHGFEDCBA
_           ABCDEFGHIJKLMNONMLKJIHGFEDCBA
_          ABCDEFGHIJKLMNOPONMLKJIHGFEDCBA
_         ABCDEFGHIJKLMNOPQPONMLKJIHGFEDCBA
_        ABCDEFGHIJKLMNOPQRQPONMLKJIHGFEDCBA
_       ABCDEFGHIJKLMNOPQRSRQPONMLKJIHGFEDCBA
_      ABCDEFGHIJKLMNOPQRSTSRQPONMLKJIHGFEDCBA
_     ABCDEFGHIJKLMNOPQRSTUTSRQPONMLKJIHGFEDCBA
_    ABCDEFGHIJKLMNOPQRSTUVUTSRQPONMLKJIHGFEDCBA
_   ABCDEFGHIJKLMNOPQRSTUVWVUTSRQPONMLKJIHGFEDCBA
_  ABCDEFGHIJKLMNOPQRSTUVWXWVUTSRQPONMLKJIHGFEDCBA
_ ABCDEFGHIJKLMNOPQRSTUVWXYXWVUTSRQPONMLKJIHGFEDCBA
_ABCDEFGHIJKLMNOPQRSTUVWXYZYXWVUTSRQPONMLKJIHGFEDCBA

Sembra DBMS_OUTPUT.PUT_LINE() taglia i caratteri spaziali iniziali (almeno nel mio TOAD), quindi inserisco _ all'inizio.