mirror of
https://github.com/BreizhHardware/py_A2.git
synced 2026-01-18 16:47:38 +01:00
TP heritage
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -235,4 +235,6 @@ fabric.properties
|
||||
.idea/httpRequests
|
||||
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
.idea
|
||||
@@ -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)
|
||||
|
||||
133
TP Recursivité/tp.py
Normal file
133
TP Recursivité/tp.py
Normal file
@@ -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))
|
||||
23
TP heritage/Part 1/Circle.py
Normal file
23
TP heritage/Part 1/Circle.py
Normal file
@@ -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
|
||||
8
TP heritage/Part 1/CircleTest.py
Normal file
8
TP heritage/Part 1/CircleTest.py
Normal file
@@ -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()}")
|
||||
30
TP heritage/Part 2/Rectangle.py
Normal file
30
TP heritage/Part 2/Rectangle.py
Normal file
@@ -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
|
||||
9
TP heritage/Part 2/RectangleTest.py
Normal file
9
TP heritage/Part 2/RectangleTest.py
Normal file
@@ -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()}")
|
||||
22
TP heritage/Part 3/Circle.py
Normal file
22
TP heritage/Part 3/Circle.py
Normal file
@@ -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()
|
||||
11
TP heritage/Part 3/CircleTest.py
Normal file
11
TP heritage/Part 3/CircleTest.py
Normal file
@@ -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())
|
||||
16
TP heritage/Part 3/DrawShapeTest.py
Normal file
16
TP heritage/Part 3/DrawShapeTest.py
Normal file
@@ -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()
|
||||
32
TP heritage/Part 3/Rectangle.py
Normal file
32
TP heritage/Part 3/Rectangle.py
Normal file
@@ -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()
|
||||
11
TP heritage/Part 3/RectangleTest.py
Normal file
11
TP heritage/Part 3/RectangleTest.py
Normal file
@@ -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())
|
||||
16
TP heritage/Part 3/Shape.py
Normal file
16
TP heritage/Part 3/Shape.py
Normal file
@@ -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
|
||||
27
TP heritage/Part 4/Circle1.py
Normal file
27
TP heritage/Part 4/Circle1.py
Normal file
@@ -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()
|
||||
18
TP heritage/Part 4/DrawShapeTest1.py
Normal file
18
TP heritage/Part 4/DrawShapeTest1.py
Normal file
@@ -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()
|
||||
37
TP heritage/Part 4/Rectangle1.py
Normal file
37
TP heritage/Part 4/Rectangle1.py
Normal file
@@ -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()
|
||||
37
TP heritage/Part 4/Shape1.py
Normal file
37
TP heritage/Part 4/Shape1.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user