Skip to content

Instantly share code, notes, and snippets.

View Denommus's full-sized avatar

Yuri Albuquerque Denommus

  • Brick Abode
  • Florianópolis, SC, Brazil
View GitHub Profile
@Denommus
Denommus / alien-language.lisp
Last active December 15, 2015 23:19
Solution for the Code Jam's problem of Alien Language ( https://code.google.com/codejam/contest/90101/dashboard#s=p0 ), in Common Lisp
#!/usr/bin/sbcl --script
(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))
(require 'cl-ppcre)
(with-open-file (in (car (last *posix-argv*)))
(let* ((l (read in))
(d (read in))
(n (read in))
(words (loop repeat d
@Denommus
Denommus / all-your-base.lisp
Created April 9, 2013 23:41
Solution for the Code Jam's problem "All Your Base" (https://code.google.com/codejam/contest/189252/dashboard#s=p0)
#!/usr/bin/sbcl --script
(defun count-unique (alist)
(let ((hash (make-hash-table :test #'equal)))
(map 'nil
(lambda (i)
(setf (gethash i hash) 1))
alist)
(hash-table-count hash)))
@Denommus
Denommus / minimum-scalar-product.lisp
Created April 11, 2013 03:07
Solution for the Code Jam's problem Minimum Scalar Product (https://code.google.com/codejam/contest/32016/dashboard#s=p0)
#!/usr/bin/sbcl --script
(with-open-file (in (car (last *posix-argv*)))
(let ((total-cases (read in)))
(with-open-file (out "A.out" :direction :output :if-exists :supersede)
(loop for i from 1 to total-cases do
(let* ((n (read in))
(v1 (loop for j from 0 below n collect (read in)))
(v2 (loop for j from 0 below n collect (read in))))
(format out "Case #~D: ~D~%" i
@Denommus
Denommus / global-counter.lisp
Created April 16, 2013 21:44
Global counter without global variable
(let ((counter 0))
(defun increment-counter ()
(incf counter))
(defun reset-counter ()
(setf counter 0))
(defun get-counter ()
counter))
@Denommus
Denommus / reverse-polish-calc.lisp
Last active December 16, 2015 09:48
A calculator using reverse polish notation, written in Common Lisp
#!/usr/bin/sbcl --script
(loop for n = (read t)
for stack = (list n) then (cons n stack)
until (member n '(exit quit))
do (unless (numberp n)
(cond
((endp (nthcdr 2 stack))
(format t "Not enough numbers in stack~%")
(pop stack))
@Denommus
Denommus / character_remove.c
Created May 2, 2013 14:41
Removing a character in C
#include <stdio.h>
#include <string.h>
int main() {
char a[] = "abcd";
memmove(a, a+1, 3);
a[3] = 0;
printf(a);
return 0;
}
@Denommus
Denommus / time-functions.lisp
Last active December 17, 2015 17:29
A small test gist for generating functions for time passing.
#!/usr/bin/sbcl --script
(defmacro time-methods (&body list)
(let ((number (gensym)))
`(progn
,@(loop for (key value) on list by #'cddr
collect `(defun ,key (,number)
(* ,number ,value))))))
(time-methods seconds 1 minutes 60 hours 3600 days 86400)
@Denommus
Denommus / integral.hs
Last active April 22, 2022 15:24
Numeric integral implementation (Simpson method) in different languages
integral :: (Fractional a, Ord a) => (a -> a) -> Integer -> a -> a -> a
integral f p a b
| a==b = 0
| otherwise = total 0 a
where dx = (b-a)/fromInteger p
total t x | x2>b = t
| otherwise = total (t+(dx*(f x+(4*f ((x+x2)/2))+f x2)/6)) x2
where x2 = x+dx
@Denommus
Denommus / factorial.lisp
Last active February 4, 2025 14:49
A simple tail-recursive factorial example
(defun factorial (number)
(labels ((factorial-helper (x accumulator)
(if (zerop x)
accumulator
(factorial-helper (- x 1) (* accumulator x)))))
(factorial-helper number 1)))
@Denommus
Denommus / tree.rs
Last active January 3, 2016 04:49
Tree implementation in Rust, both with mutable and immutable append.
use std::vec::append_one;
#[deriving(Clone)]
struct Tree<T>(T, ~[Tree<T>]);
impl<T: ToStr> ToStr for Tree<T> {
fn to_str(&self) -> ~str {
let &Tree(ref data, ref children) = self;
data.to_str() + " -> " + children.to_str()
}