Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #56403 > unrolled thread

converting letters to numbers

Started bykjakupak@gmail.com
First post2013-10-08 07:28 -0700
Last post2013-10-08 21:24 +0000
Articles 18 — 14 participants

Back to article view | Back to comp.lang.python


Contents

  converting letters to numbers kjakupak@gmail.com - 2013-10-08 07:28 -0700
    Re: converting letters to numbers Robert Day <robertkday@gmail.com> - 2013-10-08 15:47 +0100
      Re: converting letters to numbers kjakupak@gmail.com - 2013-10-08 07:51 -0700
        Re: converting letters to numbers Joel Goldstick <joel.goldstick@gmail.com> - 2013-10-08 11:01 -0400
    Re: converting letters to numbers random832@fastmail.us - 2013-10-08 11:36 -0400
      Re: converting letters to numbers kjakupak@gmail.com - 2013-10-08 08:44 -0700
        Re: converting letters to numbers random832@fastmail.us - 2013-10-08 11:58 -0400
        Re: converting letters to numbers Tim Roberts <timr@probo.com> - 2013-10-13 20:13 -0700
          Re: converting letters to numbers Steven D'Aprano <steve@pearwood.info> - 2013-10-14 05:02 +0000
            Re: converting letters to numbers Charles Hixson <charleshixsn@earthlink.net> - 2013-10-16 12:18 -0700
              Re: converting letters to numbers Piet van Oostrum <piet@vanoostrum.org> - 2013-10-16 16:25 -0400
            Re: converting letters to numbers Rotwang <sg552@hotmail.co.uk> - 2013-10-16 23:39 +0100
              Re: converting letters to numbers MRAB <python@mrabarnett.plus.com> - 2013-10-16 23:53 +0100
              Re: converting letters to numbers Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-10-17 01:21 +0100
            Re: converting letters to numbers Tim Roberts <timr@probo.com> - 2013-10-19 16:31 -0700
            Re: converting letters to numbers rusi <rustompmody@gmail.com> - 2013-10-20 01:33 -0700
    Re: converting letters to numbers Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-08 17:05 +0100
    Re: converting letters to numbers Dave Angel <davea@davea.name> - 2013-10-08 21:24 +0000

#56403 — converting letters to numbers

Fromkjakupak@gmail.com
Date2013-10-08 07:28 -0700
Subjectconverting letters to numbers
Message-ID<26151b64-4f5e-4ee9-81ac-26679932f43d@googlegroups.com>
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.

[toc] | [next] | [standalone]


#56407

FromRobert Day <robertkday@gmail.com>
Date2013-10-08 15:47 +0100
Message-ID<mailman.856.1381243701.18130.python-list@python.org>
In reply to#56403
On 08/10/13 15:28, kjakupak@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.

[toc] | [prev] | [next] | [standalone]


#56408

Fromkjakupak@gmail.com
Date2013-10-08 07:51 -0700
Message-ID<32293336-949a-448c-bc1c-a7ac5186fcb6@googlegroups.com>
In reply to#56407
On Tuesday, October 8, 2013 10:47:39 AM UTC-4, Robert Day wrote:
> On 08/10/13 15:28, kjakupak@gmail.com 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'.

[toc] | [prev] | [next] | [standalone]


#56413

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-10-08 11:01 -0400
Message-ID<mailman.857.1381244474.18130.python-list@python.org>
In reply to#56408
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

[toc] | [prev] | [next] | [standalone]


#56418

Fromrandom832@fastmail.us
Date2013-10-08 11:36 -0400
Message-ID<mailman.859.1381246619.18130.python-list@python.org>
In reply to#56403
On Tue, Oct 8, 2013, at 10:28, kjakupak@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). 
> 
> 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?

[toc] | [prev] | [next] | [standalone]


#56419

Fromkjakupak@gmail.com
Date2013-10-08 08:44 -0700
Message-ID<fb210bab-7ea2-4c70-93e0-7f111d794b72@googlegroups.com>
In reply to#56418
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

[toc] | [prev] | [next] | [standalone]


#56424

Fromrandom832@fastmail.us
Date2013-10-08 11:58 -0400
Message-ID<mailman.861.1381247920.18130.python-list@python.org>
In reply to#56419
On Tue, Oct 8, 2013, at 11:44, kjakupak@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'

[toc] | [prev] | [next] | [standalone]


#56789

FromTim Roberts <timr@probo.com>
Date2013-10-13 20:13 -0700
Message-ID<b2om59pa1npetckqmm66dt16ugkvrv520e@4ax.com>
In reply to#56419
kjakupak@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, timr@probo.com
Providenza & Boekelheide, Inc.

[toc] | [prev] | [next] | [standalone]


#56790

FromSteven D'Aprano <steve@pearwood.info>
Date2013-10-14 05:02 +0000
Message-ID<525b7aec$0$30000$c3e8da3$5496439d@news.astraweb.com>
In reply to#56789
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

[toc] | [prev] | [next] | [standalone]


#56894

FromCharles Hixson <charleshixsn@earthlink.net>
Date2013-10-16 12:18 -0700
Message-ID<mailman.1119.1381951191.18130.python-list@python.org>
In reply to#56790
On 10/13/2013 10:02 PM, Steven D'Aprano wrote:
> 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?
>
>
>
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

[toc] | [prev] | [next] | [standalone]


#56895

FromPiet van Oostrum <piet@vanoostrum.org>
Date2013-10-16 16:25 -0400
Message-ID<m2eh7lezf2.fsf@cochabamba.vanoostrum.org>
In reply to#56894
Charles Hixson <charleshixsn@earthlink.net> writes:

> On 10/13/2013 10:02 PM, Steven D'Aprano wrote:
>> 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?
>>
>>
>>
> 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.
-- 
Piet van Oostrum <piet@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]

[toc] | [prev] | [next] | [standalone]


#56902

FromRotwang <sg552@hotmail.co.uk>
Date2013-10-16 23:39 +0100
Message-ID<l3n4j4$enl$1@dont-email.me>
In reply to#56790
On 14/10/2013 06:02, Steven D'Aprano wrote:
> 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?

Maybe TeX?

[toc] | [prev] | [next] | [standalone]


#56903

FromMRAB <python@mrabarnett.plus.com>
Date2013-10-16 23:53 +0100
Message-ID<mailman.1122.1381963987.18130.python-list@python.org>
In reply to#56902
On 16/10/2013 23:39, Rotwang wrote:
> On 14/10/2013 06:02, Steven D'Aprano wrote:
>> 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?
>
> Maybe TeX?
>
Or PostScript?

[toc] | [prev] | [next] | [standalone]


#56915

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2013-10-17 01:21 +0100
Message-ID<mailman.1130.1381969640.18130.python-list@python.org>
In reply to#56902

[Multipart message — attachments visible in raw view] — view raw

On Oct 16, 2013 11:54 PM, "MRAB" <python@mrabarnett.plus.com> wrote:
>
> On 16/10/2013 23:39, Rotwang wrote:
>>
>> On 14/10/2013 06:02, Steven D'Aprano wrote:
>>>
>>> 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?
>>
>>
>> Maybe TeX?
>>
> Or PostScript?

My money's on matlab.

[toc] | [prev] | [next] | [standalone]


#57134

FromTim Roberts <timr@probo.com>
Date2013-10-19 16:31 -0700
Message-ID<u6pu591b9su6up6uvp3oubkdujdv5rjg8h@4ax.com>
In reply to#56790
Steven D'Aprano <steve@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.
-- 
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

[toc] | [prev] | [next] | [standalone]


#57144

Fromrusi <rustompmody@gmail.com>
Date2013-10-20 01:33 -0700
Message-ID<c80db7cd-2a4a-4b46-ab6a-e6d450e239f7@googlegroups.com>
In reply to#56790
On Monday, October 14, 2013 10:32:36 AM UTC+5:30, Steven D'Aprano wrote:
> 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?


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…

[toc] | [prev] | [next] | [standalone]


#56428

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-10-08 17:05 +0100
Message-ID<mailman.863.1381248347.18130.python-list@python.org>
In reply to#56403
On 08/10/2013 15:28, kjakupak@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

[toc] | [prev] | [next] | [standalone]


#56444

FromDave Angel <davea@davea.name>
Date2013-10-08 21:24 +0000
Message-ID<mailman.874.1381267474.18130.python-list@python.org>
In reply to#56403
On 8/10/2013 10:28, kjakupak@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). 
>
> 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.

-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web