Skip to content

Instantly share code, notes, and snippets.

@jgomo3
Last active August 13, 2022 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgomo3/4adc5e709c41361c99f289cd2cb33076 to your computer and use it in GitHub Desktop.
Save jgomo3/4adc5e709c41361c99f289cd2cb33076 to your computer and use it in GitHub Desktop.
Solución al ejercicio 3 de Blackbox
(ns jgomo3.playground.blackbox.bb3)
(def tdc-long 16)
(def tdc-ocult 13)
(def tdc-most (- tdc-long tdc-ocult))
(defn tdc-req? [n]
"String -> Boolean
Condición para procesar `n` como tarjeta de crédito"
(= (count n) tdc-long))
(defn sólo-los-dígitos [n]
"String-or-Int -> String
Recoge sólo los dígitos del número `n`"
(-> n
str
(clojure.string/replace #"[^0-9]" "")))
(defn substituir-dígitos
"String Char -> String
Substituye cada uno de los primeros `tdc-ocult` dígitos de la
cadena `n` por el caracter `x`."
[n x]
(apply str (concat (repeat tdc-ocult x) (subs n tdc-ocult))))
(defn ocultar-números
"[String-or-Int]
[String-or-Int Char] -> String or nil
Substituye cada uno de los primeros dígitos del número `n` con el
caracter `x` (que por defecto es `\\x`).
El número de caracteres a substituir está controlado por
el valor `tdc-ocult`.
Si `n` no tiene exactamente `tdc-long` dígitos, esta función
devuelve `nil`.
"
[n & {x :x, :or {x \x}}]
(let [n (sólo-los-dígitos n)]
(when (tdc-req? n)
(substituir-dígitos n x))))
(comment
(sólo-los-dígitos "1 . d 23 4")
;; => "1234"
(sólo-los-dígitos 1234)
;; => "1234"
(tdc-req? "1234567890123456")
;; => true
(substituir-dígitos "1234567890123456" \x)
;; => "xxxxxxxxxxxxx456"
(ocultar-números "1234567890123456")
;; => "xxxxxxxxxxxxx456"
(ocultar-números "1234567890123456" :x \*)
;; => "*************456"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment