Skip to content

Instantly share code, notes, and snippets.

View maurobaraldi's full-sized avatar

Mauro Navarro Baraldi maurobaraldi

View GitHub Profile
@maurobaraldi
maurobaraldi / bbtree.py
Created February 3, 2016 22:23 — forked from olomix/bbtree.py
Balanced binary tree in Python
#!/usr/bin/env python2.7
import random
import subprocess
class Node(object):
def __init__(self, key, value):
self.key = key
self.value = value
@maurobaraldi
maurobaraldi / life.py
Created February 20, 2016 10:53 — forked from jsbueno/life.py
Game of Life (Python + Pygame)
# coding: utf-8
import pygame
import random
rr = random.randrange
SIZE = 800, 600
cellsize = 20
try:
  1. Plain Strings (207): foo
  2. Anchors (208): k$
  3. Ranges (202): ^[a-f]*$
  4. Backrefs (201): (...).*\1
  5. Abba (169): ^(.(?!(ll|ss|mm|rr|tt|ff|cc|bb)))*$|^n|ef
  6. A man, a plan (177): ^(.)[^p].*\1$
  7. Prime (286): ^(?!(..+)\1+$)
  8. Four (199): (.)(.\1){3}
  9. Order (198): ^[^o].....?$
  10. Triples (507): (^39|^44)|(^([0369]|([147][0369]*[258])|(([258]|[147][0369]*[147])([0369]*|[258][0369]*[147])([147]|[258][0369]*[258])))*$)
@maurobaraldi
maurobaraldi / test_temp_directory.py
Created April 2, 2016 11:22 — forked from odyniec/test_temp_directory.py
An example Python unittest test case that creates a temporary directory before a test is run and removes it when it's done.
import shutil, tempfile
from os import path
import unittest
class TestExample(unittest.TestCase):
def setUp(self):
# Create a temporary directory
self.test_dir = tempfile.mkdtemp()
def tearDown(self):
@maurobaraldi
maurobaraldi / snake.py
Created November 11, 2016 15:21 — forked from jsbueno/snake.py
Simple Snake Game using Pygame - used in the workshop in Guarulhos at 2016-10-21
# coding: utf-8
import pygame
import random
# [email protected]
TAMANHO = (640, 480)
T = 640 / 32
TX = 640 / T
TY = 480 / T
@maurobaraldi
maurobaraldi / gist:953e910f37ab78dce77c5a5b428c2f43
Created May 24, 2018 00:30 — forked from ptigas/gist:2820165
linked list implementation in python
class Node :
def __init__( self, data ) :
self.data = data
self.next = None
self.prev = None
class LinkedList :
def __init__( self ) :
self.head = None
@maurobaraldi
maurobaraldi / LRU.py
Created June 21, 2018 14:09 — forked from reterVision/LRU.py
LRU algorithm implemented in Python.
from datetime import datetime
class LRUCacheItem(object):
"""Data structure of items stored in cache"""
def __init__(self, key, item):
self.key = key
self.item = item
self.timestamp = datetime.now()
@maurobaraldi
maurobaraldi / product.py
Created August 13, 2018 15:48 — forked from gregglind/product.py
Recursive python "product"
"""
this has a lot of list / tuple casting, and doesn't use
native python list properties to full advantage.
It mimics Little Schemer style coding quite well though.
"""
import itertools
@maurobaraldi
maurobaraldi / bubble.py
Created September 19, 2018 23:25 — forked from jootse84/bubble.py
Bubble sort recursive implementation in Python
def bubble(l_elems, is_sorted, step):
if step is 1 or is_sorted:
# base case: l_elems is already sorted or we pass through the list len(l_elems) times
return l_elems
else:
is_swapped = False
for i in range(len(l_elems) - 1):
# compares each pair of adjacent items and swaps them if they are in the wrong order
if l_elems[i] > l_elems[i + 1]:
is_swapped = True
@maurobaraldi
maurobaraldi / simpletree.py
Created February 27, 2019 22:41 — forked from jsbueno/simpletree.py
Simple example of ranged binary tree in Python, able to perform slice retrieval
# coding: utf-8
class EmptyClass(object):
def __repr__(self):
return ""
Empty = EmptyClass()
class Node(object):
def __init__(self, value, key=None):