forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
HydraDarkNg
authored and
HydraDarkNg
committed
Sep 7, 2023
1 parent
feb74b2
commit 551d13b
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
Retos/Reto #36 - PERMUTACIONES [Media]/lisp/HydraDarkNg.lisp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
;; Reto #36: Permutaciones | ||
;;;; Dificultad: Media | Publicación: 04/09/23 | Corrección: 18/09/23 | ||
|
||
;;; Enunciado | ||
|
||
;; | ||
;; Crea un programa que sea capaz de generar e imprimir todas las | ||
;; permutaciones disponibles formadas por las letras de una palabra. | ||
;; - Las palabras generadas no tienen por qué existir. | ||
;; - Deben usarse todas las letras en cada permutación. | ||
;; - Ejemplo: sol, slo, ols, osl, los, lso | ||
;; | ||
|
||
(declaim (optimize (speed 3) (safety 0))) | ||
|
||
(declaim (ftype (function (list &optional list) list) all-permutations)) | ||
(declaim (ftype (function (string) null) generate-permutation)) | ||
|
||
(defun all-permutations (list &optional (remain list)) | ||
(cond ((null remain) nil) | ||
((null (rest list)) (list list)) | ||
(t (append | ||
(mapcar (lambda (l) (cons (first list) l)) | ||
(all-permutations (rest list))) | ||
(all-permutations (append (rest list) (list (first list))) (rest remain)))))) | ||
|
||
(defun generate-permutation (string) | ||
(format t "~{~{~a~}~%~}" | ||
(all-permutations | ||
(map 'list #'identity string)))) | ||
|
||
(generate-permutation "sol") |