Skip to content

Instantly share code, notes, and snippets.

def digito_rif(ci):
'''
toma un nro de cedula o rif y verifica el digito validador
'''
base = {'V': 1 * 4, 'E': 2 * 4, 'J': 3 * 4}
oper = [0, 3, 2, 7, 6, 5, 4, 3, 2]
for i in range(len(ci[:9])):
if i == 0:
val = base.get(ci[0], 0)
else:
@apalala
apalala / complext_loop.py
Created July 21, 2014 22:28
Complex Lookp
# already sorted
for name, group in itertools.groupby(db_iter, key=lambda x: x[0]):
total_sales = 0
number_of_sales = 0
earliest_sale = None
latest_sale = None
for _, amount, date in group:
if amount is not None:
total_sales += amount
@apalala
apalala / bench.py
Last active November 6, 2023 01:45
A simple Python benchmark
from __future__ import print_function
from math import sin, cos, radians
import timeit
'''
A simple Python benchmark.
Results on an overclocked AMD FX-8150 Eight-Core CPU @ 3.0 GHz, and
an Intel Core i5-2410M CPU @ 2.30GHz.
@apalala
apalala / rebase.py
Created January 29, 2014 14:43
Convert the given integer to the base and symbols of the given alphabet
import string
ALPHABET = string.digits + string.ascii_letters
def rebase(value, width=1, alphabet=ALPHABET):
""" Convert the given integer to the base
and symbols of the given alphabet
"""
n = len(alphabet)
result = []
@apalala
apalala / xfloat.py
Last active December 25, 2015 12:39
Python xfloat with division by zero returning inf.
'''
This is a partial solution because several other operator methods
should be overridden for absolute consistency.
The complete list is at:
http://docs.python.org/2/reference/datamodel.html#emulating-numeric-types
http://docs.python.org/3/reference/datamodel.html#emulating-numeric-types
'''
import math
@apalala
apalala / .gitignore
Created August 9, 2012 23:00
Cardinal Splines in Python
*.swp
*.pyc
*.pyo
.project
.pydevproject
.settings
@apalala
apalala / memoization.py
Created May 23, 2012 11:08
Memoization in Python
#!/usr/bin/env python
# -*- encoding:utf-8 -*-
"""
Solution to Project Euler Problem
http://projecteuler.net/
by Apalala <apalala@gmail.com>
(cc) Attribution-ShareAlike
http://creativecommons.org/licenses/by-sa/3.0/
@apalala
apalala / singleton.py
Created May 22, 2012 16:17
Singleton implementation in Python
def singleton(cls):
class Singleton(cls):
def __new__(cls, *args, **kwargs):
try:
return cls.__instance
except AttributeError:
cls.__instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
cls.instance = cls.__instance
return cls.__instance
@apalala
apalala / languages.py
Created December 14, 2011 16:52
Spoken Languages in Python
# -*- coding: UTF-8 -*-
#####################################################################
# List of spoken languages taken from Wikipedia:
# http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
# and translated into a Python list of dictionaries.
#
# Copyless 2011 Juancarlo Añez <apalala@gmail.com>
#####################################################################