Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Imprimindo uma pirâmide de alfabeto em PL/SQL


Este funciona:
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

Parece DBMS_OUTPUT.PUT_LINE() corta os caracteres de espaço à esquerda (pelo menos no meu TOAD), então coloco _ no inicio.