Skip to content

Instantly share code, notes, and snippets.

View aj07mm's full-sized avatar

Julio Marins aj07mm

View GitHub Profile
@aj07mm
aj07mm / qsort.py
Created September 5, 2020 00:17
qsort.py
# 3 = 2^2
# x = 2
def qsort(arr):
print(arr)
if len(arr) == 0:
return arr
pivot = arr[0]
sup_arr = []
@aj07mm
aj07mm / bfs.py
Created September 5, 2020 00:17
bfs.py
from collections import deque
grafo = {}
grafo["voce"] = ["alice", "bob", "claire"]
grafo["bob"] = ["anuj", "peggy"]
grafo["alice"] = ["peggy"]
grafo["claire"] = ["thom", "jonny"]
grafo["anuj"] = []
grafo["peggy"] = []
grafo["thom"] = []
@aj07mm
aj07mm / tail-rect.scm
Created September 2, 2020 20:43
tail-rect.scm
(define (fact x)
(if (= x 0) 1
(* x (fact (- x 1)))))
(define (fact x)
(define (fact-tail x accum)
(if (= x 0) accum
(fact-tail (- x 1) (* x accum))))
(fact-tail x 1))
@aj07mm
aj07mm / readwriteserializermethodfield.py
Created June 25, 2020 16:21
ReadWriteSerializerMethodField
class ReadWriteSerializerMethodField(serializers.Field):
def __init__(self, method_name=None, **kwargs):
self.method_name = method_name
kwargs['source'] = '*'
#kwargs['read_only'] = True
super(ReadWriteSerializerMethodField, self).__init__(**kwargs)
def bind(self, field_name, parent):
self.field_name = field_name
# In order to enforce a consistent style, we error if a redundant
@aj07mm
aj07mm / transducer.py
Created March 31, 2020 21:03
transducer.py
"""
user=> (transduce (map inc) + 0 [1 2 3])
9
user=> (transduce (map inc) - 10 [1 2 3])
-1
"""
def transduce(transducer, reducer, iterable, memo):
return reduce(transducer(reducer), iterable, memo)
@aj07mm
aj07mm / transducer.clj
Created March 31, 2020 21:02
transducer.clj
(defn transducer [rf]
(fn
([] (rf))
([result] (rf result))
([result input] (rf result input))))
(defn transducer [rf] # rf eh +
(fn
([result] (rf result 0))
@aj07mm
aj07mm / solve.scm
Created February 14, 2020 18:51
solve.scm
(define (add-streams s1 s2)
(stream-map + s1 s2))
(define (scale-stream stream factor)
(stream-map (lambda (x) (* x factor)) stream))
(define (integral delayed-integrand initial-value dt)
(define int
(cons-stream initial-value
(let ((integrand (force delayed-integrand)))
@aj07mm
aj07mm / derive.py
Created August 15, 2019 19:37
derive.py
def derive(obj):
from django.contrib.admin.utils import NestedObjects
from django.db import DEFAULT_DB_ALIAS
from django.db.models.fields.related import ForeignKey
"""
Derive a new model instance from previous one,
and point all related fields to the new instance
"""
obj2 = copy.copy(obj)
obj2.pk = None
@aj07mm
aj07mm / get_image_file.py
Created July 25, 2019 16:58
get_image_file.py
def get_image_file(name='test.png', ext='png', size=(50, 50), color=(256, 0, 0)):
from StringIO import StringIO
from PIL import Image
from django.core.files import File
file_obj = StringIO()
file_obj.write('')
image = Image.new("RGBA", size=size, color=color)
image.save(file_obj, ext)
file_obj.seek(0)
@aj07mm
aj07mm / debounce_throttling.js
Created July 4, 2019 19:06
debounce_throttling.js
function debounce(fn, delay) {
var timer = null;
return function () {
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
};
}