cours: n°2

This commit is contained in:
Raito Bezarius 2020-06-17 17:30:19 +02:00
parent 2dbcc1d3a6
commit c8fafe2ce3
13 changed files with 230 additions and 0 deletions

View file

@ -0,0 +1 @@
use nix

View file

@ -0,0 +1,24 @@
let nixpkgs = <nixpkgs>;
in
{
pkgs ? import nixpkgs {}
, pythonPackageName ? "python3"
, python ? pkgs.${pythonPackageName}}:
rec {
pythonDependencies = (python.withPackages
(ps: [
ps.pytest
ps.pytest_xdist
ps.pytest-mock
ps.pytestcov
ps.hypothesis
]));
shell = pkgs.mkShell {
buildInputs = with pkgs; [
pythonDependencies
];
};
}

View file

@ -0,0 +1,2 @@
[pytest]
addopts = --doctest-modules

View file

@ -0,0 +1 @@
(import ./default.nix {}).shell

View file

@ -0,0 +1,24 @@
import os
import tempfile
class UnixFS:
@staticmethod
def rm(filename):
os.remove(filename)
# Sans intégration
def test_monkeypatche(mocker):
mocker.patch('os.remove')
UnixFS.rm('contrôle complet')
os.remove.assert_called_once_with('contrôle complet')
# Avec intégration
def test_db_with_db():
with tempfile.NamedTemporaryFile(delete=False) as fp:
fp.write(b'Bonjour!')
fp.seek(0)
assert fp.read() == b'Bonjour!'
UnixFS.rm(fp.name)
# assert que le fichier a été supprimé.

View file

@ -0,0 +1,35 @@
from hypothesis import given
from hypothesis.strategies import text
def encode(input_string):
count = 1
prev = ''
lst = []
for character in input_string:
if character != prev:
if prev:
entry = (prev,count)
lst.append(entry)
#print lst
count = 1
prev = character
else:
count += 1
else:
try:
entry = (character,count)
lst.append(entry)
return (lst, 0)
except Exception as e:
print("Exception encountered {e}".format(e=e))
return (e, 1)
def decode(lst):
q = ""
for character, count in lst:
q += character * count
return q
@given(text())
def test_decode_inverts_encode(s):
assert decode(encode(s)) == s

View file

@ -0,0 +1,32 @@
from hypothesis import given, assume
from hypothesis.strategies import lists, permutations
from collections import Counter, defaultdict
# Ici, on va observer un paradoxe classique en théorie des votes juste en utilisant Hypothesis.
# Imaginez, ici que vous fabriquez un algorithme de couplage, type APB/Admissions Parallèles/Parcoursup.
# On se donne par Hypothesis au moins trois candidats et trois voteurs.
@given(lists(permutations(["A", "B", "C"]), min_size=3))
def test_condorcet_paradox(election):
all_candidates = {"A", "B", "C"}
# On calcule les préférences paires à paires (u, v) de chaque candidat.
counts = Counter()
for vote in election:
for i, vi in enumerate(vote):
for j in range(i + 1, len(vote)):
counts[(vi, vote[j])] += 1
# On regarde quelle paires de candidats a la majorité.
graph = defaultdict(set)
for i in all_candidates:
for j in all_candidates:
if counts[(i, j)] > counts[(j, i)]:
graph[i].add(j)
# Maintenant, on vérifie que les préférences sont transitives, i.e. si A préfère B, B préfère C, alors A préfère C non?
for x in all_candidates:
for y in graph[x]:
for z in graph[y]:
# vérifions que x n'est pas dans graph[z]
assert x not in graph[z]

View file

@ -0,0 +1,15 @@
def est_premier(n):
"""
>>> est_premier(3)
True
>>> est_premier(5)
True
>>> est_premier(4)
True
"""
for k in range(n):
if k % n == 0:
return False
return True