Voilà le code , avec certains tests :
La méthode .swapcase() simplifie beaucoup la vie
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 >>> def pluriel_ail(mot) : ail= 'bail corail émail soupirail travail ventail vitrail'.split() if mot in ail : return mot[0 : -2] + 'ux' >>> pluriel_ail('bail') 'baux' >>> pluriel_ail('travail') 'travaux' >>> def pluriel_ou(mot) : ou = 'hibou chou genou caillou pou bijou'.split() if mot in ou : return mot + 'x' >>> pluriel_ou('hibou') 'hiboux' >>> def pluriel_eu(mot) : eu = 'pneu bleu'.split() if mot in eu : return mot + 's' elif mot[-2:] == 'eu' : return (mot+'x') >>> pluriel_eu('pneu') 'pneus' >>> pluriel_eu('cheveu') 'cheveux' >>> def pluriel_al(mot) : al = 'banal fatal naval natal bancal bal festival chacal carnaval cal serval'.split() if mot in al : return mot + 's' >>> def pluriel_au(mot): au = 'landau sarrau'.split() if mot in au : return mot + 's' elif mot[-2:] == 'au' : return (mot+'x') >>> def pluriel_except(mot): if mot== 'oeil' : return ('yeux') elif mot == 'ail' : return ('aulx') elif mot[-1] == 'z' or mot[-1] == 'x' : return (mot) >>> def _plurial(mot): functions = [ pluriel_ail, pluriel_ou, pluriel_eu, pluriel_al, pluriel_au, pluriel_except ] for f in functions: m = f(mot) if m: return m >>> _plurial('Hibou') >>> _plurial('hibou') 'hiboux' >>> def plurial(mot): m = mot.lower() r = _plurial(m) if m != mot: r = r.swapcase() return r >>> plurial('Hibou') 'HIBOUX' >>> plurial('oeil') 'yeux' >>> plurial('ail') 'aulx' >>> plurial('AIL') 'AULX'elle remet tout soit en majuscule ou en minuscule .
Tu as utilisé ces liens wiztricks ,non ?http://docs.python.org/2/library/str...tring.swapcase https://github.com/epeli/underscore.string/pull/125 http://docs.python.org/3/library/std...l#str.swapcase
_La méthode .title() peut être intéressante pour des phrases :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 >>> "they're bill's friends from the UK".title() "They'Re Bill'S Friends From The Uk"
_Mais le sens par exemple de l'expression suivante : 'rt'.swapcase().swapcase() == 'rt' retourne True , c'est un peu comme çà en gros que fonctionne la dernière fonction plurial(mot) et retourne une valeur du mot au pluriel.
Partager