Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-10 Thread eryksun
On Tue, Dec 10, 2013 at 1:08 PM, spir wrote: > There is another standard tool called 'apply' in general, which sequentially > *performms* the effect of an action on a sequence of inputs. Since it just > applies action, 'apply' does not return any result. 'apply' is rarely used > (even more than ma

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-10 Thread Mark Lawrence
On 10/12/2013 18:08, spir wrote: There is another standard tool called 'apply' in general, which sequentially *performms* the effect of an action on a sequence of inputs. The apply function has been deprecated since 2.3 and never got into Python 3. -- My fellow Pythonistas, ask not what our

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-10 Thread spir
On 12/10/2013 02:31 PM, Rafael Knuth wrote: Hej Steven, thanks for the clarification. I have two questions - one about map function and the other about return. So, in mathematics we might have a mapping between (let's say) counting numbers 1, 2, 3, 4, ... and the even numbers larger than fifty

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-10 Thread spir
On 12/10/2013 02:31 PM, Rafael Knuth wrote: Hej Steven, thanks for the clarification. I have two questions - one about map function and the other about return. So, in mathematics we might have a mapping between (let's say) counting numbers 1, 2, 3, 4, ... and the even numbers larger than fifty

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-10 Thread Steven D'Aprano
On Tue, Dec 10, 2013 at 02:31:55PM +0100, Rafael Knuth wrote: > > So in Python, we can do this with map. First we define a function to do > > the transformation, then pass it to map: > > > > def transform(n): > > return 50 + 2*n Notice here that transformation function takes *one* value, and

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-10 Thread Rafael Knuth
Hej Steven, thanks for the clarification. I have two questions - one about map function and the other about return. > So, in mathematics we might have a mapping between (let's say) counting > numbers 1, 2, 3, 4, ... and the even numbers larger than fifty, 52, 54, > 56, ... and so on. The mapping

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-10 Thread spir
[off-topic] On 12/10/2013 01:39 PM, Wolfgang Maier wrote: def digits(n): """Generator that breaks down an integer into digits from right to left.""" while n>0: yield n % 10 n //= 10 Aha! one more sign that we write numbers backwards! Denis _

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-10 Thread Wolfgang Maier
Steven D'Aprano pearwood.info> writes: > > On Tue, Dec 10, 2013 at 10:39:34AM +0100, Rafael Knuth wrote: > > > def DigSum (integer): > > s = 0 > > while integer != 0: > > integer, remainder = divmod(integer, 10) > > s += remainder > > print(s) > > A thought comes to

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-10 Thread spir
On 12/10/2013 11:56 AM, Steven D'Aprano wrote: This is because the function does *two things*, when it should do one. First it calculates the digit sum, and then it prints it. print's inside functions are a sign of debug not completely cleaned ;-) (and also a sign that test funcs do not provid

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-10 Thread Wolfgang Maier
Asokan Pichai talentsprint.com> writes: > > If you liked it, I will give you one that uses one less variable > > def digitSum(n): >       dsum = 0 >       while n > 0: >             dsum += n % 10 > >             n /= 10 >       return dsum > > Stupid of me not to have mentioned that this Pyt

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-10 Thread Steven D'Aprano
On Tue, Dec 10, 2013 at 10:39:34AM +0100, Rafael Knuth wrote: > def DigSum (integer): > s = 0 > while integer != 0: > integer, remainder = divmod(integer, 10) > s += remainder > print(s) A thought comes to mind... an very important lesson is to learn the difference be

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-10 Thread Asokan Pichai
On Tue, Dec 10, 2013 at 4:06 PM, Asokan Pichai wrote: > > > > On Tue, Dec 10, 2013 at 3:09 PM, Rafael Knuth wrote: > >> Hej there, >> >> > I don't know if everyone would consider this more elegant but it's >> > certainly shorter: >> >> Thanks! >> >> def DigitSum(YourNumber): >> > ... retu

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-10 Thread Asokan Pichai
On Tue, Dec 10, 2013 at 3:09 PM, Rafael Knuth wrote: > Hej there, > > > I don't know if everyone would consider this more elegant but it's > > certainly shorter: > > Thanks! > > def DigitSum(YourNumber): > > ... return sum(map(int, YourNumber)) > > ... > DigitSum('55') > > 10 > > I d

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-10 Thread Steven D'Aprano
On Tue, Dec 10, 2013 at 10:39:34AM +0100, Rafael Knuth wrote: > I don't understand yet what the "map" function does - can you explain? > I read the Python 3.3.0 documentation on that topic but I frankly > didn't really understand it The "map" function comes from so-called functional programming

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-10 Thread Oscar Benjamin
On 10 December 2013 09:39, Rafael Knuth wrote: > > def DigitSum(YourNumber): >> ... return sum(map(int, YourNumber)) >> ... > DigitSum('55') >> 10 > > I don't understand yet what the "map" function does - can you explain? > I read the Python 3.3.0 documentation on that topic but I fran

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-10 Thread Rafael Knuth
Hej there, > I don't know if everyone would consider this more elegant but it's > certainly shorter: Thanks! def DigitSum(YourNumber): > ... return sum(map(int, YourNumber)) > ... DigitSum('55') > 10 I don't understand yet what the "map" function does - can you explain? I read the

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread spir
On 12/09/2013 09:27 PM, Alan Gauld wrote: On 09/12/13 17:57, Roel Schroeven wrote: You are right in a sense, but this is what int() does, isn't it? No. int() can be done in several ways... spir should have said "..., but this is what str() does, ..." I think. Doh! Yes, that makes sense I

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread Alan Gauld
On 09/12/13 22:28, Dave Angel wrote: On Mon, 09 Dec 2013 20:27:38 +, Alan Gauld wrote: Incidentally, I just remembered another completely different way to do it, although I can't recall how it works! Unfortunately it doesn't work for 10, 20, ... Ah, I knew it must be too good to be tru

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread Dave Angel
On Mon, 09 Dec 2013 20:27:38 +, Alan Gauld wrote: Incidentally, I just remembered another completely different way to do it, although I can't recall how it works! Maybe one of the math gurus can explain it, and how to extend it. This version only works for 2 digit numbers... and has a huge g

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread Alan Gauld
On 09/12/13 17:57, Roel Schroeven wrote: You are right in a sense, but this is what int() does, isn't it? No. int() can be done in several ways... spir should have said "..., but this is what str() does, ..." I think. Doh! Yes, that makes sense I should have realized. All in all I think

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread Roel Schroeven
Alan Gauld schreef: On 09/12/13 13:48, spir wrote: On 12/09/2013 02:29 PM, Wolfgang Maier wrote: spir gmail.com> writes: Tu sum it up (aha!): you algorithm is the right and only one No, it's not the only one. ... also the pure numbers approach pointed out by me and Alan. You are right in

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread Dave Angel
On Mon, 09 Dec 2013 17:00:49 +0100, spir wrote: On 12/09/2013 03:49 PM, Alan Gauld wrote: > On 09/12/13 13:48, spir wrote: >> On 12/09/2013 02:29 PM, Wolfgang Maier wrote: >> You are right in a sense, but this is what int() does, isn't it? > No. int() can be done in several ways but usually it

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread spir
On 12/09/2013 03:49 PM, Alan Gauld wrote: On 09/12/13 13:48, spir wrote: On 12/09/2013 02:29 PM, Wolfgang Maier wrote: spir gmail.com> writes: Tu sum it up (aha!): you algorithm is the right and only one No, it's not the only one. ... also the pure numbers approach pointed out by me and A

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread Alan Gauld
On 09/12/13 13:48, spir wrote: On 12/09/2013 02:29 PM, Wolfgang Maier wrote: spir gmail.com> writes: Tu sum it up (aha!): you algorithm is the right and only one No, it's not the only one. ... also the pure numbers approach pointed out by me and Alan. You are right in a sense, but this i

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread Wolfgang Maier
spir gmail.com> writes: > > On 12/09/2013 02:29 PM, Wolfgang Maier wrote: > > spir gmail.com> writes: > > > >> > >> Tu sum it up (aha!): you algorithm is the right and only one > > > > No, it's not the only one. It's certainly the most obvious one, but there is > > also the pure numbers approac

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread spir
On 12/09/2013 02:29 PM, Wolfgang Maier wrote: spir gmail.com> writes: Tu sum it up (aha!): you algorithm is the right and only one No, it's not the only one. It's certainly the most obvious one, but there is also the pure numbers approach pointed out by me and Alan. You are right in a sen

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread spir
On 12/09/2013 02:42 PM, Rafael Knuth wrote: Tu sum it up (aha!): you algorithm is the right and only one No, it's not the only one. It's certainly the most obvious one, but there is also the pure numbers approach pointed out by me and Alan. So far I received 7 different alternative suggestion

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread Rafael Knuth
>> Tu sum it up (aha!): you algorithm is the right and only one > > No, it's not the only one. It's certainly the most obvious one, but there is > also the pure numbers approach pointed out by me and Alan. So far I received 7 different alternative suggestions, both pure numbers & mixed int/str app

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread Wolfgang Maier
spir gmail.com> writes: > > Tu sum it up (aha!): you algorithm is the right and only one No, it's not the only one. It's certainly the most obvious one, but there is also the pure numbers approach pointed out by me and Alan. ___ Tutor maillist -

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread spir
On 12/09/2013 09:08 AM, Rafael Knuth wrote: Hej there, I wrote a program that converts an integer into a digit sum: def DigitSum(YourNumber): DigitList = [] YourNumber = str(YourNumber) for i in YourNumber: DigitList.append(int(i)) print(sum(DigitList)) DigitSum(55

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread Alan Gauld
On 09/12/13 08:08, Rafael Knuth wrote: def DigitSum(YourNumber): DigitList = [] YourNumber = str(YourNumber) for i in YourNumber: DigitList.append(int(i)) print(sum(DigitList)) DigitSum(55) 10 It actually works but I was wondering if that's the only way to solve th

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread Oscar Benjamin
On 9 December 2013 08:08, Rafael Knuth wrote: > Hej there, > > I wrote a program that converts an integer into a digit sum: > > def DigitSum(YourNumber): > DigitList = [] > YourNumber = str(YourNumber) > for i in YourNumber: > DigitList.append(int(i)) > print(sum(DigitList)

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread Wolfgang Maier
Rafael Knuth gmail.com> writes: > > Hej there, > > I wrote a program that converts an integer into a digit sum: > > def DigitSum(YourNumber): > DigitList = [] > YourNumber = str(YourNumber) > for i in YourNumber: > DigitList.append(int(i)) > print(sum(DigitList)) > > D

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread Rafael Knuth
Thanks, guys - got it! I was suspecting that my solution is too complex and that there must be a simpler way to convert integers into a digit sum. Have a great morning/day/evening, Raf On Mon, Dec 9, 2013 at 9:23 AM, Amit Saha wrote: > On Mon, Dec 9, 2013 at 6:08 PM, Rafael Knuth wrote: >> Hej t

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread Asokan Pichai
On Mon, Dec 9, 2013 at 1:38 PM, Rafael Knuth wrote: > Hej there, > > I wrote a program that converts an integer into a digit sum: > > def DigitSum(YourNumber): > DigitList = [] > YourNumber = str(YourNumber) > for i in YourNumber: > DigitList.append(int(i)) > print(sum(Dig

Re: [Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread Amit Saha
On Mon, Dec 9, 2013 at 6:08 PM, Rafael Knuth wrote: > Hej there, > > I wrote a program that converts an integer into a digit sum: > > def DigitSum(YourNumber): > DigitList = [] > YourNumber = str(YourNumber) > for i in YourNumber: > DigitList.append(int(i)) > print(sum(Digi

[Tutor] Converting integers into digit sum (Python 3.3.0)

2013-12-09 Thread Rafael Knuth
Hej there, I wrote a program that converts an integer into a digit sum: def DigitSum(YourNumber): DigitList = [] YourNumber = str(YourNumber) for i in YourNumber: DigitList.append(int(i)) print(sum(DigitList)) DigitSum(55) >>> 10 It actually works but I was wondering if