Notebook originel: exos_python.ipynb
Exercices Python🔗
Introduction🔗
Intégration: méthode des rectangles🔗
[1]:
def integ_rectangles(f, a, b, n=100):
"""
Retourne une évaluation de l'intégrale de la fonction `f` entre `a` et `b`
par la méthode des rectangles, avec `n` pas.
"""
return 0
Fizz-Buzz🔗
[2]:
def fizzbuzz(n):
"""
Retourne une chaîne de caractères à imprimer.
"""
return ''
PGCD: algorithme d’Euclide🔗
[3]:
def pgcd(a, b):
"""
Calcul du PGCD de deux entiers 0 < b < a.
"""
return 0
Manipulation de listes🔗
Crible d’Ératosthène🔗
[4]:
def erathostene(n):
"""
Retourne la liste des entiers premiers ≤ n.
"""
return []
Carré magique🔗
[5]:
def carremagique(n):
"""
Retourne le carré magique d'ordre n (impair).
"""
return [[]]
Programmation🔗
Suite de Syracuse🔗
[6]:
def suite_syracuse(n):
"""
Retourne la suite de Syracuse d'ordre n.
"""
return []
def temps_syracuse(n, altitude=False):
"""
Retourne le temps de vol (total ou en altitude) de la suite de Syracuse d'ordre n.
"""
return 0
Tests de correction🔗
NE PAS MODIFIER/EFFACER!
[7]:
import unittest
class TestNotebook(unittest.TestCase):
def test_01_integ_rectangles(self):
self.assertAlmostEqual(integ_rectangles(lambda x: x**2, 0, 1, 100), 1/3, places=4)
def test_02_fizzbuzz(self):
self.assertEqual(fizzbuzz(16),
"1 2 Fizz! 4 Buzz! Fizz! 7 8 Fizz! Buzz! 11 Fizz! 13 14 Fizz Buzz! 16")
def test_03_pgcd(self):
self.assertEqual(pgcd(756, 306), 18)
with self.assertRaises(AssertionError):
pgcd(306, 756)
def test_04_erathostene(self):
self.assertEqual(erathostene(41),
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41])
def test_05_carremagique(self):
carre = [[11, 18, 25, 2, 9],
[10, 12, 19, 21, 3],
[4, 6, 13, 20, 22],
[23, 5, 7, 14, 16],
[17, 24, 1, 8, 15]]
self.assertEqual(carremagique(5), carre)
with self.assertRaises(AssertionError):
carremagique(6)
def test_06_syracuse(self):
self.assertEqual(suite_syracuse(15),
[15, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1])
self.assertEqual(temps_syracuse(15), 17)
self.assertEqual(temps_syracuse(15, altitude=True), 11)
unittest.main(argv=[''], verbosity=2, exit=False)
test_01_integ_rectangles (__main__.TestNotebook) ... FAIL
test_02_fizzbuzz (__main__.TestNotebook) ... FAIL
test_03_pgcd (__main__.TestNotebook) ... FAIL
test_04_erathostene (__main__.TestNotebook) ... FAIL
test_05_carremagique (__main__.TestNotebook) ... FAIL
test_06_syracuse (__main__.TestNotebook) ... FAIL
======================================================================
FAIL: test_01_integ_rectangles (__main__.TestNotebook)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/ipykernel_2267707/903517605.py", line 6, in test_01_integ_rectangles
self.assertAlmostEqual(integ_rectangles(lambda x: x**2, 0, 1, 100), 1/3, places=4)
AssertionError: 0 != 0.3333333333333333 within 4 places (0.3333333333333333 difference)
======================================================================
FAIL: test_02_fizzbuzz (__main__.TestNotebook)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/ipykernel_2267707/903517605.py", line 9, in test_02_fizzbuzz
self.assertEqual(fizzbuzz(16),
AssertionError: '' != '1 2 Fizz! 4 Buzz! Fizz! 7 8 Fizz! Buzz! 11 Fizz! 13 14 Fizz Buzz! 16'
+ 1 2 Fizz! 4 Buzz! Fizz! 7 8 Fizz! Buzz! 11 Fizz! 13 14 Fizz Buzz! 16
======================================================================
FAIL: test_03_pgcd (__main__.TestNotebook)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/ipykernel_2267707/903517605.py", line 13, in test_03_pgcd
self.assertEqual(pgcd(756, 306), 18)
AssertionError: 0 != 18
======================================================================
FAIL: test_04_erathostene (__main__.TestNotebook)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/ipykernel_2267707/903517605.py", line 18, in test_04_erathostene
self.assertEqual(erathostene(41),
AssertionError: Lists differ: [] != [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]
Second list contains 13 additional elements.
First extra element 0:
2
- []
+ [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]
======================================================================
FAIL: test_05_carremagique (__main__.TestNotebook)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/ipykernel_2267707/903517605.py", line 27, in test_05_carremagique
self.assertEqual(carremagique(5), carre)
AssertionError: Lists differ: [[]] != [[11, 18, 25, 2, 9], [10, 12, 19, 21, 3], [[53 chars] 15]]
First differing element 0:
[]
[11, 18, 25, 2, 9]
Second list contains 4 additional elements.
First extra element 1:
[10, 12, 19, 21, 3]
- [[]]
+ [[11, 18, 25, 2, 9],
+ [10, 12, 19, 21, 3],
+ [4, 6, 13, 20, 22],
+ [23, 5, 7, 14, 16],
+ [17, 24, 1, 8, 15]]
======================================================================
FAIL: test_06_syracuse (__main__.TestNotebook)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/ipykernel_2267707/903517605.py", line 32, in test_06_syracuse
self.assertEqual(suite_syracuse(15),
AssertionError: Lists differ: [] != [15, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1]
Second list contains 18 additional elements.
First extra element 0:
15
- []
+ [15, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1]
----------------------------------------------------------------------
Ran 6 tests in 0.006s
FAILED (failures=6)
[7]:
<unittest.main.TestProgram at 0x7f0b6978a9d0>
[ ]:
Cette page a été générée à partir de
exos_python.ipynb
.