Skip to content

Instantly share code, notes, and snippets.

View luabida's full-sized avatar
🌙
路安

Luã B. Vacaro luabida

🌙
路安
  • FGV Rio
  • Brazil
  • 02:52 (UTC -03:00)
View GitHub Profile
@luabida
luabida / image-grid.md
Last active May 3, 2022 17:53 — forked from trusktr/image-grid.md
Image grids in Markdown
1 2 3
4 5 6
1 2 3 4
5 6 7 8
9 10 11
@luabida
luabida / packages.md
Last active November 23, 2022 18:22
Packages

Installing Python packages and managing Virtual Environments

How to install a package?

# https://packaging.python.org/en/latest/tutorials/installing-packages/
$ python3 -m pip --version
$ python3 -m ensurepip --default-pip
@luabida
luabida / repeat.py
Last active November 29, 2022 14:19
Profiling using `timeit.repeat()`, with Min, Mean and Max
from timeit import repeat
from statistics import mean
def stats(stmt, r=100):
res = repeat(stmt, globals=globals(), repeat=r)
print(f'Avg: {mean(res)}\nMin: {min(res)}\nMax: {max(res)}')
"""
def foo():
return 1+1
@luabida
luabida / primes.py
Created January 3, 2023 14:48
Recursion & Laziness Evaluation | First 100 positive prime numbers
def positives(n):
yield n
yield from positives(n+1)
def primes(s):
n = next(s)
yield n
yield from primes(i for i in s if i%n != 0)
@luabida
luabida / approximation_algorithm.py
Created January 6, 2023 22:50
Square root calculator with Recursion.
def next_(n, x):
return (x + n / x) / 2
# Repeats infinitely
def repeat(f, a):
yield a
for v in repeat(f, f(a)):
yield v
# Delimiter
@luabida
luabida / bisect.py
Created January 10, 2023 22:02
Bisect: bisect() & insort()
import bisect
import random
# bisect()
def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):
i = bisect.bisect(breakpoints, score)
return grades[i]
[grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]
# ['F', 'A', 'C', 'C', 'B', 'A', 'A']
@luabida
luabida / last_month_range.py
Created January 15, 2023 13:31
Calculates the first and the last day of previous datetime month.
from datetime import datetime
import calendar
def calc_last_month_range(date: datetime) -> tuple:
if date.month == 1:
ini_date = datetime(date.year - 1, 12, 1)
else:
ini_date = datetime(date.year, date.month - 1, 1)
end_date = datetime(
@luabida
luabida / smallest_webserver.py
Last active February 11, 2023 18:58
Smallest Python Webserver - by Dr. Charles Severance
from socket import *
def createServer():
serversocket = socket(AF_INET, SOCK_STREAM)
try:
serversocket.bind(('localhost',9000))
print('Starting server at http://127.0.0.1:9000/')
serversocket.listen(5)
while(1):
(clientsocket, address) = serversocket.accept()
@luabida
luabida / normalize.py
Created May 18, 2023 18:08
Normalize string
import unicodedata
def normalize_str(string: str) -> str:
non_ascii = (
unicodedata.normalize("NFKD", string).encode("ascii", "ignore").decode()
)
string = non_ascii.lower().replace(" ", "_")
@luabida
luabida / get_type.rs
Last active February 19, 2024 17:43
[rs] Get type
fn print_type<T>(_: &T) {
println!("{}", std::any::type_name::<T>())
}