Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

converting letters to numbers

100 views
Skip to first unread message

kjak...@gmail.com

unread,
Oct 8, 2013, 10:28:18 AM10/8/13
to
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.

Robert Day

unread,
Oct 8, 2013, 10:47:39 AM10/8/13
to pytho...@python.org
On 08/10/13 15:28, kjak...@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.

kjak...@gmail.com

unread,
Oct 8, 2013, 10:51:24 AM10/8/13
to
Expected output is add('C', 'E') returns 'G'; where 'C' and 'E' correspond to 2 and 4 respectively with sum 6, corresponding to 'G'.

Joel Goldstick

unread,
Oct 8, 2013, 11:01:12 AM10/8/13
to kjak...@gmail.com, pytho...@python.org
You wrote this:

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

First of all, this looks like homework. People will help you with
concepts here, but most frown on just providing answers. With that in
mind look at this:

>>> ord('A')
65
>>> ord('a')
97
>>>

In your assignment you refer to Upper case letters. In your code you
take the ordinal value of lower case 'a'


--
Joel Goldstick
http://joelgoldstick.com

rand...@fastmail.us

unread,
Oct 8, 2013, 11:36:51 AM10/8/13
to kjak...@gmail.com, pytho...@python.org
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?

kjak...@gmail.com

unread,
Oct 8, 2013, 11:44:47 AM10/8/13
to
On Tuesday, October 8, 2013 11:36:51 AM UTC-4, rand...@fastmail.us wrote:
>
>
>
> 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

rand...@fastmail.us

unread,
Oct 8, 2013, 11:58:36 AM10/8/13
to kjak...@gmail.com, pytho...@python.org
On Tue, Oct 8, 2013, at 11:44, kjak...@gmail.com wrote:
> def add(c1, c2):
> ans = ''

This only makes sense if your answer is going to be multiple characters.

> for i in c1 + c2:

This line concatenates the strings together.

> ans += chr((((ord(i)-65))%26) + 65)

The way you are doing the modulus, this results in - well, let me
illustrate:

>>> add('','WXYZ[\]^_`abcde')
'WXYZABCDEFGHIJK'

Mark Lawrence

unread,
Oct 8, 2013, 12:05:33 PM10/8/13
to pytho...@python.org
On 08/10/2013 15:28, kjak...@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).
>

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

Dave Angel

unread,
Oct 8, 2013, 5:24:03 PM10/8/13
to pytho...@python.org
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.

--
DaveA


Tim Roberts

unread,
Oct 13, 2013, 11:13:32 PM10/13/13
to
kjak...@gmail.com wrote:
>
>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)
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Steven D'Aprano

unread,
Oct 14, 2013, 1:02:36 AM10/14/13
to
On Sun, 13 Oct 2013 20:13:32 -0700, Tim Roberts wrote:

> 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?



--
Steven

Charles Hixson

unread,
Oct 16, 2013, 3:18:39 PM10/16/13
to pytho...@python.org
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.

--
Charles Hixson

Piet van Oostrum

unread,
Oct 16, 2013, 4:25:21 PM10/16/13
to
Lisp and scheme use semicolon (;). It wouldn't have been that difficult
to look that up I think.
--
Piet van Oostrum <pi...@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]

Rotwang

unread,
Oct 16, 2013, 6:39:28 PM10/16/13
to
Maybe TeX?

MRAB

unread,
Oct 16, 2013, 6:53:02 PM10/16/13
to pytho...@python.org
Or PostScript?

Oscar Benjamin

unread,
Oct 16, 2013, 8:21:49 PM10/16/13
to pytho...@python.org


On Oct 16, 2013 11:54 PM, "MRAB" <pyt...@mrabarnett.plus.com> wrote:
>
> On 16/10/2013 23:39, Rotwang wrote:
>>

> Or PostScript?

My money's on matlab.

Tim Roberts

unread,
Oct 19, 2013, 7:31:14 PM10/19/13
to
Steven D'Aprano <st...@pearwood.info> wrote:
>On Sun, 13 Oct 2013 20:13:32 -0700, Tim Roberts wrote:
>
>> def add(c1, c2):
>> % Decode
>>...
>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.

rusi

unread,
Oct 20, 2013, 4:33:38 AM10/20/13
to
Maybe Tim was putting in the percentage of CPU cycles (wetware cycles??) on Decode, Process and Encode. So we have the % but not the percent…
0 new messages