converting letters to numbers

Robert Day robertkday at gmail.com
Tue Oct 8 10:47:39 EDT 2013


On 08/10/13 15:28, kjakupak at gmail.com wrote:
> I have to define a function add(c1, c2), where c1 and c2 are capital letters; the return value should be the sum (obtained by converting the letters to numbers, adding mod 26, then converting back to a capital letter).
>
Can you give some expected outputs? For example, add('A', 'B') should 
presumably return 'C', and add('M', 'B') should presumably return 'O', 
but what about add('A', 'A') or add('Z', 'Z')?

It feels like the only tricky bit is mapping letters to numbers (i.e. 
does A equal 1 or 0?), which you'd do by subtracting a fixed value from 
the result of chr. Once you've done that, you'd do the arithmetic to get 
a number between 1 and 26 (or 0 and 25), then add the same fixed value 
to that and call ord on the result.



More information about the Python-list mailing list