Skip to content

Instantly share code, notes, and snippets.

import random
s, z, n = 0, 0, 0
dx, dy = 0, -1
# Let keep it simple :-)
frog = random.randint(-100, 100)
jump = random.randint(-100, 100)
while 1:
i = s + n * z
n += 1
@fbparis
fbparis / example.log
Last active December 29, 2018 07:44
Coding/decoding any binary data to/from random textual content (here Lorem Ipsum like)
>>> vpn.Encode(r.content)
2018-12-28 15:18:01,876 loremvpn INFO Data inflated by factor 7.33 (2.79 after zlib compression)
'faugiames metrumstietum orcurat metrie rid ortumst lumstiuscilisit teantum te lum har lia har mesuat orpitorpibustrumstietus faugit fames fam cullicumst nenisiscillandumst lignimes pentenentinc mes la cust custorassapisiturascillustacenaestrumst cullucitur ur nea tea acu ac landie orpis orpitorpitiesel ur lend erius fam peliac fam cust netumstostrumstatem nenascibust las mesemes lucillum lassi sem lasciliam faucincitac orcitiesuat culassectos acienia penibuscitienasciostateandui uraesuam daliscit tetrum semese aculutandui orcu lac harcibenisi hasse hassimet pellusci lac faucie orpenti laceliacuscin ortit iduiss orpitiac lemposustortustrum idignimis orci neandio idapiesus selesumstacustiestincur neanduisit liturumstiesellit orpelibendumstienistrutac neturientur har urassem licincid orpeneandit fam penibendumstiscidio se landignimi ida met tetriscieneneandimes lesumstor mesempostrius iding
@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()
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 / 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()
@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 / 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 / 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 / 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 / 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__ = 'fboudot@pm.me'
__license__ = 'IV'
version_info = (0, 0, 1)