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


Groups > comp.lang.python > #56444

Re: converting letters to numbers

From Dave Angel <davea@davea.name>
Subject Re: converting letters to numbers
Date 2013-10-08 21:24 +0000
References <26151b64-4f5e-4ee9-81ac-26679932f43d@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.874.1381267474.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web