99 Prolog Problems
99 Prolog Problems
Every predicate that you write should begin with a comment that describes the predicate in a declarative
statement. Do not describe procedurally, what the predicate does, but write down a logical statement which
includes the arguments of the predicate. You should also indicate the intended data types of the arguments and
the allowed flow patterns.
The problems have different levels of difficulty. Those marked with a single asterisk (*) are easy. If you have
successfully solved the preceeding problems you should be able to solve them within a few (say 15) minutes.
Problems marked with two asterisks (**) are of intermediate difficulty. If you are a skilled Prolog programmer
it shouldn't take you more than 30-90 minutes to solve them. Problems marked with three asterisks (***) are
more difficult. You may need more time (i.e. a few hours or more) to find a good solution.
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 1 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
Example:
?- my_flatten([a, [b, [c, d], e]], X).
X = [a, b, c, d, e]
Example:
?- compress([a,a,a,a,b,c,c,a,a,d,e,e,e,e],X).
X = [a,b,c,a,d,e]
Example:
?- pack([a,a,a,a,b,c,c,a,a,d,e,e,e,e],X).
X = [[a,a,a,a],[b],[c,c],[a,a],[d],[e,e,e,e]]
Example:
?- encode([a,a,a,a,b,c,c,a,a,d,e,e,e,e],X).
X = [[4,a],[1,b],[2,c],[2,a],[1,d][4,e]]
Example:
?- encode_modified([a,a,a,a,b,c,c,a,a,d,e,e,e,e],X).
X = [[4,a],b,[2,c],[2,a],d,[4,e]]
Example:
?- encode_direct([a,a,a,a,b,c,c,a,a,d,e,e,e,e],X).
X = [[4,a],b,[2,c],[2,a],d,[4,e]]
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 2 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
P17 (*) Split a list into two parts; the length of the first part is given.
Do not use any predefined predicates.
Example:
?- split([a,b,c,d,e,f,g,h,i,k],3,L1,L2).
L1 = [a,b,c]
L2 = [d,e,f,g,h,i,k]
Example:
?- slice([a,b,c,d,e,f,g,h,i,k],3,7,L).
X = [c,d,e,f,g]
?- rotate([a,b,c,d,e,f,g,h],-2,X).
X = [g,h,a,b,c,d,e,f]
Hint: Use the predefined predicates length/2 and append/3, as well as the result of problem P17.
P22 (*) Create a list containing all integers within a given range.
Example:
?- range(4,9,L).
L = [4,5,6,7,8,9]
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 3 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
P23 (**) Extract a given number of randomly selected elements from a list.
The selected items shall be put into a result list.
Example:
?- rnd_select([a,b,c,d,e,f,g,h],3,L).
L = [e,d,a]
Hint: Use the built-in random number generator random/2 and the result of problem P20.
P24 (*) Lotto: Draw N different random numbers from the set 1..M.
The selected numbers shall be put into a result list.
Example:
?- rnd_select(6,49,L).
L = [23,1,17,33,21,37]
P26 (**) Generate the combinations of K distinct objects chosen from the N elements of a list
In how many ways can a committee of 3 be chosen from a group of 12 people? We all know that there
are C(12,3) = 220 possibilities (C(N,K) denotes the well-known binomial coefficients). For pure
mathematicians, this result may be great. But we want to really generate all the possibilities (via
backtracking).
Example:
?- combination(3,[a,b,c,d,e,f],L).
L = [a,b,c] ;
L = [a,b,d] ;
L = [a,b,e] ;
...
Example:
?- group3([aldo,beat,carla,david,evi,flip,gary,hugo,ida],G1,G2,G3).
G1 = [aldo,beat], G2 = [carla,david,evi], G3 = [flip,gary,hugo,ida]
...
b) Generalize the above predicate in a way that we can specify a list of group sizes and the predicate will
return a list of groups.
Example:
?- group([aldo,beat,carla,david,evi,flip,gary,hugo,ida],[2,2,5],Gs].
Gs = [[aldo,beat],[carla,david],[evi,flip,gary,hugo,ida]]
...
Note that we do not want permutations of the group members; i.e. [[aldo,beat],...] is the same solution as
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 4 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
You may find more about this combinatorial problem in a good book on discrete mathematics under the
term "multinomial coefficients".
Example:
?- lsort([[a,b,c],[d,e],[f,g,h],[d,e],[i,j,k,l],[m,n],[o]],L).
L = [[o], [d, e], [d, e], [m, n], [a, b, c], [f, g, h], [i, j, k, l]]
b) Again, we suppose that a list (InList) contains elements that are lists themselves. But this time the
objective is to sort the elements of InList according to their length frequency; i.e. in the default, where
sorting is done ascendingly, lists with rare lengths are placed first, others with a more frequent length
come later.
Example:
?- lfsort([[a,b,c],[d,e],[f,g,h],[d,e],[i,j,k,l],[m,n],[o]],L).
L = [[i, j, k, l], [o], [a, b, c], [f, g, h], [d, e], [d, e], [m, n]]
Note that in the above example, the first two lists in the result L have length 4 and 1, both lengths appear
just once. The third and forth list have length 3 which appears, there are two list of this length. And
finally, the last three lists have length 2. This is the most frequent length.
Arithmetic
P31 (**) Determine whether a given integer number is prime.
Example:
?- is_prime(7).
Yes
P32 (**) Determine the greatest common divisor of two positive integer numbers.
Use Euclid's algorithm.
Example:
?- gcd(36, 63, G).
G=9
P33 (*) Determine whether two positive integer numbers are coprime.
Two numbers are coprime if their greatest common divisor equals 1.
Example:
?- coprime(35, 64).
Yes
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 5 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
Example: m = 10: r = 1,3,7,9; thus phi(m) = 4. Note the special case: phi(1) = 1.
?- Phi is totient_phi(10).
Phi = 4
Find out what the value of phi(m) is if m is a prime number. Euler's totient function plays an important
role in one of the most widely used public key cryptography methods (RSA). In this exercise you should
use the most primitive method to calculate this function (there are smarter ways that we shall discuss
later).
P36 (**) Determine the prime factors of a given positive integer (2).
Construct a list containing the prime factors and their multiplicity.
Example:
?- prime_factors_mult(315, L).
L = [[3,2],[5,1],[7,1]]
P38 (*) Compare the two methods of calculating Euler's totient function.
Use the solutions of problems P34 and P37 to compare the algorithms. Take the number of logical
inferences as a measure for efficiency. Try to calculate phi(10090) as an example.
Example:
?- goldbach(28, L).
L = [5,23]
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 6 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
Example:
?- goldbach_list(9,20).
10 = 3 + 7
12 = 5 + 7
14 = 3 + 11
16 = 3 + 13
18 = 5 + 13
20 = 3 + 17
In most cases, if an even number is written as the sum of two prime numbers, one of them is very small.
Very rarely, the primes are both bigger than say 50. Try to find out how many such cases there are in the
range 2..3000.
A logical expression in two variables can then be written in prefix notation, as in the following example:
and(or(A,B),nand(A,B)).
Now, write a predicate table/3 which prints the truth table of a given logical expression in two variables.
Example:
?- table(A,B,and(A,or(A,B))).
true true true
true fail true
fail true fail
fail fail fail
Example:
?- table(A,B, A and (A or not B)).
true true true
true fail true
fail true fail
fail fail fail
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 7 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
variables. Define table/2 in a way that table(List,Expr) prints the truth table for the expression Expr,
which contains the logical variables enumerated in List.
Example:
?- table([A,B,C], A and (B or C) equ A and B or A and C).
true true true true
true true fail true
true fail true true
true fail fail true
fail true true true
fail true fail true
fail fail true true
fail fail fail true
Find out the construction rules and write a predicate with the following specification:
Can you apply the method of "result caching" in order to make the predicate more efficient, when it is to
be used repeatedly?
We suppose a set of symbols with their frequencies, given as a list of fr(S,F) terms. Example:
[fr(a,45),fr(b,13),fr(c,12),fr(d,16),fr(e,9),fr(f,5)]. Our objective is to construct a list hc(S,C) terms, where
C is the Huffman code word for the symbol S. In our example, the result could be Hs = [hc(a,'0'),
hc(b,'101'), hc(c,'100'), hc(d,'111'), hc(e,'1101'), hc(f,'1100')] [hc(a,'01'),...etc.]. The task shall be
performed by the predicate huffman/2 defined as follows:
Binary Trees
A binary tree is either empty or it is composed of a root element and two successors, which are binary
trees themselves.
In Prolog we represent the empty tree by the atom 'nil' and the non-empty
tree by the term t(X,L,R), where X denotes the root node and L and R
denote the left and right subtree, respectively. The example tree depicted
opposite is therefore represented by the following Prolog term:
T1 = t(a,t(b,t(d,nil,nil),t(e,nil,nil)),t(c,nil,t(f,t(g,nil,nil),nil)))
Other examples are a binary tree that consists of a root node only:
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 8 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
You can check your predicates using these example trees. They are given as test cases in p54.pl.
Write a predicate cbal_tree/2 to construct completely balanced binary trees for a given number of nodes.
The predicate should generate all solutions via backtracking. Put the letter 'x' as information into all nodes
of the tree.
Example:
?- cbal_tree(4,T).
T = t(x, t(x, nil, nil), t(x, nil, t(x, nil, nil))) ;
T = t(x, t(x, nil, nil), t(x, t(x, nil, nil), nil)) ;
etc......No
Then use this predicate to test the solution of the problem P56.
Example:
?- test_symmetric([5,3,18,1,4,12,21]).
Yes
?- test_symmetric([3,2,5,7,1]).
No
How many such trees are there with 57 nodes? Investigate about how many solutions there are for a
given number of nodes? What if the number is even? Write an appropriate predicate.
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 9 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
Write a predicate hbal_tree/2 to construct height-balanced binary trees for a given height. The predicate
should generate all solutions via backtracking. Put the letter 'x' as information into all nodes of the tree.
Example:
?- hbal_tree(3,T).
T = t(x, t(x, t(x, nil, nil), t(x, nil, nil)), t(x, t(x, nil, nil), t(x, nil, nil))) ;
T = t(x, t(x, t(x, nil, nil), t(x, nil, nil)), t(x, t(x, nil, nil), nil)) ;
etc......No
P60 (**) Construct height-balanced binary trees with a given number of nodes
Consider a height-balanced binary tree of height H. What is the maximum number of nodes it can
contain?
Clearly, MaxN = 2**H - 1. However, what is the minimum number MinN? This question is more
difficult. Try to find a recursive statement and turn it into a predicate minNodes/2 defined as follwos:
On the other hand, we might ask: what is the maximum height H a height-balanced binary tree with N
nodes can have?
Now, we can attack the main problem: construct all the height-balanced binary trees with a given nuber
of nodes.
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 10 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
Using atlevel/3 it is easy to construct a predicate levelorder/2 which creates the level-order sequence of
the nodes. However, there are more efficient ways to do that.
Particularly, complete binary trees are used as data structures (or addressing schemes) for heaps.
We can assign an address number to each node in a complete binary tree by enumerating the nodes in
levelorder, starting at the root with number 1. In doing so, we realize that for every node X with address
A the following property holds: The address of X's left and right successors are 2*A and 2*A+1,
respectively, supposed the successors do exist. This fact can be used to elegantly construct a complete
binary tree structure. Write a predicate complete_binary_tree/2 with the following specification:
In order to store the position of the nodes, we extend the Prolog term representing a node (and its
successors) as follows:
% layout_binary_tree(T,PT) :- PT is the "positioned" binary tree obtained from the binary tree T. (+,?)
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 11 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
Use the same conventions as in problem P64 and P65 and test your predicate in an appropriate way.
Note: This is a difficult problem. Don't give up too early!
a(b(d,e),c(,f(g,)))
b) Write the same predicate tree_string/2 using difference lists and a single predicate tree_dlist/2 which
does the conversion between a tree and a difference list in both directions.
For simplicity, suppose the information in the nodes is a single letter and there are no spaces in the string.
a) Write predicates preorder/2 and inorder/2 that construct the preorder and inorder sequence of a given
binary tree, respectively. The results should be atoms, e.g. 'abdecfg' for the preorder sequence of the
example in problem P67.
b) Can you use preorder/2 from problem part a) in the reverse direction; i.e. given a preorder sequence,
construct a corresponding tree? If not, make the necessary arrangements.
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 12 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
c) If both the preorder sequence and the inorder sequence of the nodes of a binary tree are given, then the
tree is determined unambiguously. Write a predicate pre_in_tree/3 that does the job.
d) Solve problems a) to c) using difference lists. Cool! Use the predefined predicate time/1 to compare
the solutions.
What happens if the same character appears in more than one node. Try for instance
pre_in_tree(aba,baa,T).
Multiway Trees
A multiway tree is composed of a root element and a (possibly empty) set of successors which are
multiway trees themselves. A multiway tree is never empty. The set of successor trees is sometimes called
a forest.
In Prolog we represent a multiway tree by a term t(X,F), where X denotes the root
node and F denotes the forest of successor trees (a Prolog list). The example tree
depicted opposite is therefore represented by the following Prolog term:
T = t(a,[t(f,[t(g,[])]),t(c,[]),t(b,[t(d,[]),t(e,[])])])
Write another version of the predicate that allows for a flow pattern (o,i).
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 13 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
Define the syntax of the string and write a predicate tree(String,Tree) to construct the Tree when the
String is given. Work with atoms (instead of strings). Make your predicate work in both directions.
P72 (*) Construct the bottom-up order sequence of the tree nodes
Write a predicate bottom_up(Tree,Seq) which constructs the bottom-up sequence of the nodes of the
multiway tree Tree. Seq should be a Prolog list. What happens if you run your predicate backwords?
The following pictures show how multiway tree structures are represented in Lisp.
Note that in the "lispy" notation a node with successors (children) in the tree is always the first element
in a list, followed by its children. The "lispy" representation of a multiway tree is a sequence of atoms
and parentheses '(' and ')', which we shall collectively call "tokens". We can represent this sequence of
tokens as a Prolog list; e.g. the lispy expression (a (b c)) could be represented as the Prolog list ['(', a, '(',
b, c, ')', ')']. Write a predicate tree_ltl(T,LTL) which constructs the "lispy token list" LTL if the tree is
given as term T in the usual Prolog notation.
Example:
?- tree_ltl(t(a,[t(b,[]),t(c,[])]),LTL).
LTL = ['(', a, '(', b, c, ')', ')']
As a second, even more interesting exercise try to rewrite tree_ltl/2 in a way that the inverse conversion
is also possible: Given the list LTL, construct the Prolog tree T. Use difference lists.
Graphs
A graph is defined as a set of nodes and a set of edges, where each edge is a pair of nodes.
There are several ways to represent graphs in Prolog. One method is to represent each edge separately as one
clause (fact). In this form, the graph depicted below is represented as the following predicate:
edge(h,g).
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 14 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
edge(k,f).
edge(f,b).
...
We call this graph-term form. Note, that the lists are kept sorted, they are really sets, without duplicated
elements. Each edge appears only once in the edge list; i.e. an edge from a node x to another node y is
represented as e(x,y), the term e(y,x) is not present. The graph-term form is our default representation. In
SWI-Prolog there are predefined predicates to work with sets.
A third representation method is to associate with each node the set of nodes that are adjacent to that node. We
call this the adjacency-list form. In our example:
The representations we introduced so far are Prolog terms and therefore well suited for automated processing,
but their syntax is not very user-friendly. Typing the terms by hand is cumbersome and error-prone. We can
define a more compact and "human-friendly" notation as follows: A graph is represented by a list of atoms and
terms of the type X-Y (i.e. functor '-' and arity 2). The atoms stand for isolated nodes, the X-Y terms describe
edges. If an X appears as an endpoint of an edge, it is automatically defined as a node. Our example could be
written as:
[b-c, f-c, g-h, d, f-b, k-f, h-g]
We call this the human-friendly form. As the example shows, the list does not have to be sorted and may even
contain the same edge multiple times. Notice the isolated node d. (Actually, isolated nodes do not even have to
be atoms in the Prolog sense, they can be compound terms, as in d(3.75,blue) instead of d in the example).
When the edges are directed we call them arcs. These are represented by ordered
pairs. Such a graph is called directed graph. To represent a directed graph, the forms
discussed above are slightly modified. The example graph opposite is represented as
follows:
Arc-clause form
arc(s,u).
arc(u,r).
...
Graph-term form
digraph([r,s,t,u,v],[a(s,r),a(s,u),a(u,r),a(u,s),a(v,u)])
Adjacency-list form
[n(r,[]),n(s,[r,u]),n(t,[]),n(u,[r]),n(v,[u])]
Note that the adjacency-list does not have the information on whether it is a graph or a digraph.
Human-friendly form
[s > r, t, u > r, s > u, u > s, v > u]
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 15 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
Finally, graphs and digraphs may have additional information attached to nodes and edges (arcs). For the
nodes, this is no problem, as we can easily replace the single character identifiers with arbitrary compound
terms, such as city('London',4711) . On the other hand, for edges we have to extend our notation. Graphs
with additional information attached to edges are called labelled graphs.
Arc-clause form
arc(m,q,7).
arc(p,q,9).
arc(p,m,5).
Graph-term form
digraph([k,m,p,q],[a(m,p,7),a(p,m,5),a(p,q,9)])
Adjacency-list form
[n(k,[]),n(m,[q/7]),n(p,[m/5,q/9]),n(q,[])]
Notice how the edge information has been packed into a term with functor '/' and arity 2, together with
the corresponding node.
Human-friendly form
[p>q/9, m>q/7, k, p>m/5]
The notation for labelled graphs can also be used for so-called multi-graphs, where more than one edge (or
arc) are allowed between two given nodes.
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 16 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
Write a predicate that determines whether two graphs are isomorphic. Hint: Use an open-ended list to
represent the function f.
b) Write a predicate that generates a list of all nodes of a graph sorted according to decreasing degree.
c) Use Welch-Powell's algorithm to paint the nodes of a graph in such a way that adjacent nodes have
different colors.
Miscellaneous Problems
P90 (**) Eight queens problem
This is a classical problem in computer science. The objective is to place eight queens on a chessboard so
that no two queens are attacking each other; i.e., no two queens are in the same row, the same column, or
on the same diagonal.
Hint: Represent the positions of the queens as a list of numbers 1..N. Example: [4,2,7,3,6,8,5,1] means
that the queen in the first column is in row 4, the queen in the second column is in row 2, etc. Use the
generate-and-test paradigm.
Hints: Represent the squares by pairs of their coordinates of the form X/Y, where both X and Y are
integers between 1 and N. (Note that '/' is just a convenient functor, not division!) Define the relation
jump(N,X/Y,U/V) to express the fact that a knight can jump from X/Y to U/V on a NxN chessboard.
And finally, represent the solution of our problem as a list of N*N knight positions (the knight's tour).
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 17 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
Anyway the puzzle goes like this: Given a tree with N nodes (and hence N-1 edges). Find a way to
enumerate the nodes from 1 to N and, accordingly, the edges from 1 to N-1 in such a way, that for each
edge K the difference of its node numbers equals to K. The conjecture is that this is always possible.
For small trees the problem is easy to solve by hand. However, for larger trees, and 14 is already very
large, it is extremely difficult to find a solution. And remember, we don't know for sure whether there is
always a solution!
Write a predicate that calculates a numbering scheme for a given tree. What is the solution for the larger
tree pictured above?
. . 4 | 8 . . | . 1 7 9 3 4 | 8 2 5 | 6 1 7
| | | |
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 18 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
6 7
. | 9 . . | . . . 6 7
2 | 9 1 4 | 8 5 3
| | | |
5 . 8 | . 3 . | . . 4 5 1 8 | 6 3 7 | 9 2 4
--------+---------+-------- --------+---------+--------
3 . . | 7 4 . | 1 . . 3 2 5 | 7 4 8 | 1 6 9
| | | |
. 6 9 | . . . | 7 8 . 4 6 9 | 1 5 3 | 7 8 2
| | | |
. . 1 | . 6 9 | . . 5 7 8 1 | 2 6 9 | 4 3 5
--------+---------+-------- --------+---------+--------
1 . . | . 8 . | 3 . 6 1 9 7 | 5 8 2 | 3 4 6
| | | |
. . . | . . 6 | . 9 1 8 5 3 | 4 7 6 | 2 9 1
| | | |
2 4 . | . . 1 | 5 . . 2 4 6 | 3 9 1 | 5 7 8
Every spot in the puzzle belongs to a (horizontal) row and a (vertical) column, as well as to one single
3x3 square (which we call "square" for short). At the beginning, some of the spots carry a single-digit
number between 1 and 9. The problem is to fill the missing spots with digits in such a way that every
number between 1 and 9 appears exactly once in each row, in each column, and in each square.
The puzzle goes like this: Essentially, each row and column of a rectangular bitmap is annotated with the
respective lengths of its distinct strings of occupied cells. The person who solves the puzzle must
complete the bitmap given only these lengths.
|_|_|_|_|_|_|_|_| 3 |_|X|X|X|_|_|_|_| 3
|_|_|_|_|_|_|_|_| 2 1 |X|X|_|X|_|_|_|_| 2 1
|_|_|_|_|_|_|_|_| 3 2 |_|X|X|X|_|_|X|X| 3 2
|_|_|_|_|_|_|_|_| 2 2 |_|_|X|X|_|_|X|X| 2 2
|_|_|_|_|_|_|_|_| 6 |_|_|X|X|X|X|X|X| 6
|_|_|_|_|_|_|_|_| 1 5 |X|_|X|X|X|X|X|_| 1 5
|_|_|_|_|_|_|_|_| 6 |X|X|X|X|X|X|_|_| 6
|_|_|_|_|_|_|_|_| 1 |_|_|_|_|X|_|_|_| 1
|_|_|_|_|_|_|_|_| 2 |_|_|_|X|X|_|_|_| 2
1 3 1 7 5 3 4 3 1 3 1 7 5 3 4 3
2 1 5 1 2 1 5 1
For the example above, the problem can be stated as the two lists
[[3],[2,1],[3,2],[2,2],[6],[1,5],[6],[1],[2]] and [[1,2],[3,1],[1,5],[7,1],[5],[3],[4],[3]] which give the "solid"
lengths of the rows and columns, top-to-bottom and left-to-right, respectively. Published puzzles are
larger than this example, e.g. 25 x 20, and apparently always have unique solutions.
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 19 of 20
P-99: Ninety-Nine Prolog Problems 03/05/2006 11:42 AM
Words are strings (character lists) of at least two characters. A horizontal or vertical sequence of character
places in the crossword puzzle framework is called a site. Our problem is to find a compatible way of
placing words onto sites.
Hints: (1) The problem is not easy. You will need some time to thoroughly understand it. So, don't give
up too early! And remember that the objective is a clean solution, not just a quick-and-dirty hack!
(2) Reading the data file is a tricky problem for which a solution is provided in the file p99-readfile.pl.
Use the predicate read_lines/2.
(3) For efficiency reasons it is important, at least for larger puzzles, to sort the words and the sites in a
particular order. For this part of the problem, the solution of P28 may be very helpful.
http://www.hta-bi.bfh.ch/~hew/informatik3/prolog/p-99/ Page 20 of 20