Last active
August 29, 2015 14:20
-
-
Save y2q-actionman/f753679f1f4188bf6c0d to your computer and use it in GitHub Desktop.
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
(in-package :cl-user) | |
;; https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour | |
;; 1:00 | |
(defun problem1-for (numlist) | |
(let ((ret 0)) | |
(dolist (n numlist ret) | |
(incf ret n)))) | |
;; 2:00 | |
(defun problem1-while (numlist) | |
(loop with ret = 0 | |
for n = numlist | |
while n | |
do (incf ret (car n)) | |
(pop numlist) | |
finally (return ret))) | |
;; 2:00 | |
(defun problem1-recursion (numlist) | |
(labels ((iter (ret numlist) | |
(if numlist | |
(iter (+ ret (car numlist)) (cdr numlist)) | |
ret))) | |
(iter 0 numlist))) | |
;; 2:14 | |
(defun problem2 (list1 list2) | |
(loop for i = (pop list1) | |
for j = (pop list2) | |
if i | |
collect i into ret | |
if j | |
collect j into ret | |
while (and list1 list2) | |
finally (return (append ret list1 list2)))) | |
;; 4:52 | |
(defun problem3 (to-num) | |
(loop with results = nil | |
for i from 0 below to-num | |
if (= i 0) | |
do (push 0 results) | |
else if (= i 1) | |
do (push 1 results) | |
else | |
do (push (+ (first results) | |
(second results)) | |
results) | |
finally | |
(return (nreverse results)))) | |
;; 7:02 | |
;; fix 4:00, but same mistake as the passing solution said in | |
;; https://blog.svpino.com/2015/05/08/solution-to-problem-4 | |
(defun problem4 (numlist) | |
(let* ((tail-char (code-char (1- (char-code #\0)))) | |
(tail-char-string (string tail-char)) | |
(num-strings-sen | |
(loop for n in numlist | |
as numstr = (format nil "~D" n) | |
collect (concatenate 'string numstr tail-char-string))) | |
(num-strings-sen-sorted | |
(sort num-strings-sen #'string>))) | |
(loop with largest-num-string = "" | |
for numstr-sen in num-strings-sen-sorted | |
as numstr = (string-right-trim tail-char-string numstr-sen) | |
do (setf largest-num-string | |
(concatenate 'string largest-num-string numstr)) | |
finally | |
(return (parse-integer largest-num-string))))) | |
;; algorithm -- 25:00 | |
;; output formatting -- 5:00 | |
(defun problem5 (&key (nums '(1 2 3 4 5 6 7 8 9)) (target-value 100)) | |
(let ((rev-nums (reverse nums))) | |
(labels ((unwrap-nums (nums) | |
(loop with ret = 0 | |
for n = (pop nums) | |
do (incf ret n) | |
while nums | |
do (setf ret (* ret 10)) | |
finally (return ret))) | |
(format-syms (nums symbols) | |
(loop for i in nums | |
for sym in symbols | |
do (ecase sym | |
((+ -) (format t " ~A ~A" sym i)) | |
((nil) (format t "~A" i))))) | |
(iter (value symbols prev-nums rest-rev-nums) | |
;; (format t "~&iter: ~A, ~A, ~A, ~A~%" value symbols prev-nums rest-rev-nums) | |
(cond | |
(rest-rev-nums | |
;; + | |
(iter (+ value (unwrap-nums prev-nums)) | |
(list* '+ symbols) | |
(list (car rest-rev-nums)) | |
(cdr rest-rev-nums)) | |
;; - | |
(iter (- value (unwrap-nums prev-nums)) | |
(list* '- symbols) | |
(list (car rest-rev-nums)) | |
(cdr rest-rev-nums)) | |
;; nothing | |
(iter value | |
(list* nil symbols) | |
(list* (car rest-rev-nums) prev-nums) | |
(cdr rest-rev-nums))) | |
(prev-nums | |
;; + | |
(iter (+ value (unwrap-nums prev-nums)) | |
(list* '+ symbols) | |
nil | |
nil) | |
;; - | |
(iter (- value (unwrap-nums prev-nums)) | |
(list* '- symbols) | |
nil | |
nil)) | |
((or (null target-value) ; prints all -- OMAKE! | |
(= value target-value)) | |
;; (format t "~&[success] ~A~%" (problem2 symbols nums)) | |
(fresh-line) | |
(format t "~A =" value) | |
(format-syms nums symbols) | |
(terpri)) | |
(t | |
;; (format t "~&[fail: ~D] ~A~%" value (problem2 symbols nums)) | |
nil)))) | |
(iter 0 | |
nil | |
(list (first rev-nums)) | |
(cdr rev-nums))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment