Initiation à la programmation ENSAE 1A

interro_rapide_30_minutes_2013_12.tex

question 1.1 : remplacer des accents

		
def remplace_accent(s):
    return s.replace("é","e").replace("à","a").replace("ù","u")
    
def remplace_accent(s):
    s = s.replace("é","e")
    s = s.replace("à","a")
    s = s.replace("ù","u")
    return s
    
def remplace_accent(s):
    for a,b in [('à','a'), ('é','e'), ('ù','u')]:
        s = s.replace(a,b)
    return s

question 1.1 : remplacer des accents sans la fonction replace

		
def remplace_accent(mot):
    liste = list(mot)
    for i in range(len(liste)) :
        if liste[i] == "é" : liste[i] = "e"
        elif liste[i] == "è" : liste[i] = "e"
        elif liste[i] == "à" : liste[i] = "a"
    return "".join(liste)

File: interro_rapide_30_minutes_2013_12.tex, line 70

		
def remplace_accent(mot):
    for i in range(len(mot)) :
        if mot[i] == "é" : mot[i] = "e"
        elif mot[i] == "è" : mot[i] = "e"
        elif mot[i] == "à" : mot[i] = "a"
    return "".join(mot)    

File: interro_rapide_30_minutes_2013_12.tex, line 81


  File "interro_rapide_30_minutes_2013_12.py", line 14, in remplace_accent
    if mot[i] == "é" : mot[i] = "e"
TypeError: 'str' object does not support item assignment

question 1.3 : vérifier que deux mots sont des anagrammes

		
def anagrammes(mot1, mot2):
    mot1 = remplace_accent(mot1)
    mot2 = remplace_accent(mot2)
    return "".join(sorted(mot1)) == "".join(sorted(mot2))

File: interro_rapide_30_minutes_2013_12.tex, line 117


def est_mot_double(s) :
    ...
    return True ou False
		
print (est_mot_double('feraiifare'))  # affiche True

question 2.1 : compter les lettres d'un mot


def frequence_lettre(mot) :
    res = { }
    for c in mot :
        res[c] = res.get(c,0)+1
    return res

question 2.2 : un mot en double exemplaire et plusieurs versions

		
def est_double_mot(mot):
    freq = frequence_lettre(mot)
    for f in freq :
        if freq[f] % 2 != 0 : 
            return False
    return True
		
def est_double_mot(mot):
    freq = frequence_lettre(mot)
    reste = [ v%2 for k,v in freq.items() ]
    return sum(reste) == 0

def est_double_mot(mot):
    return sum( [ v%2 for k,v in  frequence_lettre(mot).items() ] ) == 0

question 2.3 : intercaler deux mots

		
def intercale(mot1,mot2):
    res = ""
    for i in range(len(mot1)) :
        res += mot1[i] + mot2[i]
    return res