Code-golf - the description is the program

Asked

Viewed 162 times

6

It will be accepted at most a include (or Resources file etc) that is native to the language (which is probably what will make all the interesting part) and the description of the problem shall be the code of the.

An example in C:

#include "magica.h"
inicia o programa
dado i igual 5
leia x
se x menor que i
imprime verdadeiro
senao
imprime falso
encerra

magic. h

#define inicia int
#define o
#define programa main(){
#define dado int
#define igual =
#define x
#define leia ,d=0;scanf("%d",&d);
#define se if(
#define menor d<
#define que
#define imprime
#define verdadeiro )printf("verdadeiro");
#define senao else
#define falso printf("falso");
#define encerra return 0;}
#include<stdio.h>

The challenge:

Dado um vetor com 20 elementos
no qual o primeiro eh 1 e o ultimo 20 e os 
elementos intermediarios sao incrementos do mesmo
Seja i um inteiro,
para i de 3 a 15 faca
se o elemento i do vetor eh um numero primo 
imprima i
fim do programa

Note:

Will be accepted small amendments to the text provided that they do not alter the meaning of the text. The most voted community response will be chosen before 7 July (one week).

  • 5

    This question seems to be discounted because it is about Code Golf, set in Stack Overflow Meta as out of scope.

  • 3

    @Gypsy omorrisonmendez, but it seems that most of the goal agrees in having code golf.

  • 1

    Yes, the community is well divided on this aspect... I will take the bait: if the question is not closed until I have an answer, I will post. P

  • 3

    It’s still a very divided question @Demarco as you can see in this answer

  • This question really shows how divided the community is: The question has 2 votes in favor (no votes against) and 4 votes to close.

  • 3

    For those who don’t have enough reputation to see the positive and negative count of votes, which @Jorgeb. refers is that the item on Code Golf in the opinion poll is +19/-11, showing that both a lot of people want it and a lot of people don’t want it. Similarly, in this particular question 4 voted to close and 3 voted to leave open (so far).

  • I think it’s interesting to have Code Golf Aki, since the existing community is in English and we don’t want to create an entirely new one just to use another language, I’ll see how it goes :)

  • 1

    I have said several times that I do not like this kind of question, but for coherence with the current vote on the goal, it should be open.

  • I read everything you wrote, but so far my question remains the same... What is the question?

Show 4 more comments

1 answer

4


Prolog (38 lines)

magic for.

/* Defino minha mini-linguagem */

:-op(10, fy, [dado, um, vetor, com, no, qual, o, primeiro, eh, ultimo, intermediarios, 
              sao, incrementos, do, seja, para, se, elemento, imprima, fim]).
:-op(20, xfy, [elementos, e, um, de, a, do, primo]).
:-op(30, xfy, [faca]).

/* Defino os comandos da minha mini-linguagem */

term_expansion((dado um vetor com _ elementos no qual o primeiro eh A e o ultimo B e
os elementos intermediarios sao incrementos do mesmo), vetor(V)) :-
    criar_vetor(A,B,V).

term_expansion((seja i um inteiro), inteiro(i)). % Não interessa...

term_expansion((para I de A a B faca C), _) :-
    criar_vetor(A,B,V), % Seja um vetor de A a B
    member(X,V),        % e X um membro desse vetor
    chame(C,I,X),       % Faça o que foi pedido, usando I como alias pra X
    fail.               % Faça de conta que falhou. Tente de novo, com um novo membro
term_expansion((para _ de _ a _ faca _), feito). % Todos os números de A a B foram visitados

chame((se o elemento I do vetor eh um numero primo ProximaInstrucao), I, X) :-
  vetor(V),
  elemento(X,V,E),
  (primo(E) -> chame(ProximaInstrucao, I, E)).

chame((imprima I), I, X) :-
    writeln(X).

term_expansion(fim do programa, fim_do_programa). % Não interessa...

/* Implemento os comandos */

criar_vetor(A,A,[A]).
criar_vetor(A,B,[A|R]) :-
  A < B,
  A1 is A + 1,
  criar_vetor(A1,B,R).

elemento(_,[],_) :- fail.
elemento(1,[E|_],E) :- !.
elemento(X,[_|R],E) :-
    X1 is X - 1,
    elemento(X1,R,E).

primo(X) :-              % X não é primo se
    X1 is X - 1,
    criar_vetor(2,X1,V), % dado um vetor de 2 a X-1
    member(Y,V),         % existe um Y membro desse vetor
    0 =:= X mod Y,       % tal que X é multiplo de Y
    !, fail.
primo(_).                % Caso contrário ele é primo

codegolf.pro

:-include(magica).

dado um vetor com 20 elementos
no qual o primeiro eh 1 e o ultimo 20 e os 
elementos intermediarios sao incrementos do mesmo.
seja i um inteiro.
para i de 3 a 15 faca
se o elemento i do vetor eh um numero primo 
imprima i.
fim do programa.

Tested on Swi-Prolog 6.6.5. Example in Ideone.

  • it was not difficult to choose the answer, after all it was the only one. A pity that the question was closed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.