Skip to content

Instantly share code, notes, and snippets.

Avatar

Julio Marins aj07mm

View GitHub Profile
View qsort.py
# 3 = 2^2
# x = 2
def qsort(arr):
print(arr)
if len(arr) == 0:
return arr
pivot = arr[0]
sup_arr = []
View 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"] = []
View 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 Jun 25, 2020
ReadWriteSerializerMethodField
View readwriteserializermethodfield.py
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
View 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)
View 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))
View 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)))
View 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 Jul 25, 2019
get_image_file.py
View 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 Jul 4, 2019
debounce_throttling.js
View 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);
};
}