Created
February 11, 2012 01:54
-
-
Save howeyc/1795221 to your computer and use it in GitHub Desktop.
Embedly Challenge (apply.embed.ly)
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
(defun list-to-n (n) | |
(loop for x from 1 to n | |
collect x)) | |
(defun fac (n) | |
(reduce #'* (list-to-n n))) | |
(defun num-to-str (num) | |
(with-output-to-string (str) | |
(format str "~a" num))) | |
(defun get-digits (str) | |
(loop for x from 1 to (length str) | |
collect (parse-integer str :start (1- x) :end x))) | |
(defun r (n) | |
(apply #'+ (get-digits (num-to-str (fac n))))) | |
(defun r-until-lim (lim) | |
(loop for x from 1 by 1 | |
until (= (r x) lim) | |
finally (return x))) | |
(defun run () | |
(r-until-lim 8001)) |
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
(ql:quickload "cl-html-parse") | |
(let ((in nil) (curdepth 0) (depths nil)) | |
(defun get-depths (tree) | |
(cond ((eq :article (first tree)) | |
(setf in t) | |
(incf curdepth) | |
(dolist (subl (rest tree)) | |
(if (listp subl) | |
(get-depths subl))) | |
(setf in nil) | |
(decf curdepth)) | |
((and (eq :p (first tree)) in) | |
(push curdepth depths))) | |
(when in (incf curdepth)) | |
(dolist (subl (rest tree)) | |
(if (listp subl) | |
(get-depths subl))) | |
(when in (decf curdepth))) | |
(defun get-results () | |
depths) | |
(defun clear-results() | |
(psetf in nil | |
curdepth 0 | |
depths nil))) | |
(defun std-deviation (vals) | |
(let ((mean (/ (apply #'+ vals) (length vals))) | |
(diffs nil)) | |
(dolist (val vals) | |
(push (expt (- val mean) 2) diffs)) | |
(sqrt (/ (apply #'+ diffs) (length diffs))))) | |
(defun run-it (filepath) | |
(clear-results) | |
(with-open-file (file filepath :direction :input) | |
(dolist (tag (html-parse:parse-html file)) | |
(get-depths tag)) | |
(std-deviation (get-results)))) | |
(defun run () | |
(run-it "2.html")) |
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
(defun gen-freq (most-freq limit cur) | |
(unless (> cur limit) | |
(append (list (/ most-freq cur)) (gen-freq most-freq limit (1+ cur))))) | |
(defun run () | |
(let* ((lst (gen-freq 2520 900 1)) (max (/ (apply #'+ lst) 2))) | |
(loop for x in lst | |
counting t into terms | |
summing x into total | |
until (> total max) | |
finally (return terms)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment