Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aorandexiaohai/13c153ff7c768933f5ac514b92a3550c to your computer and use it in GitHub Desktop.
Save aorandexiaohai/13c153ff7c768933f5ac514b92a3550c to your computer and use it in GitHub Desktop.
Ninety Nine Scheme Problems (30 answered)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Quatre-vingts-dix-neuf problèmes en Scheme ;
; KEDJI Komlan Akpédjé <eric.kedji@gmail.com> ;
; http://erickedji.wordpress.com/ ;
; Elève ingénieur à l'ENSIAS ;
; ;
; Inspiré de 'Ninety-nine Lisp Problems' ;
; (www.ic.unicamp.br/~meidanis/courses/ ;
; mc336/2006s2/funcional/ ;
; L-99_Ninety-Nine_Lisp_Problems.html) ;
; lui-même inspiré d'une liste de problèmes ;
; en Prolog par werner.hett@hti.bfh.ch ;
; ;
; Les étoiles après les numéros de problèmes ;
; indiquent le niveau de difficulté. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Manipulation de listes ;
; ;
; Ces exercices peut être résolus plus élégamment avec ;
; des constructions du style map/filter/foldr/foldl ... ;
; Seulement, le but est de permettre au débutant de se ;
; familiariser avec la manipulation de listes. Il s'agit ;
; d'une capacité fondamentale pour écrire quoi que ce ;
; soit en Scheme. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P01 (*) Trouver la dernière cellule d'une liste.
; Exemple:
; * (my-last '(a b c d))
; (d)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; La dernière cellule d'une liste à 1|0 élément est la liste elle même.
; La dernière cellule d'une liste à plus d'un élément est identique à
; la dernière cellule de son cdr
(define (my-last liste)
(if (or (null? liste)
(null? (cdr liste)))
liste
(my-last (cdr liste))))
;(my-last '(a b c d))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P02 (*) Trouver l'avant dernière cellule d'une liste.
; Exemple:
; * (my-but-last '(a b c d))
; (c d)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (my-but-last liste)
(if (or (null? liste)
(null? (cdr liste))
(null? (cddr liste)))
liste
(my-but-last (cdr liste))))
;(my-but-last '(a b c d))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P03 (*) Trouver le Kieme élément d'une liste.
; Le premier élément de la liste porte le numéro 1
; Exemple:
; * (element-at '(a b c d e) 3)
; c
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; L'élément à la position 1 dans une liste est le car
; L'élément à la position k dans une liste est à la position k-1
; dans son cdr
(define (element-at liste numero)
(if (= 1 numero)
(car liste)
(element-at (cdr liste) (- numero 1))))
;(element-at '(a b c d e) 3)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P04 (*) Trouver le nombre d'éléments d'une liste.
; * (number-of-elements '(1 2 3 4))
; 4
; * (number-of-elements '(1 (2 3) 4))
; 3
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (number-of-elements liste)
(if (null? liste)
0
(+ 1 (number-of-elements (cdr liste)))))
;(number-of-elements '(a b h j (k t) l l j))
; Version iterative (c'est une technique générale pour
; transformer la récursivité non-terminale en récusivité
; terminale (tail-recursion))
; Au lieu de laisser les appels récursifs construire le résultat,
; on accumule explicitement le résultat dans un paramètre.
; Tout comme les boucles classiques dans les langages impératifs.
(define (number-of-elements2 liste)
(define (noe-iter reste-liste count)
(if (null? reste-liste)
count
(noe-iter (cdr reste-liste) (+ 1 count))))
(noe-iter liste 0))
;(number-of-elements2 '(a b (c ge f) d e))
; compter le nombre d'atomes (ie le nombre
; d'éléments qui ne sont pas des paires)
; * (number-of-atoms '(1 (2 3) 4)
; 4
; * (number-of-atoms '(1 (2 . 3) 4)
; 4
; * (number-of-atoms '(1 2 3 (4 5 6 . 7) 8))
; 8
(define (number-of-atoms liste)
(define (atom? x) (not (pair? x)))
(define (notnull? x) (not (null? x)))
(if (null? liste)
0
(let ((tete (car liste))
(queue (cdr liste)))
(+ (if (atom? tete)
1
(number-of-atoms tete))
(if (and (atom? queue) (notnull? queue)) ; une paire: (a . b)
1
(number-of-atoms (cdr liste)))))))
;(number-of-atoms '(a b h j (k t) l l (e (e) (a b) j))) ; => 13
;(number-of-atoms '(1 2 (3 4 . 5) 6)) ; => 6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P05 (*) Inverser l'ordre des éléments d'une liste.
; * (my-reverse '(1 2 3))
; (3 2 1)
; * (my-reverse '(1 (2 3) 4))
; (4 (2 3) 1)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (my-reverse liste)
(if (null? liste)
'()
(append (my-reverse (cdr liste))
(list (car liste)))))
; par accumulation
; - copier en tête de la liste source
; - coller en tête de la liste cible
(define (my-reverse2 liste)
(define (mr-iter reste-liste result)
(if (null? reste-liste)
result
(mr-iter (cdr reste-liste)
(cons (car reste-liste) result))))
(mr-iter liste '()))
;(my-reverse '(s ff k h (k l) l t))
;(my-reverse2 '(s ff k h (k l) l t))
; deep-reverse: inverser l'ordre dans les sous-listes
; aussi (récursivement). Toutes les listes sont garanties
; se terminer par nil (ie par de dotted pair du style (... a . b))
; * (deep-reverse '(a (b c) d))
; (d (c b) a)
(define (deep-reverse liste)
(cond ((null? liste) '())
((null? (cdr liste)) (if (not (list? (car liste)))
liste
(list (deep-reverse (car liste)))))
(else (append (deep-reverse (cdr liste))
(deep-reverse (list (car liste)))))))
;(deep-reverse '(1 2 (3 4) 5 (6 (7 8 (9)) 10) 11))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P06 (*) Déterminer si une liste est palindrome.
; Un palindrome peut être lu aussi bien d'avant en arrière que
; d'arrière en avant, comme (x a m a x).
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (is-palindrome liste)
(equal? liste (reverse liste)))
;(is-palindrome '(x (a b) a m a (a b) x))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P07 (**) Aplatir une structure de listes imbriquées.
; Transformer une liste, contenant potentiellement des listes
; comme éléments en une liste 'plate', en remplaçant chaque
; liste par ses éléments (récursivement).
;
; Exemple:
; * (my-flatten '(a (b (c d) e)))
; (a b c d e)
;
; Indication: Utiliser les fonctions prédéfinies list et append.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (my-flatten liste)
(if (null? liste)
'()
(append (if (list? (car liste))
(my-flatten (car liste))
(list (car liste)))
(my-flatten (cdr liste)))))
;(my-flatten '(a (b (c d) e)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P08 (**) Eliminer les répétitions consécutives dans une liste.
; Si une liste contient des éléments répétés (consécutifs),
; les remplacer par une seule copie. L'ordre des éléments
; ne doit pas être modifié.
;
; Exemple:
; * (compress '(a a a a b c c a a d e e e e))
; (a b c a d e)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (compress liste)
(if (or (null? liste) (null? (cdr liste)))
liste
(let ((compressed-cdr (compress (cdr liste))))
(if (equal? (car liste) (car compressed-cdr))
compressed-cdr
(cons (car liste) compressed-cdr)))))
;(compress '(a a a a b (c c) (c c) (a b) (a b) a a d e e e e))
; Version itérative
(define (iterative-compress liste)
(define (ic-helper reste-liste reversed-result)
(if (null? reste-liste)
reversed-result
(if (equal? (car reste-liste) (car reversed-result))
(ic-helper (cdr reste-liste) reversed-result)
(ic-helper (cdr reste-liste)
(cons (car reste-liste) reversed-result)))))
(reverse (ic-helper (cdr liste) (list (car liste)))))
;(iterative-compress '(a a a a b (c c) (a b) (a b) a a d e e e e))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P09 (**) Regrouper les répétitions consécutives d'éléments d'une
; liste en des sous-listes
;
; Exemple:
; * (pack '(a a a a b c c a a d e e e e))
; ((a a a a) (b) (c c) (a a) (d) (e e e e))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (pack liste)
(if (null? liste)
(list liste) ; cas trivial
(let ((packed-cdr (pack (cdr liste))))
(cond ((null? (car packed-cdr)) ; packed-cdr provient du cas trivial
(list (list (car liste))))
((equal? (car liste) (caar packed-cdr)) ; même élément: ajout à la sous-liste
(cons (cons (car liste)
(car packed-cdr))
(cdr packed-cdr)))
(else (cons (list (car liste)) ; nouvel élément: nouvelle sous-liste
packed-cdr))))))
;(pack '(a a a a b c c a a d e (e) (e) e))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P10 (*) Compression d'une liste par `run-length encoding'
; Utiliser le résultat du problème P09 to implémenter la
; technique de compression de données connue sous le nom
; de run-length encoding. Les répétitions consécutives
; d'éléments sont codés en listes de la forme (N E), où
; N est le nombre de répétitions de l'élément E.
;
; Exemple:
; * (encode '(a a a a b c c a a d e e e e))
; ((4 a) (1 b) (2 c) (2 a) (1 d)(4 e))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (encode liste)
(define (encode-helper packed-list)
(if (null? packed-list)
'()
(cons (list (length (car packed-list))
(caar packed-list))
(encode-helper (cdr packed-list)))))
(encode-helper (pack liste)))
;(encode '(a a a a b c c a a d e e e e))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P11 (*) Amélioration de run-length encoding.
; Modifier le résultat du problème P10, de telle sorte que les
; éléments non dupliqués soient tout simplement copiés. Seuls les
; éléments dupliqués sont mis sous la forme (N E).
;
; Exemple:
; * (encode-modified '(a a a a b c c a a d e e e e))
; ((4 a) b (2 c) (2 a) d (4 e))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (encode-modified liste)
(define (simplify encoded-list)
(if (null? encoded-list)
'()
(cons (if (= 1 (caar encoded-list))
(cadar encoded-list)
(car encoded-list))
(simplify (cdr encoded-list)))))
(simplify (encode liste)))
;(encode-modified '(a a a a b c c a a d e e e e))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P12 (**) Decoder une liste compressée par run-length encoding.
; Etant donné une liste compressée comme spécifié dans le
; le problème P11, retrouver la liste originale.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (decode-rl encoded-list)
(define (expand element count)
(if (= count 0)
'()
(cons element
(expand element (- count 1)))))
(cond ((null? encoded-list) '())
((not (list? (car encoded-list)))
(append (list (car encoded-list))
(decode-rl (cdr encoded-list))))
(else (append (expand (cadar encoded-list)
(caar encoded-list))
(decode-rl (cdr encoded-list))))))
;(decode-rl (encode-modified '(a a a a b c c a a d e e e e)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P13 (**) Compression d'une liste par run-length encoding
; (solution directe).
; Implémenter directement la compression par run-length encoding.
; En d'autres mots, ne pas créer explicitement les sous-listes
; contenant les répétitions, comme dans le problème P09, mais juste
; compter le nombre de répétition. Simplifier la liste résultante
; comme dans le problème P11, en remplaçant (1 X) par X.
;
; Exemple:
; * (encode-direct '(a a a a b c c a a d e e e e))
; ((4 a) b (2 c) (2 a) d (4 e))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (encode-direct list-to-encode)
(define (simplify liste)
(if (null? liste)
'()
(cons (if (= 1 (caar liste))
(cadar liste)
(car liste))
(simplify (cdr liste)))))
(define (encode-direct-helper liste)
(if (null? liste)
'() ; cas trivial
(let ((encoded-cdr (encode-direct-helper (cdr liste))))
(cond ((null? encoded-cdr) (list (list 1
(car liste)))) ; liste à un seul élément
((equal? (car liste)
(cadar encoded-cdr))
(cons (list (+ 1 (caar encoded-cdr)) ; ajout d'élément répété
(car liste))
(cdr encoded-cdr)))
(else (cons (list 1 ; nouvelle sous-liste
(car liste))
encoded-cdr))))))
(simplify (encode-direct-helper list-to-encode)))
;(encode-direct '(a a a a b c c a a d e e e e))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P14 (*) Dupliquer chaque éléments d'une liste.
; Exemple:
; * (dupli '(a b c c d))
; (a a b b c c c c d d)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (dupli liste)
(if (null? liste)
'()
(append (list (car liste)
(car liste))
(dupli (cdr liste)))))
;(dupli '(a b c c d))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P15 (**) Répliquer chaque élément d'une liste un nombre donné de fois.
; Exemple:
; * (repli '(a b c) 3)
; (a a a b b b c c c)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (repli liste count)
(define (replicate-element element how-many)
(if (= how-many 0)
'()
(cons element
(replicate-element element
(- how-many 1)))))
(if (null? liste)
'()
(append (replicate-element (car liste)
count)
(repli (cdr liste)
count))))
;(repli '(a b c) 9)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P16 (**) Supprimer chaque Nième élément d'une liste.
; Exemple:
; * (drop '(a b c d e f g h i k) 3)
; (a b d e g h k)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (drop liste position)
(define (drop-helper temp-list temp-position)
(cond ((null? temp-list) '())
((= 1 temp-position) (drop-helper (cdr temp-list)
position))
(else (cons (car temp-list)
(drop-helper (cdr temp-list)
(- temp-position 1))))))
(drop-helper liste position))
;(drop '(a b c d e f g h i k) 3)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P17 (*) Découper une liste en deux sous listes; la longueur de
; la première sous-liste est donnée.
; N'utiliser aucun prédicat prédéfini.
;
; Exemple:
; * (split '(a b c d e f g h i k) 3)
; ((a b c) (d e f g h i k))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (split liste first-part-length)
(if (= 0 first-part-length)
(list '() liste)
(let ((splited-cdr (split (cdr liste)
(- first-part-length 1))))
(list (cons (car liste)
(car splited-cdr))
(cadr splited-cdr)))))
;(split '(a b c d e f g h i k) 3)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P18 (**) Extraire une sous-liste d'une liste.
; Etant donné deux indices, I et K, la sous-liste est la liste
; contenant les éléments entre le Iième et le Kième élément
; (les deux limites incluses). Compter à partir de 1.
;
; Exemple:
; * (slice '(a b c d e f g h i k) 3 7)
; (c d e f g)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (slice liste start stop)
(car (split (cadr (split liste
(- start 1)))
(+ 1
(- stop start)))))
;(slice '(a b c d e f g h i k) 3 7)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P19 (**) Effectuer une rotation de liste de N positions vers la gauche.
;
; Exemples:
; * (rotate '(a b c d e f g h) 3)
; (d e f g h a b c)
;
; * (rotate '(a b c d e f g h) -2)
; (g h a b c d e f)
;
; Indication: Utiliser les fonctions prédéfinies length et append,
; et le résultat du problème P17.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (rotate liste arg)
(if (null? liste)
'()
(let ((splited-liste (split liste
(modulo arg
(length liste)))))
(append (cadr splited-liste)
(car splited-liste)))))
;(rotate '(a b c d e f g h) 3)
;(rotate '(a b c d e f g h) -2)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P20 (*) Supprimer le Kième élément d'une liste.
;
; Exemple:
; * (remove-at '(a b c d) 2)
; (a c d)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (remove-at liste place)
(if (= 1 place)
(cdr liste)
(cons (car liste)
(remove-at (cdr liste)
(- place 1)))))
;(remove-at '(a b c d) 2)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P21 (*) Inserer un élément à la Kième position dans une liste.
;
; Exemple:
; * (insert-at 'alfa '(a b c d) 2)
; (a alfa b c d)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (insert-at element liste place)
(if (= 1 place)
(cons element liste)
(cons (car liste)
(insert-at element
(cdr liste)
(- place 1)))))
;(insert-at 'alfa '(a b c d) 2)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P22 (*) Créer une liste contenant tous les entiers dans un
; intervalle donné.
;
; Exemple:
; * (range 4 9)
; (4 5 6 7 8 9)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (range start stop)
(if (= start stop)
(list stop)
(cons start
(range (+ start 1)
stop))))
;(range 4 9)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P23 (**) Extraire un nombre donné d'éléments d'une liste, de façon
; aléatoire.
; Exemple:
; * (rnd-select '(a b c d e f g h) 3)
; (e d a)
;
; Indication: Utiliser le générateur de nombres aléatoires
; prédéfini et la solution du problème P20.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (rnd-select liste how-many)
(define (my-random n) (+ 1 (random n))) ; 1 <= nombre aléatoire <= n
(if (= 0 how-many)
'()
(let ((place (my-random (length liste))))
(cons (element-at liste place)
(rnd-select (remove-at liste place)
(- how-many 1))))))
;(rnd-select '(a b c d e f g h) 3)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P24 (*) Loto: Sélectionner N différents nombres aléatoires
; dans l'ensemble 1..M.
; Exemple:
; * (lotto-select 6 49)
; (23 1 17 33 21 37)
;
; Indication: Combiner les solutions aux problèmes P22 et P23.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (lotto-select how-many upper-bound)
(rnd-select (range 1 upper-bound)
how-many))
;(lotto-select 6 49)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; P25 (*) Générer une permutation aléatoire des éléments d'une liste.
; Exemple:
; * (rnd-permu '(a b c d e f))
; (b a d c e f)
;
; Indication: Utiliser la solution du problème P23.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (rnd-permu liste)
(rnd-select liste
(length liste)))
;(rnd-permu '(a b c d e f))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P26 (**) Générer les différentes combinaisons de K objets distincts
; choisis dans une liste de N objets.
;
; De combien de façons peut-on former un commité de 3 personnes,
; choisies dans un groupe de 12 personnes?
; On sait tous qu'il y a C(12,3) = 220 possibilités (C(N,K) est
; le fameux coefficient du binôme). Pour des mathématiciens purs,
; le nombre de possibilités est à lui seul interessant. Ce que
; nous voulons, nous, c'est *lister* toutes les possibilités.
;
; Exemple:
; * (combinaison 3 '(a b c d e f))
; ((a b c) (a b d) (a b e) ... )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; il y a deux types de listes à k éléments:
; - ceux qui ne contiennent pas le car
; - ceux qui contiennent le car
; ces derniers sont obtenus en ajoutant le car à toutes
; les listes à k-1 éléments générés à partir du cdr.
; (ce genre de raisonnement permet de venir à bout de
; bon nombre de problèmes à caractère combinatoire)
(define (combinaison k liste)
(define (add-element x liste)
(if (null? liste)
'()
(cons (cons x (car liste))
(add-element x (cdr liste)))))
(cond ((or (= 0 k) (> k (length liste))) '(()))
((= k (length liste)) (list liste))
(else (append (combinaison k (cdr liste))
(add-element (car liste)
(combinaison (- k 1) (cdr liste)))))))
; (combinaison 3 '(a b c d e f))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P27 (**) Grouper les éléments d'un ensemble en des sous-ensembles
; disjoints.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; a) De combien de façons un groupe de 9 personnes peut-il
; travailler en sous-groupes de 2, 3 et 4 personnes?. Ecrire une
; fonction qui génère toutes les possibilités et les retourne dans
; une liste.
; Exemple:
; * (groups234 '(aldo beat carla david evi flip gary hugo ida))
; ( ( (aldo beat) (carla david evi) (flip gary hugo ida) )
; ... )
(define (groups234 liste)
(define (add-3-and-4-groups pairs)
(if (null? pairs)
'()
(append (add-group (car pairs)
(let ((remaining-seven-elements (complement (car pairs) liste)))
(complement-all (combinaison 3 remaining-seven-elements)
remaining-seven-elements)))
(add-3-and-4-groups (cdr pairs)))))
(define (add-group group partition)
(if (null? partition)
'()
(cons (cons group (car partition))
(add-group group (cdr partition)))))
(define (complement subset set)
(define (drop-element e set)
(cond ((null? set) '())
((equal? e (car set)) (cdr set))
(else (cons (car set) (drop-element e (cdr set))))))
(cond ((null? subset) set)
((member (car subset) set)
(complement (cdr subset) (drop-element (car subset) set)))
(else (complement (cdr subset) set))))
(define (complement-all combinations set)
(if (null? combinations)
'()
(let ((subset (car combinations)))
(cons (list subset
(complement subset set))
(complement-all (cdr combinations) set)))))
(add-3-and-4-groups (combinaison 2 liste)))
; (groups234 '(aldo beat carla david evi flip gary hugo ida))
; la solution précédente est affreusement moche. Seulement, la
; rendre jolie, c'est résoubre le b-). En général, tenter de résoudre
; directement des cas particuliers donne des solutions lourdes. Ne
; vous torturez pas à comprendre cette solution: ça ne vous apprendra
; pas grande chose.
; b) Généraliser la procédure précédente de manière à pouvoir
; spécifier une liste de tailles. Il est garanti que toutes les tailles
; sont positives et que leur somme est égale à la longueur de la liste.
;
; Exemple:
; * (group '(aldo beat carla david evi flip gary hugo ida) '(2 2 5))
; ( ( (aldo beat) (carla david) (evi flip gary hugo ida) )
; ... )
;
; Noter qu'on ne veut pas des permutations des membre d'un sous-groupe.
; En d'autres mots, ((aldo beat) ...) est identique à ((beat aldo) ...).
; Cependant, on fait la distinction entre ((aldo beat) (carla david) ...) et
; ((carla david) (aldo beat) ...).
;
; Vous pouvez vous renseigner sur ce problème dans un document sur
; l'arithmétique, sous le nom "coéfficients du multinôme"
(define (group liste tailles)
(define (group-helper set sizes)
(if (null? (cdr sizes))
(list (list set))
(add-subsets (combinaison (car sizes) set)
set
(cdr sizes))))
; pour une liste de tailles (2 3 4) par exemple, prendre un doublet
; (ie un élément de subsets), demander à group-helper de construire
; une partition en utilisant comme set le complémentaire du doublet
; et comme liste de tailles (3 4), et ajouter le doublet concerné
; en début de chaque groupe de cette partition.
(define (add-subsets subsets set sizes)
(if (null? subsets)
'()
(let* ((subset (car subsets))
(complementary-of-subset (complement subset set)))
(append (add-subgroup subset
(group-helper complementary-of-subset
sizes))
(add-subsets (cdr subsets)
set
sizes)))))
; une partition etant donnée, ajouter un sous-groupe à chacun de ses
; éléments. ie
; (add-subgroup (elle lui) (((luc jean) (chris pierre)) ...))
; -> (((elle lui) (luc jean) (chris pierre)) ...)
(define (add-subgroup subgroup partition)
(if (null? partition)
'()
(cons (cons subgroup (car partition))
(add-subgroup subgroup (cdr partition)))))
; la complémentation de la théorie des ensembles
(define (complement subset set)
(define (drop-element e set)
(cond ((null? set) '())
((equal? e (car set)) (cdr set))
(else (cons (car set) (drop-element e (cdr set))))))
(cond ((null? subset) set)
((member (car subset) set)
(complement (cdr subset) (drop-element (car subset) set)))
(else (complement (cdr subset) set))))
(group-helper liste tailles))
; (group '(aldo beat carla david evi flip gary hugo ida) '(2 2 5))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P28 (**) Tri d'une liste sur la base de la longueur de ses
; sous-listes.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; a) On suppose que les éléments de la liste sont eux-mêmes des
; listes. L'objectif est de trier les éléments suivant leur tailles.
; Par exemple, les plus courtes listes d'abord, les plus longues listes
; ensuite; ou vice-versa.
;
; Exemple:
; * (lsort '((a b c) (d e) (f g h) (d e) (i j k l) (m n) (o)))
; ((o) (d e) (d e) (m n) (a b c) (f g h) (i j k l))
(define (lsort liste)
(define (insert-element element sorted-list)
(cond ((null? sorted-list)
(list element))
((<= (length element) (length (car sorted-list)))
(cons element sorted-list))
(else (cons (car sorted-list)
(insert-element element (cdr sorted-list))))))
(define (lsort-helper liste)
(if (null? liste)
'()
(let ((sorted-cdr (lsort (cdr liste))))
(insert-element (car liste) sorted-cdr))))
(lsort-helper liste))
; (lsort '((a b c) (d e) (f g h) (d e) (i j k l) (m n) (o)))
; b) On suppose, une fois de plus, que les éléments de la liste
; sont eux-mêmes des listes. Seulement, cette fois-ci, on désire trier
; les éléments suivant leur fréquence. Par exemple, si l'ordre est
; ascendant, les listes avec des longueurs rares sont placées au début,
; celles ayant des longueurs plus féquentes suivent, ...
;
; Exemple:
; * (lfsort '((a b c) (d e) (f g h) (d e) (i j k l) (m n) (o)))
; ((i j k l) (o) (a b c) (f g h) (d e) (d e) (m n))
;
; Noter que dans l'exemple ci-dessus, les deux premières listes
; dans le résultat sont de longueur 4 et 1; chacune des deux longueurs
; apparaît une seule fois. La troisième et la quatrième liste sont de
; longueur 3 qui apparaît deux fois (il y a deux listes qui ont cette
; longueur). Et enfin, les trois dernières listes sont de longueur 2.
; C'est la longueur la plus fréquente.
(define (lfsort liste)
; on réutilise la procédure pack du problème 9, on redéfinissant
; l'égalité d'éléments comme étant l'égalitant des longueurs
; (les éléments sont des listes)
(define (pack liste)
(if (null? liste)
(list liste)
(let ((packed-cdr (pack (cdr liste))))
(cond ((null? (car packed-cdr))
(list (list (car liste))))
((equal? (length (car liste))
(length (caar packed-cdr)))
(cons (cons (car liste)
(car packed-cdr))
(cdr packed-cdr)))
(else (cons (list (car liste))
packed-cdr))))))
; procédure inverse de pack: (expand (pack liste)) ==> liste
(define (expand liste)
(if (null? liste)
'()
(append (car liste)
(expand (cdr liste)))))
(expand (lsort (pack (lsort liste))))) ; c'est pas joli ça?
; (lfsort '((a b c) (d e) (f g h) (d e) (i j k l) (m n) (o)))
; Les exercices qui suivent ne sont pas encore corrigés.
; Quand ma cervelle se mettra à se plaindre des
; System.out.println("foobar");
; je reviendrais m'abriter derrière les parenthèses, et je
; continuerais ...
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Arithmetic
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P31 (**) Determine whether a given integer number is prime.
; Exemple:
; * (is-prime 7)
; T
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P32 (**) Determine the greatest common divisor of two positive integer numbers.
; Use Euclid's algorithm.
; Exemple:
; * (gcd 36 63)
; 9
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P33 (*) Determine whether two positive integer numbers are coprime.
; Two numbers are coprime if their greatest common divisor equals 1.
; Exemple:
; * (coprime 35 64)
; T
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P34 (**) Calculate Euler's totient function phi(m).
; Euler's so-called totient function phi(m) is defined as the number of positive integers r (1 <= r < m) that are coprime to m.
;
; Exemple: m = 10: r = 1,3,7,9; thus phi(m) = 4. Note the special case: phi(1) = 1.
;
; * (totient-phi 10)
; 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).
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P35 (**) Determine the prime factors of a given positive integer.
; Construct a flat list containing the prime factors in ascending order.
; Exemple:
; * (prime-factors 315)
; (3 3 5 7)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P36 (**) Determine the prime factors of a given positive integer (2).
; Construct a list containing the prime factors and their multiplicity.
; Exemple:
; * (prime-factors-mult 315)
; ((3 2) (5 1) (7 1))
;
; Hint: The problem is similar to problem P13.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P37 (**) Calculate Euler's totient function phi(m) (improved).
; See problem P34 for the definition of Euler's totient function. If the list of the prime factors of a number m is known in the form of problem P36 then the function phi(m) can be efficiently calculated as follows: Let ((p1 m1) (p2 m2) (p3 m3) ...) be the list of prime factors (and their multiplicities) of a given number m. Then phi(m) can be calculated with the following formula:
;
; phi(m) = (p1 - 1) * p1 ** (m1 - 1) + (p2 - 1) * p2 ** (m2 - 1) + (p3 - 1) * p3 ** (m3 - 1) + ...
;
; Note that a ** b stands for the b'th power of a.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 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 Exemple.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P39 (*) A list of prime numbers.
; Given a range of integers by its lower and upper limit, construct a list of all prime numbers in that range.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P40 (**) Goldbach's conjecture.
; Goldbach's conjecture says that every positive even number greater than 2 is the sum of two prime numbers. Exemple: 28 = 5 + 23. It is one of the most famous facts in number theory that has not been proved to be correct in the general case. It has been numerically confirmed up to very large numbers (much larger than we can go with our Prolog system). Write a predicate to find the two prime numbers that sum up to a given even integer.
;
; Exemple:
; * (goldbach 28)
; (5 23)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P41 (**) A list of Goldbach compositions.
; Given a range of integers by its lower and upper limit, print a list of all even numbers and their Goldbach composition.
;
; Exemple:
; * (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.
;
; Exemple (for a print limit of 50):
; * (goldbach-list 1 2000 50)
; 992 = 73 + 919
; 1382 = 61 + 1321
; 1856 = 67 + 1789
; 1928 = 61 + 1867
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Logic and Codes
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P46 (**) Truth tables for logical expressions.
; Define predicates and/2, or/2, nand/2, nor/2, xor/2, impl/2 and equ/2 (for logical equivalence) which succeed or fail according to the result of their respective operations; e.g. and(A,B) will succeed, if and only if both A and B succeed. Note that A and B can be Prolog goals (not only the constants true and fail).
;
; A logical expression in two variables can then be written in prefix notation, as in the following Exemple: 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.
;
; Exemple:
; * table(A,B,and(A,or(A,B))).
; true true true
; true fail true
; fail true fail
; fail fail fail
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P47 (*) Truth tables for logical expressions (2).
; Continue problem P46 by defining and/2, or/2, etc as being operators. This allows to write the logical expression in the more natural way, as in the Exemple: A and (A or not B). Define operator precedence as usual; i.e. as in Java.
;
; Exemple:
; * table(A,B, A and (A or not B)).
; true true true
; true fail true
; fail true fail
; fail fail fail
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P48 (**) Truth tables for logical expressions (3).
; Generalize problem P47 in such a way that the logical expression may contain any number of logical 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.
;
; Exemple:
; * 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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P49 (**) Gray code.
; An n-bit Gray code is a sequence of n-bit strings constructed according to certain rules. For Exemple,
; n = 1: C(1) = ['0','1'].
; n = 2: C(2) = ['00','01','11','10'].
; n = 3: C(3) = ['000','001','011','010',?110?,?111?,?101?,?100?].
;
; Find out the construction rules and write a predicate with the following specification:
;
; % gray(N,C) :- C is the N-bit Gray code
;
; Can you apply the method of "result caching" in order to make the predicate more efficient, when it is to be used repeatedly?
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P50 (***) Huffman code.
; First of all, consult a good book on discrete mathematics or algorithms for a detailed description of Huffman codes!
;
; We suppose a set of symbols with their frequencies, given as a list of fr(S,F) terms. Exemple: [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 Exemple, 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:
;
; % huffman(Fs,Hs) :- Hs is the Huffman code table for the frequency table Fs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 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 Lisp we represent the empty tree by 'nil' and the non-empty tree by the list (X L R), where X denotes the root node and L and R denote the left and right subtree, respectively. The Exemple tree depicted opposite is therefore represented by the following list:
;
; (a (b (d nil nil) (e nil nil)) (c nil (f (g nil nil) nil)))
;
; Other Exemples are a binary tree that consists of a root node only:
;
; (a nil nil) or an empty binary tree: nil.
;
; You can check your predicates using these Exemple trees. They are given as test cases in p54.lisp.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P54A (*) Check whether a given term represents a binary tree
; Write a predicate istree which returns true if and only if its argument is a list representing a binary tree.
; Exemple:
; * (istree (a (b nil nil) nil))
; T
; * (istree (a (b nil nil)))
; NIL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; P55 (**) Construct completely balanced binary trees
; In a completely balanced binary tree, the following property holds for every node: The number of nodes in its left subtree and the number of nodes in its right subtree are almost equal, which means their difference is not greater than one.
;
; Write a function cbal-tree 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.
; Exemple:
; * 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
;
; P56 (**) Symmetric binary trees
; Let us call a binary tree symmetric if you can draw a vertical line through the root node and then the right subtree is the mirror image of the left subtree. Write a predicate symmetric/1 to check whether a given binary tree is symmetric. Hint: Write a predicate mirror/2 first to check whether one tree is the mirror image of another. We are only interested in the structure, not in the contents of the nodes.
;
; P57 (**) Binary search trees (dictionaries)
; Use the predicate add/3, developed in chapter 4 of the course, to write a predicate to construct a binary search tree from a list of integer numbers.
; Exemple:
; * construct([3,2,5,7,1],T).
; T = t(3, t(2, t(1, nil, nil), nil), t(5, nil, t(7, nil, nil)))
;
; Then use this predicate to test the solution of the problem P56.
; Exemple:
; * test-symmetric([5,3,18,1,4,12,21]).
; Yes
; * test-symmetric([3,2,5,7,1]).
; No
;
; P58 (**) Generate-and-test paradigm
; Apply the generate-and-test paradigm to construct all symmetric, completely balanced binary trees with a given number of nodes. Exemple:
; * sym-cbal-trees(5,Ts).
; Ts = [t(x, t(x, nil, t(x, nil, nil)), t(x, t(x, nil, nil), nil)), t(x, t(x, t(x, nil, nil), nil), t(x, nil, t(x, nil, nil)))]
;
; 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.
;
; P59 (**) Construct height-balanced binary trees
; In a height-balanced binary tree, the following property holds for every node: The height of its left subtree and the height of its right subtree are almost equal, which means their difference is not greater than one.
;
; 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.
; Exemple:
; * 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:
;
; % minNodes(H,N) :- N is the minimum number of nodes in a height-balanced binary tree of height H.
; (integer,integer), (+,?)
;
; On the other hand, we might ask: what is the maximum height H a height-balanced binary tree with N nodes can have?
;
; % maxHeight(N,H) :- H is the maximum height of a height-balanced binary tree with N nodes
; (integer,integer), (+,?)
;
; Now, we can attack the main problem: construct all the height-balanced binary trees with a given nuber of nodes.
;
; % hbal-tree-nodes(N,T) :- T is a height-balanced binary tree with N nodes.
;
; Find out how many height-balanced trees exist for N = 15.
;
; P61 (*) Count the leaves of a binary tree
; A leaf is a node with no successors. Write a predicate count-leaves/2 to count them.
;
; % count-leaves(T,N) :- the binary tree T has N leaves
;
; P61A (*) Collect the leaves of a binary tree in a list
; A leaf is a node with no successors. Write a predicate leaves/2 to collect them in a list.
;
; % leaves(T,S) :- S is the list of all leaves of the binary tree T
;
; P62 (*) Collect the internal nodes of a binary tree in a list
; An internal node of a binary tree has either one or two non-empty successors. Write a predicate internals/2 to collect them in a list.
;
; % internals(T,S) :- S is the list of internal nodes of the binary tree T.
;
; P62B (*) Collect the nodes at a given level in a list
; A node of a binary tree is at level N if the path from the root to the node has length N-1. The root node is at level 1. Write a predicate atlevel/3 to collect all nodes at a given level in a list.
;
; % atlevel(T,L,S) :- S is the list of nodes of the binary tree T at level L
;
; 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.
;
; P63 (**) Construct a complete binary tree
; A complete binary tree with height H is defined as follows: The levels 1,2,3,...,H-1 contain the maximum number of nodes (i.e 2**(i-1) at the level i, note that we start counting the levels from 1 at the root). In level H, which may contain less than the maximum possible number of nodes, all the nodes are "left-adjusted". This means that in a levelorder tree traversal all internal nodes come first, the leaves come second, and empty successors (the nil's which are not really nodes!) come last.
;
; 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:
;
; % complete-binary-tree(N,T) :- T is a complete binary tree with N nodes. (+,?)
;
; Test your predicate in an appropriate way.
;
; P64 (**) Layout a binary tree (1)
; Given a binary tree as the usual Prolog term t(X,L,R) (or nil). As a preparation for drawing the tree, a layout algorithm is required to determine the position of each node in a rectangular grid. Several layout methods are conceivable, one of them is shown in the illustration below.
;
; In this layout strategy, the position of a node v is obtained by the following two rules:
;
; * x(v) is equal to the position of the node v in the inorder sequence
; * y(v) is equal to the depth of the node v in the tree
;
;
;
; In order to store the position of the nodes, we extend the Prolog term representing a node (and its successors) as follows:
;
; % nil represents the empty tree (as usual)
; % t(W,X,Y,L,R) represents a (non-empty) binary tree with root W "positioned" at (X,Y), and subtrees L and R
;
; Write a predicate layout-binary-tree/2 with the following specification:
;
; % layout-binary-tree(T,PT) :- PT is the "positioned" binary tree obtained from the binary tree T. (+,?)
;
; Test your predicate in an appropriate way.
;
; P65 (**) Layout a binary tree (2)
; An alternative layout method is depicted in the illustration opposite. Find out the rules and write the corresponding Prolog predicate. Hint: On a given level, the horizontal distance between neighboring nodes is constant.
;
; Use the same conventions as in problem P64 and test your predicate in an appropriate way.
;
; P66 (***) Layout a binary tree (3)
; Yet another layout strategy is shown in the illustration opposite. The method yields a very compact layout while maintaining a certain symmetry in every node. Find out the rules and write the corresponding Prolog predicate. Hint: Consider the horizontal distance between a node and its successor nodes. How tight can you pack together two subtrees to construct the combined binary tree?
;
; 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!
;
; Which layout do you like most?
;
; P67 (**) A string representation of binary trees
;
; Somebody represents binary trees as strings of the following type (see Exemple opposite):
;
; a(b(d,e),c(,f(g,)))
;
; a) Write a Prolog predicate which generates this string representation, if the tree is given as usual (as nil or t(X,L,R) term). Then write a predicate which does this inverse; i.e. given the string representation, construct the tree in the usual form. Finally, combine the two predicates in a single predicate tree-string/2 which can be used in both directions.
;
; 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.
;
; P68 (**) Preorder and inorder sequences of binary trees
; We consider binary trees with nodes that are identified by single lower-case letters, as in the Exemple of problem P67.
;
; 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 Exemple 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.
;
; 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).
;
; P69 (**) Dotstring representation of binary trees
; We consider again binary trees with nodes that are identified by single lower-case letters, as in the Exemple of problem P67. Such a tree can be represented by the preorder sequence of its nodes in which dots (.) are inserted where an empty subtree (nil) is encountered during the tree traversal. For Exemple, the tree shown in problem P67 is represented as 'abd..e..c.fg...'. First, try to establish a syntax (BNF or syntax diagrams) and then write a predicate tree-dotstring/2 which does the conversion in both directions. Use difference lists.
;
; 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 Exemple 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,[])])])
;
;
; P70B (*) Check whether a given term represents a multiway tree
; Write a predicate istree/1 which succeeds if and only if its argument is a Prolog term representing a multiway tree.
; Exemple:
; * istree(t(a,[t(f,[t(g,[])]),t(c,[]),t(b,[t(d,[]),t(e,[])])])).
; Yes
;
; P70C (*) Count the nodes of a multiway tree
; Write a predicate nnodes/1 which counts the nodes of a given multiway tree.
; Exemple:
; * nnodes(t(a,[t(f,[])]),N).
; N = 2
;
; Write another version of the predicate that allows for a flow pattern (o,i).
;
; P70 (**) Tree construction from a node string
; We suppose that the nodes of a multiway tree contain single characters. In the depth-first order sequence of its nodes, a special character ^ has been inserted whenever, during the tree traversal, the move is a backtrack to the previous level.
;
; By this rule, the tree in the figure opposite is represented as: afg^^c^bd^e^^^
;
; 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.
;
; P71 (*) Determine the internal path length of a tree
; We define the internal path length of a multiway tree as the total sum of the path lengths from the root to all nodes of the tree. By this definition, the tree in the figure of problem P70 has an internal path length of 9. Write a predicate ipl(Tree,IPL) for the flow pattern (+,-).
;
; 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?
;
; P73 (**) Lisp-like tree representation
; There is a particular notation for multiway trees in Lisp. Lisp is a prominent functional programming language, which is used primarily for artificial intelligence problems. As such it is one of the main competitors of Prolog. In Lisp almost everything is a list, just as in Prolog everything is a term.
;
; 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.
;
; Exemple:
; * 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).
; edge(k,f).
; edge(f,b).
; ...
;
; We call this edge-clause form. Obviously, isolated nodes cannot be represented. Another method is to represent the whole graph as one data object. According to the definition of the graph as a pair of two sets (nodes and edges), we may use the following Prolog term to represent the Exemple graph:
;
; graph([b,c,d,f,g,h,k],[e(b,c),e(b,f),e(c,f),e(f,k),e(g,h)])
;
; 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 Exemple:
;
; [n(b,[c,f]), n(c,[b,f]), n(d,[]), n(f,[b,c,k]), ...]
;
; 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 Exemple 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 Exemple 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 Exemple).
;
;
; 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 Exemple 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]
;
;
; 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.
;
; P80 (***) Conversions
; Write predicates to convert between the different graph representations. With these predicates, all representations are equivalent; i.e. for the following problems you can always pick freely the most convenient form. The reason this problem is rated (***) is not because it's particularly difficult, but because it's a lot of work to deal with all the special cases.
;
; P81 (**) Path from one node to another one
; Write a predicate path(G,A,B,P) to find an acyclic path P from node A to node b in the graph G. The predicate should return all paths via backtracking.
;
; P82 (*) Cycle from a given node
; Write a predicate cycle(G,A,P) to find a closed path (cycle) P starting at a given node A in the graph G. The predicate should return all cycles via backtracking.
;
; P83 (**) Construct all spanning trees
; Write a predicate s-tree(Graph,Tree) to construct (by backtracking) all spanning trees of a given graph. With this predicate, find out how many spanning trees there are for the graph depicted to the left. The data of this Exemple graph can be found in the file p83.dat. When you have a correct solution for the s-tree/2 predicate, use it to define two other useful predicates: is-tree(Graph) and is-connected(Graph). Both are five-minutes tasks!
;
; P84 (**) Construct the minimal spanning tree
; Write a predicate ms-tree(Graph,Tree,Sum) to construct the minimal spanning tree of a given labelled graph. Hint: Use the algorithm of Prim. A small modification of the solution of P83 does the trick. The data of the Exemple graph to the right can be found in the file p84.dat.
;
;
; P85 (**) Graph isomorphism
; Two graphs G1(N1,E1) and G2(N2,E2) are isomorphic if there is a bijection f: N1 -> N2 such that for any nodes X,Y of N1, X and Y are adjacent if and only if f(X) and f(Y) are adjacent.
;
; Write a predicate that determines whether two graphs are isomorphic. Hint: Use an open-ended list to represent the function f.
;
; P86 (**) Node degree and graph coloration
; a) Write a predicate degree(Graph,Node,Deg) that determines the degree of a given node.
;
; 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.
;
; P87 (**) Depth-first order graph traversal (alternative solution)
; Write a predicate that generates a depth-first order graph traversal sequence. The starting point should be specified, and the output should be a list of nodes that are reachable from this starting point (in depth-first order).
;
; P88 (**) Connected components (alternative solution)
; Write a predicate that splits a graph into its connected components.
;
; P89 (**) Bipartite graphs
; Write a predicate that finds out whether a given graph is bipartite.
;
;
; 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. Exemple: [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.
;
; P91 (**) Knight's tour
; Another famous problem is this one: How can a knight jump on an NxN chessboard in such a way that it visits every square exactly once?
;
; 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).
;
; P92 (***) Von Koch's conjecture
; Several years ago I met a mathematician who was intrigued by a problem for which he didn't know a solution. His name was Von Koch, and I don't know whether the problem has been solved since.
;
; 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?
;
; P93 (***) An arithmetic puzzle
; Given a list of integer numbers, find a correct way of inserting arithmetic signs (operators) such that the result is a correct equation. Exemple: With the list of numbers [2,3,5,7,11] we can form the equations 2-3+5+7 = 11 or 2 = (3*5+7)/11 (and ten others!).
;
; P94 (***) Generate K-regular simple graphs with N nodes
; In a K-regular graph all nodes have a degree of K; i.e. the number of edges incident in each node is K. How many (non-isomorphic!) 3-regular graphs with 6 nodes are there? See also a table of results and a Java applet that can represent graphs geometrically.
;
; P95 (**) English number words
; On financial documents, like cheques, numbers must sometimes be written in full words. Exemple: 175 must be written as one-seven-five. Write a predicate full-words/1 to print (non-negative) integer numbers in full words.
;
; P96 (**) Syntax checker (alternative solution with difference lists)
; In a certain programming language (Ada) identifiers are defined by the syntax diagram (railroad chart) opposite. Transform the syntax diagram into a system of syntax diagrams which do not contain loops; i.e. which are purely recursive. Using these modified diagrams, write a predicate identifier/1 that can check whether or not a given string is a legal identifier.
;
; % identifier(Str) :- Str is a legal identifier
; P97 (**) Sudoku
; Sudoku puzzles go like this:
;
; Problem statement Solution
;
; . . 4 | 8 . . | . 1 7 9 3 4 | 8 2 5 | 6 1 7
; | | | |
; 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.
; P98 (***) Nonograms
; Around 1994, a certain kind of puzzles was very popular in England. The "Sunday Telegraph" newspaper wrote: "Nonograms are puzzles from Japan and are currently published each week only in The Sunday Telegraph. Simply use your logic and skill to complete the grid and reveal a picture or diagram." As a Prolog programmer, you are in a better situation: you can have your computer do the work! Just write a little program ;-).
;
; 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.
;
; Problem statement: Solution:
;
; |_|_|_|_|_|_|_|_| 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 Exemple 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 Exemple, e.g. 25 x 20, and apparently always have unique solutions.
;
; P99 (***) Crossword puzzle
; Given an empty (or almost empty) framework of a crossword puzzle and a set of words. The problem is to place the words into the framework.
;
; The particular crossword puzzle is specified in a text file which first lists the words (one word per line) in an arbitrary order. Then, after an empty line, the crossword framework is defined. In this framework specification, an empty character location is represented by a dot (.). In order to make the solution easier, character locations can also contain predefined character values. The puzzle opposite is defined in the file p99a.dat, other Exemples are p99b.dat and p99d.dat. There is also an Exemple of a puzzle (p99c.dat) which does not have a solution.
;
; 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.lisp. 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.
;
; Last modified: Thursday, 10th January 2008; Eric KEDJI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment