# nom(s) : METTRE VOTRE OU VOS NOMS
from turtle import *
setup(640, 480)
LONGUEUR = 30
# Exercices 1, 2 et 4
def interactif():
rep = ""
instructions = ""
while rep != ".":
rep = input("Nouvelle direction h/b/g/d ? ")
instruction = instructions + rep
if rep == "h":
setheading(90)
forward(LONGUEUR)
elif rep == "b":
setheading(-90)
forward(LONGUEUR)
elif rep == "g":
setheading(180)
forward(LONGUEUR)
elif rep == "d":
setheading(0)
forward(LONGUEUR)
elif rep == "l":
up()
elif rep == "e":
down()
return instructions
# Exercices 3 et 4
def executer(instructions):
for instr in instructions:
if instr == "h":
setheading(90)
forward(LONGUEUR)
elif instr == "b":
setheading(-90)
forward(LONGUEUR)
elif instr == "g":
setheading(180)
forward(LONGUEUR)
elif instr == "d":
setheading(0)
forward(LONGUEUR)
elif instr == "l":
up()
elif instr == "e":
down()
# Exercice 5
def repeter(instructions, n):
return instructions * n
"""
>>> repeter("hg", 4)
'hghghghg'
>>> repeter("hg", 0)
''
"""
# Exercice 6
def sym_h(instructions):
rep = ''
for instr in instructions:
if instr == "h":
rep = rep + "b"
elif instr == "b":
rep = rep + "h"
else: # sinon on ne change pas l'instruction
rep = rep + instr
return rep
"""
>>> sym_h("hhddbg")
'bbddhg'
"""
# Exercice 7
def sym_v(instructions):
rep = ''
for instr in instructions:
if instr == "g":
rep = rep + "d"
elif instr == "d":
rep = rep + "g"
else: # sinon on ne change pas l'instruction
rep = rep + instr
return rep
"""
>>> sym_v('ddhhgb')
'gghhdb'
"""
# Exercice 8
def inverser(instructions):
rep = ''
for instr in instructions:
if instr == 'l':
rep = 'e' + rep
elif instr == 'e':
rep = 'l' + rep
else:
rep = instr + rep
return rep
"""
>>> inverser('hhgggbdd')
'ddbggghh'
>>> inverser('glhheddd')
'dddlhheg'
"""
# Exercice 9
"""
base = "hhdddddbbbggghd"
executer(repeter(base + inverser(base), 5))
"""
# Exercice 10
def sym_h_i(instructions):
rep = ''
for instr in instructions:
if instr == "g":
rep = "d" + rep
elif instr == "d":
rep = "g" + rep
elif instr == 'l':
rep = 'e' + rep
elif instr == 'e':
rep = 'l' + rep
else:
rep = instr + rep
return rep
"""
>>> sym_h_i('ddb')
'bgg'
"""
# Exercice 11
def sym_v_i(instructions):
rep = ''
for instr in instructions:
if instr == "h":
rep = "b" + rep
elif instr == "b":
rep = "h" + rep
elif instr == 'l':
rep = 'e' + rep
elif instr == 'e':
rep = 'l' + rep
else:
rep = instr + rep
return rep
"""
>>> sym_v_i('hdhhd')
'dbbdb'
"""
# Exercice 12
"""
base = 'hhgbghhdddbbd'
base2 = sym_v_i(base)
base3 = sym_h_i(base+base2)
executer(base+base2+base3)
"""