converting letters to numbers

K

kjakupak

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).

All I have so far is:

def add(c1, c2):
ord(c1) - ord('a') + 1
ord(c2) - ord('a') + 1

I know I need to use ord and chr, just not sure how.
 
R

Robert Day

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.
 
K

kjakupak

On 08/10/13 15:28, (e-mail address removed) wrote:

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.

Expected output is add('C', 'E') returns 'G'; where 'C' and 'E' correspond to 2 and 4 respectively with sum 6, corresponding to 'G'.
 
R

random832

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).

All I have so far is:

def add(c1, c2):
ord(c1) - ord('a') + 1
ord(c2) - ord('a') + 1

I know I need to use ord and chr, just not sure how.

Your description says capital letters, but 'a' is a lowercase letter.

Does "mod 26" means A is 1, or is it 0? i.e., is A+A = B or is it A?

What should your function do if the letter isn't a capital letter from
the basic set of 26 English letters?
 
K

kjakupak

Your description says capital letters, but 'a' is a lowercase letter.



Does "mod 26" means A is 1, or is it 0? i.e., is A+A = B or is it A?



What should your function do if the letter isn't a capital letter from

the basic set of 26 English letters?

A is 0.

Transfer it to an uppercase letter if it's a letter, if it's not then an error.
This isn't right, I know, just testing around

def add(c1, c2):
ans = ''
for i in c1 + c2:
ans += chr((((ord(i)-65))%26) + 65)
return ans
 
M

Mark Lawrence

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).

I'd say the requirement is lacking in that no encoding is specified.
All I have so far is:

def add(c1, c2):
ord(c1) - ord('a') + 1
ord(c2) - ord('a') + 1

I know I need to use ord and chr, just not sure how.

I'll further observe from your later replies that you're suffering from
the highly contagious, highly virulent double line spacing disease.
This is known to cause severe eye strain leading to blindness. In can
be cured by purchasing medication here
https://wiki.python.org/moin/GoogleGroupsPython

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence
 
D

Dave Angel

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).

All I have so far is:

def add(c1, c2):
ord(c1) - ord('a') + 1
ord(c2) - ord('a') + 1

I know I need to use ord and chr, just not sure how.


Factor the problem into three functions. one function converts a
one-character string into an int, or gives an exception if the
character. isn't uppercase ASCII.

Second function converts a small int into a string containing one
uppercase ASCII letter, throwing an exception if negative or above 25.

Third function takes two string arguents, throws an exception if either
of them is not exactly one character in length. Then it calls the first
function twice, adds the results, modulos it, and calls the second
function, returning its return value.

Which of these is giving you trouble? Notice you can use the first two
functions to test each other.
 
T

Tim Roberts

Transfer it to an uppercase letter if it's a letter, if it's not then an error.
This isn't right, I know, just testing around

def add(c1, c2):
ans = ''
for i in c1 + c2:
ans += chr((((ord(i)-65))%26) + 65)
return ans

It's close. I think you're overthinking it. Take it step by step. Decode,
process, encode. That means convert the inputs to integers, add the
integers, convert the result back.

def add(c1, c2):
% Decode
c1 = ord(c1) - 65
c2 = ord(c2) - 65
% Process
i1 = (c1 + c2) % 26
% Encode
return chr(i1+65)

Or, as a one-liner:

A = ord('A')
def add(c1, c2):
return chr((ord(c1)-A + ord(c2)-A) % 26 + A)
 
S

Steven D'Aprano

def add(c1, c2):
% Decode
c1 = ord(c1) - 65
c2 = ord(c2) - 65
% Process
i1 = (c1 + c2) % 26
% Encode
return chr(i1+65)

Python uses # for comments, not %, as I'm sure you know. What language
were you thinking off when you wrote the above?
 
C

Charles Hixson

Python uses # for comments, not %, as I'm sure you know. What language
were you thinking off when you wrote the above?
IIRC Lisp uses % for comments, but it may need to be doubled. (It's
been doubled in the examples I've seen, and I don't remember the syntax.)
Perhaps Scheme has the same convention, but Scheme could be considered a
part of the Lisp clade.
 
P

Piet van Oostrum

Charles Hixson said:
IIRC Lisp uses % for comments, but it may need to be doubled. (It's
been doubled in the examples I've seen, and I don't remember the
syntax.)
Perhaps Scheme has the same convention, but Scheme could be considered a
part of the Lisp clade.

Lisp and scheme use semicolon (;). It wouldn't have been that difficult
to look that up I think.
 
T

Tim Roberts

Steven D'Aprano said:
Python uses # for comments, not %, as I'm sure you know. What language
were you thinking off when you wrote the above?


Psssht, I know better than that.

I've been reading through MATLAB code, which uses %, but I have CERTAINLY
not written enough MATLAB to excuse that.
 
R

rusi

Python uses # for comments, not %, as I'm sure you know. What language
were you thinking off when you wrote the above?


Maybe Tim was putting in the percentage of CPU cycles (wetware cycles??) onDecode, Process and Encode. So we have the % but not the percent…
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top