Search Results
Search type | Search syntax |
---|---|
Tags | [tag] |
Exact | "words here" |
Author |
user:1234 user:me (yours) |
Score |
score:3 (3+) score:0 (none) |
Answers |
answers:3 (3+) answers:0 (none) isaccepted:yes hasaccepted:no inquestion:1234 |
Views | views:250 |
Code | code:"if (foo != bar)" |
Sections |
title:apples body:"apples oranges" |
URL | url:"*.example.com" |
Saves | in:saves |
Status |
closed:yes duplicate:no migrated:no wiki:no |
Types |
is:question is:answer |
Exclude |
-[tag] -apples |
For more details on advanced search visit our help page |
Code-golf is a competition to solve a particular problem in the fewest bytes of source code.
9
votes
Re-name all identifiers to a single letter
Prolog (SWI), 81 70 bytes
\X,[I]-->[H],{copy_term(X,Z),\(Z,H,I);nth1(I,X,H)},(\X;[]).
a--> \_,!.
Try it online!
Instead of using predicates, I use definite clause grammar (DCG) notation.
DCG notation …
4
votes
Incrementally Increment Identical Integers
Prolog (SWI), 75 66 bytes
B+A,[C]-->[A],{C is A+B},B+1+A;[X],{C=X},1+X.
_+_-->[].
a-->0+x,!.
Try it online!
This program can be run by calling the grammar a//0 to parse the input list. The remainder …
3
votes
Diamondize a Matrix
Prolog (SWI), 111 bytes
A*B*C:-append(A,B,C).
s(A,B,C):-A*[B]*C.
N+B:-N=[A|X],X+R,Z*Q*R,maplist(s,[[]|Z],A,Y),Y*Q*B,!;maplist(=([]),B).
Try it online!
Explanation
Let's first look a version of the co …
1
vote
Code golf edit distance up to 3
Prolog (SWI), 109 bytes
_+[]+[].
N+[A|B]+[A|C]:-!,N+B+C.
\N+B+C:-B=[_|X],N+X+C;C=[_|Y],N+B+Y;B=[_|X],C=[_|Y],N+X+Y.
B*C:- \ \ \a+B+C.
Try it online!
This is a golf of Wheat Wizard's answer. The key m …
1
vote
Tips for golfing in Pure Data
Avoid using triggers to sequence messages
One of the common uses of trigger objects is to make the order of execution clear when a single object is sending messages to multiple places. In those cases …
1
vote
Converting a number from Zeckendorf Representation to Decimal
Prolog (SWI), 74 bytes
\D-->[X,Y],{D is 2*X+Y};[D];a,\D.
a,[A],[B]-->[X,Y,Z],{A is X+Y,B is X+Z}.
Try it online!
Takes input as a list of integers 1 and 0 with the most significant bit first.
4
votes
A Chiral Puzzle
Prolog (SWI), 649 188 bytes
Unflipped, Unrotated
:-% l :%r %-:
write(%e%-(i %(etirw
left).%f.weg%.)right
% t)rth. %
% )tiit) %
% .htr)t %
right).%gew.f%.)left
wr …
1
vote
Generate Men of Culture
Prolog (SWI), 109 bytes
[]+[]+[].
I+[B|X]+[C|O]:-(I=[B|J],B=C;I=J,C=45),J+X+O.
I-O:-I+`Ah, I see you're a man of culture as well.`+O.
Try it online!
Case-sensitive and whitespace sensitive. The -/ …
3
votes
Rotational symmetry of string
Prolog (SWI), 64 bytes
A+B:-findall(X,(append(X,Y,A),append(Y,X,A)),[_|Z]),length(Z,B).
Try it online!
Defines a predicate +/2 that takes a string (in the form of a list of character codes) as its …
4
votes
Is It Mountainous?
Prolog (SWI), 29 bytes
a-->[X],+X.
+X-->[];a,[X],+X.
Try it online!
This program defines a DCG rule a//0 that matches any string (list of characters) that is a mountainous string.
Explanation
Fo …
6
votes
RoboZZle interpreter
Prolog (SWI), 574 bytes
Z*A:-findall(X^Y-D,(nth0(Y,Z,O),nth0(X,O,C),plus(32,C,D)),L),list_to_assoc(L,A).
N^C^G^[P,I|R]^F^X^Y^D:-map_assoc(\=(74),G);N<1e3,get_assoc(X^Y,G,J),J>67,put_assoc(X^Y,G,78,H) …
3
votes
Topple some dominoes!
Prolog (SWI), 132 bytes
+[]-->[].
+[47,124,92|T]-->"/|\\",+T.
+[47,47|T]-->"/|",+T.
+[92,92|T]-->"|\\",+T.
+[X|T]-->[X],+T.
X+Y:- +(N,X,[]),!,(X=N,Y=N;N+Y).
Try it online!
This program defines a p …
15
votes
Tips for golfing in Prolog
Use Operators for Predicate Names
It is possible to give predicates operators as names so long as the operator is one of the predefined operators (listed here) and is not already defined as a predicat …
5
votes
It's Hip to be Square
Prolog (SWI), 27 bytes
+N:-between(0,N,I),N=:=I*I.
Try it online!
Explanation
Searches through all numbers greater or equal to 0 and less than or equal to N and tests whether that number squared …
12
votes
Is this number a prime?
Prolog (SWI), 42 40 39 bytes
+X:-X>1,2+X.
Y+X:-X=<Y;0<X mod Y,1+Y+X.
Try it online!
Tests primality by checking whether any of the numbers less than the number and greater than one divide the number. …