Skip to content

Instantly share code, notes, and snippets.

@howeyc
Created February 11, 2012 01:54
Show Gist options
  • Save howeyc/1795221 to your computer and use it in GitHub Desktop.
Save howeyc/1795221 to your computer and use it in GitHub Desktop.
Embedly Challenge (apply.embed.ly)
(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))
(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"))
(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