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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / byte-array-to-fixnum-array.lisp
Last active December 15, 2015 09:49
Converts a byte array to a 32-bits unsigned int array
(defun byte-array-to-ui-32-bit-array (octet-array)
(declare (optimize (speed 3))
(type (simple-array (unsigned-byte 8) (*)) octet-array))
(if (/= (mod (length octet-array) 4) 0)
(make-array 0 :element-type '(unsigned-byte 32))
(make-array (/ (length octet-array) 4)
:element-type '(unsigned-byte 32)
:initial-contents
(loop for i from 0 below (length octet-array) by 4
for j = 0 then (1+ j)
@Denommus
Denommus / test-closure.java
Last active December 15, 2015 02:29
Trying to make a closure in Java
import java.util.concurrent.atomic.AtomicReference;
public abstract class TestClosure {
public abstract int execute(int a);
}
public class MainClass {
public static void main(String[] args) {
final AtomicReference<Integer> x = new AtomicReference<Integer>(4);
TestClosure testClosure = new TestClosure() {