Re: converting letters to numbers
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… -- https://mail.python.org/mailman/listinfo/python-list
Re: converting letters to numbers
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. -- Tim Roberts, t...@probo.com Providenza Boekelheide, Inc. -- https://mail.python.org/mailman/listinfo/python-list
Re: converting letters to numbers
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: converting letters to numbers
Charles Hixson charleshi...@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 p...@vanoostrum.org WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] -- https://mail.python.org/mailman/listinfo/python-list
Re: converting letters to numbers
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? -- https://mail.python.org/mailman/listinfo/python-list
Re: converting letters to numbers
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? -- https://mail.python.org/mailman/listinfo/python-list
Re: converting letters to numbers
On Oct 16, 2013 11:54 PM, MRAB pyt...@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. -- https://mail.python.org/mailman/listinfo/python-list
Re: converting letters to numbers
kjaku...@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 += chrord(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, t...@probo.com Providenza Boekelheide, Inc. -- https://mail.python.org/mailman/listinfo/python-list
Re: converting letters to numbers
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: converting letters to numbers
On 08/10/13 15:28, kjaku...@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. -- https://mail.python.org/mailman/listinfo/python-list
Re: converting letters to numbers
On Tuesday, October 8, 2013 10:47:39 AM UTC-4, Robert Day wrote: On 08/10/13 15:28, kjaku...@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'. -- https://mail.python.org/mailman/listinfo/python-list
Re: converting letters to numbers
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: converting letters to numbers
On Tue, Oct 8, 2013, at 10:28, kjaku...@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? -- https://mail.python.org/mailman/listinfo/python-list
Re: converting letters to numbers
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 += chrord(i)-65))%26) + 65) return ans -- https://mail.python.org/mailman/listinfo/python-list
Re: converting letters to numbers
On Tue, Oct 8, 2013, at 11:44, kjaku...@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 += chrord(i)-65))%26) + 65) The way you are doing the modulus, this results in - well, let me illustrate: add('','WXYZ[\]^_`abcde') 'WXYZABCDEFGHIJK' -- https://mail.python.org/mailman/listinfo/python-list
Re: converting letters to numbers
On 08/10/2013 15:28, kjaku...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: converting letters to numbers
On 8/10/2013 10:28, kjaku...@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 -- https://mail.python.org/mailman/listinfo/python-list