Codice Gray: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Alfiobot (discussione | contributi)
m Bot: Sostituzione automatica (-[[Image: +[[Immagine:)
Nessun oggetto della modifica
Riga 104:
 
<br clear = all />
== Implementazione in linguaggio Python ==
 
<pre>
_xor = {("0", "0"): "0",
("0", "1"): "1",
("1", "0"): "1",
("1", "1"): "0"}
 
def tograystr(binary):
prec = binary[0]
result = prec
for el in binary[1:]:
result += _xor[el, prec]
prec = el
return result
 
def tobinarystr(gray):
result = gray[0]
prec = result
for el in gray[1:]:
prec = _xor[prec, el]
result += prec
return result
</pre>
Esempio d'uso dalla shell Python:
<pre>
>>> bins = ['000', '001', '010', '011', '100', '101', '110', '111']
>>> grays = [tograystr(b) for b in bins]
>>> grays
['000', '001', '011', '010', '110', '111', '101', '100']
>>> [tobinarystr(g) for g in grays]
['000', '001', '010', '011', '100', '101', '110', '111']
</pre>
== Voci correlate ==
*[[Storia dei charset]]