Skip to content

Instantly share code, notes, and snippets.

@fbparis
fbparis / floot.py
Last active November 28, 2020 16:34
Remplace les visages par des anus
"""
Librement inspiré de https://twitter.com/IllumiReptilien/status/1325009440026877953
anus.png peut être récupéré ici : https://github.com/fbparis/floot/blob/main/anus.png
"""
# uncomment in colab
# !pip install mtcnn
import cv2
import mtcnn
@fbparis
fbparis / RandomUpperConfidenceInterval.py
Created July 10, 2020 06:44
Very good multi K-Armed Bandits solver, no need parameter!
class RandomUCI():
"""Random Upper Confidence Interval
"""
def __init__(self, bandit):
self.bandit = bandit
self.arm_count = bandit.arm_count
self.mean = np.ones(self.arm_count) / 2
self.variance = np.ones(self.arm_count) / 4
self.N = np.ones(self.arm_count)
@fbparis
fbparis / newfile.py
Created June 4, 2020 13:16
Base python3 template :)
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
"""
"""
__title__ = __file__.rsplit('.', 1)[0].title()
__author__ = '[email protected]'
__license__ = 'IV'
version_info = (0, 0, 1)
@fbparis
fbparis / text-extraction.py
Last active June 2, 2020 15:38
extract text content from a source
# -*- coding: utf-8 -*-
"""
New kind of text extractor:
Phase 1:
- remove scripts css svg comments canvas etc
- add space after each html tag
- strip tags
- normalize spaces/newlines
- keep the result
Phase 2:
@fbparis
fbparis / animal_crossing_village.py
Last active July 25, 2021 17:14
Animal Crossing New Horizons happy villagers
"""
Use the formula found on https://nookipedia.com/wiki/Compatibility (slightly modified) to compute Animal Crossing New Horizons village compositions optimizing villagers happiness.
Compatibility score between 2 villagers is a grade from 0 (worst) to 4 (best) and village score is the sum of every combinations of 2 villagers score in the village.
You can pass a required list of villagers (using their english ids or french names) to find the ideal villagers to recrute next.
See --help for options. -m3 -r2 are usualy good parameters.
Note that the program never stops so you will have to hit CTRL-C when you're done!
@fbparis
fbparis / niya.py
Last active April 16, 2020 14:47
Simulate niya games with alternative rules
from itertools import product, combinations
from random import sample
# 0 1 2 3
# 4 5 6 7
# 8 9 A B
# C D E F
cards = list(product('ABCD', '0123', repeat=1))
@fbparis
fbparis / kik.py
Last active June 2, 2020 00:18
A replacement for the indexed trie data-structure with a very low memory footprint!
"""
kik (key to index to key) is a new implentation of indexed trie (https://gist.github.com/fbparis/e114958a4a9be84146a1a579cf677e8d) reducing memory footprint by a factor 7 or more!
The main differences with indexed tries are:
* Trie nodes are no longer stored in Node objects but in a python list. An internal mechanism use the list to simulate a tree behaviour, without any recursive function.
* Nodes and values stored in the kik can be compressed with several compression levels:
* None: no compression at all
* 0: data are stored as pickle.dumps (memory use divided by 3-4)
* -1, 1 to 9: pickle.dumps are compressed with zlib
* Some internal methods can be cached with a custom implentation of a LRU cache.
@fbparis
fbparis / indexedtrie.py
Last active March 12, 2019 01:25
A very powerful and memory safe data-structure to replace python's dict: Indexed Radix Trie
"""
A Python3 indexed trie class.
An indexed trie's key can be any subscriptable object.
Keys of the indexed trie are stored using a "radix trie", a space-optimized data-structure which has many advantages (see https://en.wikipedia.org/wiki/Radix_tree).
Also, each key in the indexed trie is associated to a unique index which is build dynamically.
Indexed trie is used like a python dictionary (and even a collections.defaultdict if you want to) but its values can also be accessed or updated (but not created) like a list!
Example:
>>> t = indextrie()
import os
from math import factorial
from random import seed, randrange
class PerfectCipher(object):
"""
Each byte (0-255) of a message is replaced using a translation table which is a permutation of [0..255].
After each replacement a new translation table is used using a Linear Congruential Generator to act as a PRNG:
Next N = (a * N + c) % m
Where m is 256! (total count of [0..255] permutations) and a and c carefully chosen so that the period of the PRNG is equal to m.
@fbparis
fbparis / lorem-client.py
Last active December 29, 2018 07:45
Client and Server using LoremVPN protocol
import sys
import socket
import zlib
from loremvpn import LoremVPN
if __name__ == '__main__':
VPN = LoremVPN("demo", "demo")
HOST, PORT = "localhost", 9999
data = " ".join(sys.argv[1:]).encode()