Skip to content

Instantly share code, notes, and snippets.

View Ad115's full-sized avatar

Andrés García García Ad115

View GitHub Profile
@Ad115
Ad115 / tree_reduce.py
Created October 15, 2019 19:52
A generic tree reduce algorithm in Python
"""
Generic recursive tree reduce algorithm
=======================================
Trees are one of the most ubiquitous data structures. It is amazing how often we
as programmers tend to reimplement the same algorithms for different trees.
This module defines a generic tree-reduce algorithms that can be
used with any tree-like object such as filesystem paths, lists, nested
dictionaries an expression tree or even specialized tree classes! The only thing
@Ad115
Ad115 / tree_utils.py
Created October 15, 2019 07:20
Generic tree traversal and recursive tree reduce algorithms for Python
"""
Generic tree utilities for Python
=================================
Trees are one of the most ubiquitous data structures. It is amazing how often we
as programmers tend to reimplement the same algorithms for different trees.
This module defines generic tree-traverse and tree-reduce algorithms that can be
used with any tree-like object such as filesystem paths, lists, nested
dictionaries, expression trees or even specialized tree classes! The only thing
@Ad115
Ad115 / parse_newick.py
Last active December 1, 2023 10:18
A basic parser for trees in the Newick format.
"""
A simple function `parse_newick` to parse tree structures specified in the
Newick format (NHX supported). The function allows to customize the final
representation, be it lists, dicts or a custom Tree class.
Usage
-----
```
def tree_as_dict(label, children, distance, features):
if children:
@Ad115
Ad115 / excepciones.py
Last active October 3, 2018 06:34
En Python es mejor pedir perdón que pedir permiso...
from turtle import Turtle
squirtle = Turtle()
def izquierda(pasos):
squirtle.left(90)
squirtle.forward(pasos)
def derecha(pasos):
squirtle.right(90)
@Ad115
Ad115 / logic-error.py
Created October 1, 2018 19:19
Ejemplo de un error lógico
edad = int(input("Cuántos años tienes?"))
if edad > 65:
print("Ya te jubilaste?")
if edad > 21:
print("Ya eres un adulto.")
if edad > 13:
print("Eres todo un joven adolescente.")
@Ad115
Ad115 / runtime-error.py
Created October 1, 2018 19:12
Ejemplo de un error de ejecución
edad = int(input("Cuántos años tienes?"))
if edad > 65:
print("Ya te jubilaste?")
elif tu_edad > 21:
print("Ya eres un adulto.")
elif edad > 13:
print("Eres todo un joven adolescente.")
@Ad115
Ad115 / sintax-error.py
Created October 1, 2018 19:10
Un ejemplo de un error de sintaxis
edad = int(input("Cuántos años tienes?")
if edad > 65:
print("Ya te jubilaste?")
elif edad > 21:
print("Ya eres un adulto.")
elif edad > 13:
print("Eres todo un joven adolescente.")
@Ad115
Ad115 / module-op.py
Last active September 19, 2018 19:28
Un caso común de uso de el operador módulo en Python
import turtle
squirtle = turtle.Turtle()
# Los colores que usaremos
colors = ["red", "yellow", "blue", "orange", "green", "purple"]
n_colors = len(colors)
for i in range(100):
# Obtén el índice del color
@Ad115
Ad115 / bools-app.py
Last active September 19, 2018 19:53
Una aplicación interesante de los booleanos
import turtle
import random
squirtle = turtle.Turtle()
for i in range(100):
# Haz un giro aleatorio
coin = random.choice([True, False])
angle = random.randint(0, 360)
from turtle import Turtle
gamera = Turtle()
# Podríamos hacer muchas cosas padres con sólo repetir:
gamera.forward(50)
gamera.left(30)
gamera.forward(50)
gamera.left(30)
gamera.forward(50)