Skip to content

Instantly share code, notes, and snippets.

View luisenriquecorona's full-sized avatar
😎
I may be slow to respond.

TextKi JS luisenriquecorona

😎
I may be slow to respond.
View GitHub Profile
@luisenriquecorona
luisenriquecorona / Graphic Turtle.py
Last active March 29, 2020 05:39
Escribamos un programa que haga avanzar 100 pasos a la tortuga y deje un trazo en pantalla.
from turtle import Screen, Turtle
pantallaa=Screen ()
pantalla.setup (425,225)
pantalla.screensize (400,200)
tortuga=Turtle ()
tortuga. forward (100)
pantalla. exitonclick()
@luisenriquecorona
luisenriquecorona / randomly.py
Created February 10, 2020 21:39
This module can also choose an item at random from a sequence, and shuffle a list of items randomly.
>>> random.choice(['Life of Brian', 'Holy Grail', 'Meaning of Life'])
'Holy Grail'
>>> random.choice(['Life of Brian', 'Holy Grail', 'Meaning of Life'])
'Life of Brian'
>>> suits = ['hearts', 'clubs', 'diamonds', 'spades']
>>> random.shuffle(suits)
>>> suits
['spades', 'hearts', 'diamonds', 'clubs']
>>> random.shuffle(suits)
>>> suits
@luisenriquecorona
luisenriquecorona / Clip.py
Created February 8, 2020 04:01
Function to shorten a string by clipping at a space near the desired length.
def clip(text, max_len=80):
"""Return text clipped at the last space before or after max_len
"""
end = None
if len(text) > max_len:
space_before = text.rfind(' ', 0, max_len)
if space_before >= 0:
end = space_before
else:
space_after = text.rfind(' ', max_len)
@luisenriquecorona
luisenriquecorona / Bingocall.py
Created February 8, 2020 03:34
A BingoCage does one thing: picks items from a shuffled list.
import random
class BingoCage:
def __init__(self, items):
self._items = list(items)
random.shuffle(self._items)
def pick(self):
try:
return self._items.pop()
except IndexError:
raise LookupError('pick from empty BingoCage')
@luisenriquecorona
luisenriquecorona / Ramanujan.py
Created February 8, 2020 03:00
If you build a regular expression with bytes, patterns such as \d and \w only match ASCII characters; in contrast, if these patterns are given as str, they match Unicode digits or letters beyond ASCII.
import re
re_numbers_str = re.compile(r'\d+')
re_words_str = re.compile(r'\w+')
re_numbers_bytes = re.compile(rb'\d+')
re_words_bytes = re.compile(rb'\w+')
text_str = ("Ramanujan saw \u0be7\u0bed\u0be8\u0bef"
" as 1729 = 1³ + 12³ = 9³ + 10³.")
text_bytes = text_str.encode('utf_8')
print('Text', repr(text_str), sep='\n ')
print('Numbers')
@luisenriquecorona
luisenriquecorona / Callouts.py
Created February 8, 2020 02:32
Demo of Unicode database numerical character metadata. Callouts describe each column in the output.
import unicodedata
import re
re_digit = re.compile(r'\d')
sample = '1\xbc\xb2\u0969\u136b\u216b\u2466\u2480\u3285'
for char in sample:
print('U+%04x' % ord(char),
char.center(6),
're_dig' if re_digit.match(char) else '-',
'isdig' if char.isdigit() else '-',
'isnum' if char.isnumeric() else '-',
@luisenriquecorona
luisenriquecorona / Ascii.py
Created February 8, 2020 02:17
Transform some Western typographical symbols into ASCII.
single_map = str.maketrans("""‚ƒ„†ˆ‹‘’“”•–—˜›""",
"""'f"*^<''""---~>""")
multi_map = str.maketrans({
'€': '<euro>',
'…': '...',
'Œ': 'OE',
'™': '(TM)',
'œ': 'oe',
'‰': '<per mille>',
'‡': '**',
@luisenriquecorona
luisenriquecorona / printGuessStatistics.java
Last active January 18, 2020 07:20
Do the variables need a more meaningful context? The function name provides only part of the context; the algorithm provides the rest.
private void printGuessStatistics(char candidate, int count) {
String number;
String verb;
String pluralModifier;
if (count == 0) {
number = "no";
verb = "are";
pluralModifier = "s";
} else if (count == 1) {
number = "1";
@luisenriquecorona
luisenriquecorona / Sanitize.py
Last active January 9, 2020 05:06
Function to remove combining marks from Latin characters. import statements are omitted as this is part of the sanitize.py module.
def shave_marks_latin(txt):
"""Remove all diacritic marks from Latin base characters"""
norm_txt = unicodedata.normalize('NFD', txt)
latin_base = False
keepers = []
for c in norm_txt:
if unicodedata.combining(c) and latin_base:
continue # ignore diacritic on Latin base char
keepers.append(c)
# if it isn't combining char, it's a new base char
@luisenriquecorona
luisenriquecorona / Dial_Codes.py
Created January 5, 2020 23:38
Demonstrates the effect of loading three dicts with the same data, just in different order. The resulting dictionaries all compare equal, even if their order is not the same.
# dial codes of the top 10 most populous countries
DIAL_CODES = [
(86, 'China'),
(91, 'India'),
(1, 'United States'),
(62, 'Indonesia'),
(55, 'Brazil'),
(92, 'Pakistan'),
(880, 'Bangladesh'),