Last active
February 3, 2019 17:57
-
-
Save imjacobclark/2e425414567c23aa8baf25cd294d2faa to your computer and use it in GitHub Desktop.
Pad numbers to same length
This file contains hidden or 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
(defun pad(thing) | |
(concatenate 'string "0" thing)) | |
(defun pad-if-length-not-same(thing length) | |
(cond | |
((not | |
(= (length thing) length)) | |
(pad thing)) | |
(t thing))) | |
(defun pad-list(things length) | |
(mapcar | |
(lambda (thing) | |
(pad-if-length-not-same thing length)) | |
things)) | |
(defun pad-to-length(things length accumulator) | |
(cond | |
( | |
(not (= accumulator (1- length))) | |
(pad-to-length | |
(pad-list things length) length (1+ accumulator))) | |
(t things))) | |
(defun sort-by-length(a b) (< (length a) (length b))) | |
(defun get-largest-length(things) | |
(length | |
(car | |
(last | |
(sort things #'sort-by-length))))) | |
(defun main(things) | |
(pad-to-length | |
things | |
(get-largest-length things) | |
0)) | |
(main '("1" "19" "2" "3" "55" "4" "12" "100" "1000"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment