
################
##            ##
## Exercice 4 ##
##            ##
################
print("\nExercice 4\n")

## Question 4.1

def décoder(chaîne):
    L = [0] * len(chaîne)
    for i in range(len(chaîne)):
        L[i] = ord(chaîne[i])
    return L

## Question 4.2

def est_ascii(chaîne):
    L = décoder(chaîne)
    for i in range(len(chaîne)):
        if L[i] > 127:
            return False
    return True


## Question 4.3

def recoder(liste):
    c = ""
    for i in range(len(liste)):
        if liste[i] < 128:
            c = c + chr(liste[i])
        else:
            c = c + "?"
    return c




################
##            ##
## Exercice 5 ##
##            ##
################
print("\nExercice 5\n")


## Question 5.1

def HURLER(chaîne):
    r = ""
    for i in range(len(chaîne)):
        d = ord(chaîne[i])
        if ord('a') <= d and d <= ord('z'):
            d = d - ord('a') + ord('A')
        r = r + chr(d)
    return r

print(HURLER("Parle plus fort j’entends rien !"))

## Question 5.2

def murmurer(chaîne):
    r = ""
    for i in range(len(chaîne)):
        d = ord(chaîne[i])
        if ord('A') <= d and d <= ord('Z'):
            d = d - ord('A') + ord('a')
        r = r + chr(d)
    return r

print(murmurer("CHUT… Le Prof nous regarde…"))

## Question 5.3


def majuscule(c):
    return ord("A") <= ord(c) and ord(c) <= ord("Z")

def minuscule(c):
    return ord("a") <= ord(c) and ord(c) <= ord("z")

def est_lettre(c):
    return majuscule(c) or minuscule(c)

def title(ch):
    ancien = False # Le caractère précédent est-il une lettre ?
    res = ""
    for i in range(len(ch)):
        if not ancien: # Si je suis en début de mot
            res = res + ch[i].upper()
        else:
            res = res + ch[i]
        ancien = est_lettre(ch[i])
    return res

print(title("the beauty and the beast"))
print(title("the beauty and spider-man (first spin-off)"))



################
##            ##
## Exercice 6 ##
##            ##
################

def lire_nombre(n, base):
    if n=="": return 0
    return int(n) # pour que le code fonctionne. En vrai il fait
                  # prendre la fonction écrite dans les premiers
                  # exercices

def est_chiffre(c):
    return ord("0") <= ord(c) and ord(c) <= ord("9")

def nettoyer(ch):
    """On ne conserve que les chiffres et les virgules"""
    rés = ""
    for i in range(len(ch)):
        if est_chiffre(ch[i]) or ch[i] == ",":
            rés += ch[i]
    return rés

def nombre_de_virgules(ch):
    n = 0
    for i in range(len(ch)):
        if ch[i] == ",":
            n = n+1
    return n


def chaîne_vers_liste(ch):
    ch = nettoyer(ch)
    # On gère à part le cas de la liste vide
    if ch== "":
        return []

    # Dans le tableau il y a une case de plus que de virgules
    tab = [0] * (1+nombre_de_virgules(ch))
    # k est l’indice courant du tableau
    k = 0
    # courant est la chaîne contenant les caractères du prochain nombre
    courant = ""

    for i in range(len(ch)):
        if ch[i] == ",":
            tab[k] = lire_nombre(courant, 10) # cf. exercice 2
            courant = ""
            k = k+1
        else:
            courant = courant + ch[i]
    tab[k] = lire_nombre(courant, 10) # cf. exercice 2
    return tab


chaîne = "[12,23,  24, 2  , 1]"
print(nettoyer(chaîne))

assert chaîne_vers_liste(chaîne) == [12, 23, 24, 2, 1]
assert chaîne_vers_liste("[]")   == []
assert chaîne_vers_liste("[12]") == [12]

# Fonctionne mal si les données ne sont pas propres
chaîne_vers_liste("[-12.3, 'salut', 45 56,]")
