From 3409d5c60269097226c31e4f383617b3ab447d2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20MARQUET?= Date: Tue, 7 May 2024 10:55:02 +0200 Subject: [PATCH] TP heritage --- .gitignore | 4 +- TP Nombres Complexes/tp15.py | 16 +++- TP Recursivité/tp.py | 133 +++++++++++++++++++++++++++ TP heritage/Part 1/Circle.py | 23 +++++ TP heritage/Part 1/CircleTest.py | 8 ++ TP heritage/Part 2/Rectangle.py | 30 ++++++ TP heritage/Part 2/RectangleTest.py | 9 ++ TP heritage/Part 3/Circle.py | 22 +++++ TP heritage/Part 3/CircleTest.py | 11 +++ TP heritage/Part 3/DrawShapeTest.py | 16 ++++ TP heritage/Part 3/Rectangle.py | 32 +++++++ TP heritage/Part 3/RectangleTest.py | 11 +++ TP heritage/Part 3/Shape.py | 16 ++++ TP heritage/Part 4/Circle1.py | 27 ++++++ TP heritage/Part 4/DrawShapeTest1.py | 18 ++++ TP heritage/Part 4/Rectangle1.py | 37 ++++++++ TP heritage/Part 4/Shape1.py | 37 ++++++++ 17 files changed, 445 insertions(+), 5 deletions(-) create mode 100644 TP Recursivité/tp.py create mode 100644 TP heritage/Part 1/Circle.py create mode 100644 TP heritage/Part 1/CircleTest.py create mode 100644 TP heritage/Part 2/Rectangle.py create mode 100644 TP heritage/Part 2/RectangleTest.py create mode 100644 TP heritage/Part 3/Circle.py create mode 100644 TP heritage/Part 3/CircleTest.py create mode 100644 TP heritage/Part 3/DrawShapeTest.py create mode 100644 TP heritage/Part 3/Rectangle.py create mode 100644 TP heritage/Part 3/RectangleTest.py create mode 100644 TP heritage/Part 3/Shape.py create mode 100644 TP heritage/Part 4/Circle1.py create mode 100644 TP heritage/Part 4/DrawShapeTest1.py create mode 100644 TP heritage/Part 4/Rectangle1.py create mode 100644 TP heritage/Part 4/Shape1.py diff --git a/.gitignore b/.gitignore index 0ee3ef4..153f15d 100644 --- a/.gitignore +++ b/.gitignore @@ -235,4 +235,6 @@ fabric.properties .idea/httpRequests # Android studio 3.1+ serialized cache file -.idea/caches/build_file_checksums.ser \ No newline at end of file +.idea/caches/build_file_checksums.ser + +.idea \ No newline at end of file diff --git a/TP Nombres Complexes/tp15.py b/TP Nombres Complexes/tp15.py index e454bdc..09fe282 100644 --- a/TP Nombres Complexes/tp15.py +++ b/TP Nombres Complexes/tp15.py @@ -51,7 +51,7 @@ def carre(): turtle.left(90) -def pyntagon(): +def pentagon(): for i in range(5): turtle.forward(100) turtle.left(72) @@ -63,7 +63,7 @@ def hexagon(): turtle.left(60) -def octogon(): +def octogone(): for i in range(8): turtle.forward(100) turtle.left(45) @@ -91,11 +91,19 @@ def triangle_filled_rotation(): time.sleep(0.5) +def dessiner_racine_n_ieme(n): + for i in range(n): + turtle.forward(100) + turtle.left(360 / n) + + # triangle() # carre() -# pyntagon() +# pentagon() # hexagon() -# octogon() +# octogone() # circle() # triangle_filled() triangle_filled_rotation() +# dessiner_racine_n_ieme(5) +# dessiner_racine_n_ieme(900) diff --git a/TP Recursivité/tp.py b/TP Recursivité/tp.py new file mode 100644 index 0000000..86cf1cc --- /dev/null +++ b/TP Recursivité/tp.py @@ -0,0 +1,133 @@ +def mystery1(a, b): + if b == 0: + return a + return mystery1(a + 1, b - 1) + + +def mystery2(l1, l2=None): + if l2 is None: + l2 = [] + if len(l1) == 0: + return l2 + if l1[0] not in l2: + l2.append(l1[0]) + return mystery2(l1[1:], l2) + + +def modulo(x, y): + """Computes x % y recursively """ + if y == 0: + return "Error: Modulo by zero is not possible." + if x < y: + return x + return modulo(x - y, y) + + +def reverseString(s, i=0): + """Reverses the string s recursively """ + if i == len(s): + return "" + else: + return reverseString(s, i + 1) + s[i] + + +def pow(x, n): + """Computes x ** n recursively """ + if n == 0: + return 1 + return x * pow(x, n - 1) + + +def listSum(l): + """Computes the sum of the elements of a list recursively""" + if len(l) == 0: + return 0 + return l[0] + listSum(l[1:]) + + +def product(a, b): + """Computes the product of a and b recursively""" + if b == 0: + return 0 + return a + product(a, b - 1) + + +def even(n): + """Returns True if n is even, False otherwise""" + if n == 0: + return True + if n == 1: + return False + return even(n - 2) + + +def odd(n): + """Returns True if n is odd, False otherwise""" + if n == 0: + return False + if n == 1: + return True + return odd(n - 2) + + +def f1(): + i = f2(2) + return i + + +def f2(j): + k = f3(3 * j) + return k + + +def f3(l): + m = l + 1 + return m + + +def printNumber(i): + print(i) + printNumber(i - 1) + + +print(f1()) +printNumber(3) + +print(mystery1(5, 0)) +print(mystery1(0, 3)) +print(mystery1(2, 9)) +print(mystery1(8, 6)) + +print(mystery2([])) +print(mystery2([1, 1, 1, ])) +print(mystery2([1, 2, 2, 3, 4, 3, 5, 1])) + +# print(42 % 0) +print(modulo(42, 0)) +print(modulo(6, 13)) +print(modulo(37, 10)) + +print(reverseString("")) +print(reverseString("bonjour")) +print(reverseString("ressasser")) + +print(pow(42, 0)) +print(pow(1, 10)) +print(pow(2, 5)) + +print(listSum([])) +print(listSum([42])) +print(listSum([3, 1, 5, 2])) + +print(product(5, 2)) +print(product(9, 3)) + +print(even(0)) +print(even(1)) +print(even(48)) +print(even(49)) + +print(odd(0)) +print(odd(1)) +print(odd(48)) +print(odd(49)) diff --git a/TP heritage/Part 1/Circle.py b/TP heritage/Part 1/Circle.py new file mode 100644 index 0000000..a13980b --- /dev/null +++ b/TP heritage/Part 1/Circle.py @@ -0,0 +1,23 @@ +class Circle: + def __init__(self, x, y, radius): + self.x = x + self.y = y + self.radius = radius + + def getX(self): + return self.x + + def getY(self): + return self.y + + def getRadius(self): + return self.radius + + def setX(self, x): + self.x = x + + def setY(self, y): + self.y = y + + def setRadius(self, radius): + self.radius = radius diff --git a/TP heritage/Part 1/CircleTest.py b/TP heritage/Part 1/CircleTest.py new file mode 100644 index 0000000..56aa352 --- /dev/null +++ b/TP heritage/Part 1/CircleTest.py @@ -0,0 +1,8 @@ +from Circle import Circle + +C1 = Circle(10, 50, 100) +print(f"x {C1.getX()} y {C1.getY()} radius {C1.getRadius()}") +C1.setX(20) +C1.setY(30) +C1.setRadius(200) +print(f"x {C1.getX()} y {C1.getY()} radius {C1.getRadius()}") \ No newline at end of file diff --git a/TP heritage/Part 2/Rectangle.py b/TP heritage/Part 2/Rectangle.py new file mode 100644 index 0000000..7653c6f --- /dev/null +++ b/TP heritage/Part 2/Rectangle.py @@ -0,0 +1,30 @@ +class Rectangle: + def __init__(self, x, y, height, width): + self.x = x + self.y = y + self.height = height + self.width = width + + def getX(self): + return self.x + + def getY(self): + return self.y + + def getHeight(self): + return self.height + + def getWidth(self): + return self.width + + def setX(self, x): + self.x = x + + def setY(self, y): + self.y = y + + def setHeight(self, height): + self.height = height + + def setWidth(self, width): + self.width = width diff --git a/TP heritage/Part 2/RectangleTest.py b/TP heritage/Part 2/RectangleTest.py new file mode 100644 index 0000000..318883e --- /dev/null +++ b/TP heritage/Part 2/RectangleTest.py @@ -0,0 +1,9 @@ +from Rectangle import Rectangle + +R1 = Rectangle(10, 50, 40, 50) +print(f"x {R1.getX()} y {R1.getY()} width {R1.getWidth()} height {R1.getHeight()}") +R1.setX(20) +R1.setY(30) +R1.setWidth(50) +R1.setHeight(100) +print(f"x {R1.getX()} y {R1.getY()} width {R1.getWidth()} height {R1.getHeight()}") \ No newline at end of file diff --git a/TP heritage/Part 3/Circle.py b/TP heritage/Part 3/Circle.py new file mode 100644 index 0000000..f4cb813 --- /dev/null +++ b/TP heritage/Part 3/Circle.py @@ -0,0 +1,22 @@ +from Shape import Shape + + +class Circle(Shape): + def __init__(self, x, y, radius): + Shape.__init__(self, x, y) + self.radius = radius + + def getRadius(self): + return self.radius + + def setRadius(self, radius): + self.radius = radius + + def draw(self, turtle): + turtle.penup() + turtle.goto(self.x, self.y - self.radius) + turtle.pendown() + turtle.circle(self.radius) + turtle.penup() + turtle.goto(self.x, self.y) + turtle.pendown() \ No newline at end of file diff --git a/TP heritage/Part 3/CircleTest.py b/TP heritage/Part 3/CircleTest.py new file mode 100644 index 0000000..4d1fbcb --- /dev/null +++ b/TP heritage/Part 3/CircleTest.py @@ -0,0 +1,11 @@ +from Circle import Circle +from turtle import Turtle + + +C1 = Circle(10, 50, 100) +print(f"x {C1.getX()} y {C1.getY()} radius {C1.getRadius()}") +C1.setX(20) +C1.setY(30) +C1.setRadius(200) +print(f"x {C1.getX()} y {C1.getY()} radius {C1.getRadius()}") +C1.draw(Turtle()) \ No newline at end of file diff --git a/TP heritage/Part 3/DrawShapeTest.py b/TP heritage/Part 3/DrawShapeTest.py new file mode 100644 index 0000000..5fb0e7b --- /dev/null +++ b/TP heritage/Part 3/DrawShapeTest.py @@ -0,0 +1,16 @@ +from Circle import Circle +from Rectangle import Rectangle +from turtle import Turtle + + +def drawShapes(shapes, turtle): + for shape in shapes: + shape.draw(turtle) + + +turtle = Turtle() +c = Circle(x=0, y=100, radius=50) +r = Rectangle(x=0, y=-100, width=50, height=200) +shapes = [c, r] +drawShapes(shapes, turtle) +turtle.exitonclick() \ No newline at end of file diff --git a/TP heritage/Part 3/Rectangle.py b/TP heritage/Part 3/Rectangle.py new file mode 100644 index 0000000..863fbab --- /dev/null +++ b/TP heritage/Part 3/Rectangle.py @@ -0,0 +1,32 @@ +from Shape import Shape + + +class Rectangle(Shape): + def __init__(self, x, y, width, height): + Shape.__init__(self, x, y) + self.width = width + self.height = height + + def getWidth(self): + return self.width + + def getHeight(self): + return self.height + + def setWidth(self, width): + self.width = width + + def setHeight(self, height): + self.height = height + + def draw(self, turtle): + turtle.penup() + turtle.goto(self.x - self.width // 2, self.y - self.height // 2) + turtle.pendown() + turtle.goto(self.x + self.width // 2, self.y - self.height // 2) + turtle.goto(self.x + self.width // 2, self.y + self.height // 2) + turtle.goto(self.x - self.width // 2, self.y + self.height // 2) + turtle.goto(self.x - self.width // 2, self.y - self.height // 2) + turtle.penup() + turtle.goto(self.x, self.y) + turtle.pendown() \ No newline at end of file diff --git a/TP heritage/Part 3/RectangleTest.py b/TP heritage/Part 3/RectangleTest.py new file mode 100644 index 0000000..5cff60d --- /dev/null +++ b/TP heritage/Part 3/RectangleTest.py @@ -0,0 +1,11 @@ +from Rectangle import Rectangle +from turtle import Turtle + +R1 = Rectangle(10, 50, 40, 50) +print(f"x {R1.getX()} y {R1.getY()} width {R1.getWidth()} height {R1.getHeight()}") +R1.setX(20) +R1.setY(30) +R1.setWidth(50) +R1.setHeight(100) +print(f"x {R1.getX()} y {R1.getY()} width {R1.getWidth()} height {R1.getHeight()}") +R1.draw(Turtle()) \ No newline at end of file diff --git a/TP heritage/Part 3/Shape.py b/TP heritage/Part 3/Shape.py new file mode 100644 index 0000000..db1fa17 --- /dev/null +++ b/TP heritage/Part 3/Shape.py @@ -0,0 +1,16 @@ +class Shape: + def __init__(self, x, y): + self.x = x + self.y = y + + def getX(self): + return self.x + + def getY(self): + return self.y + + def setX(self, x): + self.x = x + + def setY(self, y): + self.y = y diff --git a/TP heritage/Part 4/Circle1.py b/TP heritage/Part 4/Circle1.py new file mode 100644 index 0000000..6b254e7 --- /dev/null +++ b/TP heritage/Part 4/Circle1.py @@ -0,0 +1,27 @@ +from Shape1 import Shape1 + + +class Circle1(Shape1): + def __init__(self, x, y, radius, lineThickness=6, lineColor="red", fillColor="green"): + Shape1.__init__(self, x, y, lineThickness, lineColor, fillColor) + self.radius = radius + + def getRadius(self): + return self.radius + + def setRadius(self, radius): + self.radius = radius + + def draw(self, turtle): + turtle.width(self.lineThickness) + turtle.pencolor(self.lineColor) + turtle.fillcolor(self.fillColor) + turtle.penup() + turtle.goto(self.x, self.y - self.radius) + turtle.pendown() + turtle.begin_fill() + turtle.circle(self.radius) + turtle.end_fill() + turtle.penup() + turtle.goto(self.x, self.y) + turtle.pendown() \ No newline at end of file diff --git a/TP heritage/Part 4/DrawShapeTest1.py b/TP heritage/Part 4/DrawShapeTest1.py new file mode 100644 index 0000000..a96691e --- /dev/null +++ b/TP heritage/Part 4/DrawShapeTest1.py @@ -0,0 +1,18 @@ +from Circle1 import Circle1 +from Rectangle1 import Rectangle1 +from turtle import Turtle + + +def drawShapes(shapes, turtle): + for shape in shapes: + shape.draw(turtle) + + +turtle = Turtle() +c = Circle1(x=0, y=100, radius=50, lineThickness=3, + lineColor="red", fillColor="orange") +r = Rectangle1(x=0, y=-100, width=50, height=200, + lineThickness=5, lineColor="blue", fillColor="cyan") +shapes = [c, r] +drawShapes(shapes, turtle) +turtle.exitonclick() \ No newline at end of file diff --git a/TP heritage/Part 4/Rectangle1.py b/TP heritage/Part 4/Rectangle1.py new file mode 100644 index 0000000..f69f5de --- /dev/null +++ b/TP heritage/Part 4/Rectangle1.py @@ -0,0 +1,37 @@ +from Shape1 import Shape1 + + +class Rectangle1(Shape1): + def __init__(self, x, y, width, height, lineThickness=2, lineColor="blue", fillColor="yellow"): + Shape1.__init__(self, x, y, lineThickness, lineColor, fillColor) + self.width = width + self.height = height + + def getWidth(self): + return self.width + + def getHeight(self): + return self.height + + def setWidth(self, width): + self.width = width + + def setHeight(self, height): + self.height = height + + def draw(self, turtle): + turtle.width(self.lineThickness) + turtle.pencolor(self.lineColor) + turtle.fillcolor(self.fillColor) + turtle.penup() + turtle.goto(self.x - self.width // 2, self.y - self.height // 2) + turtle.begin_fill() + turtle.pendown() + turtle.goto(self.x + self.width // 2, self.y - self.height // 2) + turtle.goto(self.x + self.width // 2, self.y + self.height // 2) + turtle.goto(self.x - self.width // 2, self.y + self.height // 2) + turtle.goto(self.x - self.width // 2, self.y - self.height // 2) + turtle.end_fill() + turtle.penup() + turtle.goto(self.x, self.y) + turtle.pendown() \ No newline at end of file diff --git a/TP heritage/Part 4/Shape1.py b/TP heritage/Part 4/Shape1.py new file mode 100644 index 0000000..fce14f6 --- /dev/null +++ b/TP heritage/Part 4/Shape1.py @@ -0,0 +1,37 @@ +class Shape1: + def __init__(self, x, y, lineThickness = 1, lineColor = "black", fillColor = "white"): + self.x = x + self.y = y + self.lineThickness = lineThickness + self.lineColor = lineColor + self.fillColor = fillColor + + def getX(self): + return self.x + + def getY(self): + return self.y + + def setX(self, x): + self.x = x + + def setY(self, y): + self.y = y + + def getLineThickness(self): + return self.lineThickness + + def setLineThickness(self, lineThickness): + self.lineThickness = lineThickness + + def getLineColor(self): + return self.lineColor + + def setLineColor(self, lineColor): + self.lineColor = lineColor + + def getFillColor(self): + return self.fillColor + + def setFillColor(self, fillColor): + self.fillColor = fillColor \ No newline at end of file