Skip to content

Instantly share code, notes, and snippets.

;;;; cl-overload.lisp
(in-package #:cl-overload)
(defgeneric generic-find (item sequence &key test)
(:generic-function-class fast-generic-functions:fast-generic-function))
(defmethod generic-find (item (list list) &key (test #'eql))
(and (member item list :test test)
@commander-trashdin
commander-trashdin / mul_1.asm
Created June 18, 2020 14:59
Should be a multiplication for natural bignums
dnl AMD64 mpn_mul_1.
dnl Copyright 2003-2005, 2007, 2008, 2012 Free Software Foundation, Inc.
dnl This file is part of the GNU MP Library.
dnl
dnl The GNU MP Library is free software; you can redistribute it and/or modify
dnl it under the terms of either:
dnl
dnl * the GNU Lesser General Public License as published by the Free
30
14
21 41 85 91 15 13 77 26 90 44 99 23 77 82
11
7 69 22 96 26 15 18 53 87 90 89
39
60 31 12 77 47 48 73 21 55 64 20 18 96 55 99 28 21 28 90 27 15 96 43 21 29 41 56 27 41 79 34 59 34 19 54 85 28 36 53
14
81 46 13 67 48 86 98 51 25 89 19 58 26 5
13
(defun tug-of-war (people)
(declare (optimize (speed 3))
((simple-array fixnum (*)) people))
(setf people (sort people #'>))
(let ((sum (reduce #'+ people)))
(declare (fixnum sum))
(if (evenp sum)
(let ((sum (floor sum 2)))
(labels ((recursive-walk (upto currentsum)
(declare (fixnum currentsum))
(defun diophantine-equation (a b)
(declare (fixnum a) (fixnum b)
(optimize (speed 3)))
(let ((left-side-a 1) (left-side-b 0)
(right-side-a 0) (right-side-b 1))
(declare (fixnum left-side-a) (fixnum left-side-b)
(fixnum right-side-a) (fixnum right-side-b))
(loop :until (or (= 0 a) (= 0 b))
:if (> a b)
@commander-trashdin
commander-trashdin / .lisp
Created September 11, 2020 17:22
consecutive sum
(defun consecsum (array)
(loop :with res := (make-array 1 :adjustable t :fill-pointer 0)
:with acc := (aref array 0)
:for i :from 1 :below (length array)
:for j := (- i 1)
:if (= (aref array i) (aref array j))
:do (incf acc (aref array i))
:else
:do (vector-push-extend acc res)
(setf acc (aref array i))
@commander-trashdin
commander-trashdin / word_count.cpp
Created September 15, 2020 23:04
Interview question.
#include <vector>
#include <string>
#include <unordered_map>
#include <fstream>
#include <utility>
#include <algorithm>
#include <cassert>
#include <string_view>
@commander-trashdin
commander-trashdin / safe-map.lisp
Created September 22, 2020 00:08
Safely mapping with backup
(defun prompt-new-value (prompt)
(format *query-io* prompt) ;; *query-io*: the special stream to make user queries.
(force-output *query-io*) ;; Ensure the user sees what he types.
(list (read *query-io*)))
(defun safe-map (predicate sequence &optional copy)
(let ((backup (make-array 1 :adjustable t :fill-pointer 0)))
(loop :for i :from 0 :below (length sequence)
@commander-trashdin
commander-trashdin / word_count.cpp
Created September 24, 2020 13:43
Slow version
#include <vector>
#include <string>
#include <unordered_map>
#include <fstream>
#include <utility>
#include <algorithm>
#include <cassert>
#include <string_view>
@commander-trashdin
commander-trashdin / defun-doc.lisp
Created October 16, 2020 22:26
Let's force people to write docs.
(defparameter *type-policy* :docs)
(defparameter *doc-info* (make-hash-table :test #'equal))
(defun validate-ftype (ftype)
(labels ((check-symbols (list)
(every (lambda (token)
(if (atom token)
(symbolp token)
(check-symbols token)))