Bonjour je met mon 2ème module, une fonction qui cette fois convertie les Chiffre Arabes en Chiffres Romain à sens unique cette fois, toujours pour python 2.7
Attention vous devez impérativement utiliser idle ou autres programme supportant les caractère unicode pour les nombre supérieur à 3.999, car la fonction utilise le caractère unicode 304 en hexadécimal sois 772 en décimal pour le macron des milliers le code
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 # -*- coding: cp1252 -*- def chiffres_romain(n, sep=u"."): s=str(int(n)) l=len(s) def nombre_romain(list_chiffre, res_chiffre, romain, nb_macron, rang, sep): for chiffre_var in list_chiffre: if (4<=res_chiffre<=999 and nb_macron>=1) or (0<=res_chiffre<=999 and nb_macron==0): if rang==0: if 1<=int(chiffre_var)<=3: romain=unicode(("I" + (unichr(772) * nb_macron)) * int(chiffre_var) + romain) rang+=1 elif 4<=int(chiffre_var)<=8: nb=int(chiffre_var)-5 if nb==-1: romain=unicode(("I" + (unichr(772) * nb_macron)) + ("V" + (unichr(772) * nb_macron)) + romain) rang+=1 elif 0<=nb<=3: romain=unicode(("V" + (unichr(772) * nb_macron)) + ("I" + (unichr(772) * nb_macron)) * nb + romain) rang+=1 elif int(chiffre_var)==9: romain=unicode(("I" + (unichr(772) * nb_macron)) + ("X" + (unichr(772) * nb_macron)) + romain) rang+=1 elif int(chiffre_var)==0: romain=unicode(romain) rang+=1 elif rang==1: if 1<=int(chiffre_var)<=3: romain=unicode(("X" + (unichr(772) * nb_macron)) * int(chiffre_var) + romain) rang+=1 elif 4<=int(chiffre_var)<=8: nb=int(chiffre_var)-5 if nb==-1: romain=unicode(("X" + (unichr(772) * nb_macron)) + ((unichr(772) * nb_macron) + "L") + romain) rang+=1 elif 0<=nb<=3: romain=unicode(("L" + (unichr(772) * nb_macron)) + ("X" + (unichr(772) * nb_macron)) * nb + romain) rang+=1 elif int(chiffre_var)==9: romain=unicode(("X" + (unichr(772) * nb_macron)) + ("C" + (unichr(772) * nb_macron)) + romain) rang+=1 elif int(chiffre_var)==0: romain=unicode(romain) rang+=1 elif rang==2: if 1<=int(chiffre_var)<=3: romain=unicode(("C" + (unichr(772) * nb_macron)) * int(chiffre_var) + romain) rang+=1 elif 4<=int(chiffre_var)<=8: nb=int(chiffre_var)-5 if nb==-1: romain=unicode(("C" + (unichr(772) * nb_macron)) + ("D" + (unichr(772) * nb_macron)) + romain) rang+=1 elif 0<=nb<=3: romain=unicode(("D" + (unichr(772) * nb_macron)) + ("C" + (unichr(772) * nb_macron)) * nb + romain) rang+=1 elif int(chiffre_var)==9: romain=unicode(("C" + (unichr(772) * nb_macron)) + ("M" + (unichr(772) * nb_macron)) + romain) rang+=1 elif int(chiffre_var)==0: romain=unicode(romain) rang+=1 elif 1<=res_chiffre<=3 and nb_macron>=1: romain=unicode(sep + ("M" + (unichr(772) * (nb_macron-1))) * res_chiffre + romain) rang=0 return romain nb_macron=0 nc=0 res="" romain=u"" rang=0 for i in range(l-1, -1 ,-1): res=s[i] + res nc+=1 if nc==3: list_chiffre=list(res) list_chiffre.reverse() res_chiffre=int(res) romain=nombre_romain(list_chiffre, res_chiffre, romain, nb_macron, rang, sep) if (4<=res_chiffre<=999 and nb_macron>=1) or (1<=res_chiffre<=999 and nb_macron==0): romain=unicode(sep + romain) elif 1<=res_chiffre<=3 and nb_macron>=1: None nb_macron+=1 nc=0 del res_chiffre del list_chiffre del res res="" try: if 1<=len(res)<=2: list_chiffre=list(res) list_chiffre.reverse() res_chiffre=int(res) romain=nombre_romain(list_chiffre, res_chiffre, romain, nb_macron, rang, sep) del res_chiffre del list_chiffre del res del nc except: None if romain.startswith(sep): romain=romain[1:] if n < 0 and romain[1]==sep: romain=list(romain) del romain[1] romain="".join(romain) return romain








Répondre avec citation

, il est vrai que ton code est plus lisible que le miens je met le nouveau ici et je garde tout de même l'ancien code aux cas ou ça intéresser quelqu'un pour l'ancien code, le nouveau code d'après ton modèle

Partager