terça-feira, 9 de janeiro de 2024

Uma Linha


TOP SECRET do not read this article

Vamos dar início à iniciação na Sociedade Secreta dos programas obfuscados escritos em 1 linha. Isto é só para programadores avançados. Não tentem fazer isto em casa senão podem ficar com os miolos em puré (papa)!

A maneira mais fácil de escrever um programa em apenas 1 linha é usar BASIC Apascalado. Eis os passos:

1. Ir até à página do Conversor de BASIC Apascalado (aqui).

2. Escrever um programa.

Exemplo:

PROGRAM factorials PROC main routine END

DEFPROC main routine
PROC introduction
PROC get limit
PROC write factorials
ENDPROC

DEFPROC introduction
PRINT "Factorials list"
PRINT "Pascalated BASIC demonstration"
PRINT
ENDPROC

DEFPROC get limit
REPEAT w
   PRINT "max argument = ";
   INPUT limit : PRINT limit
   IF limit >= 12 BEGIN y
     PRINT
     PRINT "Must be lower than 12."
     PRINT "Try again."
     PRINT
   ELSEIF limit < 0
     PRINT
     PRINT "Must be greater than 0."
     PRINT "Try again."
     PRINT
   ELSE
     PRINT
     PRINT "Valid input."
     PRINT
   ENDIF
UNTIL (limit >= 0) AND (limit <= 11)
ENDPROC

DEFPROC write factorials
PRINT
FOR w = limit TO 0 STEP -1
   LET argument = w
   PRINT "fact(";argument;") = ";
   PROC calculate factorial
   PRINT result
NEXT w
ENDPROC

DEFPROC calculate factorial
LET result = 1
WHILE argument > 1 DO y
   LET result = result * argument
   LET argument = argument-1
ENDWHILE
   ENDPROC

3. Juntar o programa em apenas um procedimento.

Exemplo: Onde está "PROC Main Routine" substituir pelo conteúdo do procedimento "Main Routine". Fazer isto com todos os procedimentos até ficar algo como:

PROGRAM factorials PRINT "Factorials list"
PRINT "Pascalated BASIC demonstration"
PRINT
REPEAT w
   PRINT "max argument = ";
   INPUT limit : PRINT limit
   IF limit >= 12 BEGIN y
     PRINT
     PRINT "Must be lower than 12."
     PRINT "Try again."
     PRINT
   ELSEIF limit < 0
     PRINT
     PRINT "Must be greater than 0."
     PRINT "Try again."
     PRINT
   ELSE
     PRINT
     PRINT "Valid input."
     PRINT
   ENDIF
UNTIL (limit >= 0) AND (limit <= 11)
PRINT
FOR w = limit TO 0 STEP -1
   LET argument = w
   PRINT "fact(";argument;") = ";
   LET result = 1
   WHILE argument > 1 DO y
     LET result = result * argument
     LET argument = argument-1
   ENDWHILE
   PRINT result
NEXT w
END

4. Converter para Sinclair BASIC com o Conversor BASIC Apascalado, remover os números de linha, remover os comentários REM e juntar tudo numa linha inserindo o caracter ":".

Nota: Se tentarem converter um programa com mais de 100 linhas o conversor vai rejeitar isso. Para contornar esse problema, podem trabalhar com a listagem convertida para Sinclair BASIC.

Exemplo: Onde está "GO SUB MainRoutine" substituir pelo conteúdo do procedimento "Main Routine".

Eis o resultado final:

1 PRINT "Factorials list" : PRINT "Pascalated BASIC demonstration": PRINT : FOR w = 0 TO 0.5 STEP 0 : PRINT "max argument = "; : INPUT limit : PRINT limit : LET y = limit >= 12 : FOR y = y TO 0.5 STEP -2 : PRINT : PRINT "Must be lower than 12." : PRINT "Try again." : PRINT : NEXT y : LET y = (y<0)*-1 + (y=0)*( limit < 0 ) : FOR y = y TO 0.5 STEP -2 : PRINT : PRINT "Must be greater than 0." : PRINT "Try again." : PRINT : NEXT y : LET y = (y=0) : FOR y = y TO 0.5 STEP -2 : PRINT : PRINT "Valid input." : PRINT : NEXT y : LET w = (limit >= 0) AND (limit <= 11) : NEXT w : PRINT : FOR w = limit TO 0 STEP -1 : LET argument = w : PRINT "fact(";argument;") = "; : LET result = 1 : FOR y = NOT (argument > 1) TO 0.5 STEP 0 : LET result = result * argument : LET argument = argument-1 : LET y = NOT (argument > 1) : NEXT y : PRINT result : NEXT w

Clicar aqui para correr online um exemplo mais interessante.

Clicar aqui para fazer download dos ficheiros.

2 comentários: