Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101139 > unrolled thread
| Started by | katye2007@gmail.com |
|---|---|
| First post | 2016-01-02 03:49 -0800 |
| Last post | 2016-01-02 21:26 +0000 |
| Articles | 9 on this page of 29 — 16 participants |
Back to article view | Back to comp.lang.python
Trailing zeros of 100! katye2007@gmail.com - 2016-01-02 03:49 -0800
Re: Trailing zeros of 100! David <bouncingcats@gmail.com> - 2016-01-02 23:19 +1100
Re: Trailing zeros of 100! katye2007@gmail.com - 2016-01-02 04:34 -0800
Re: Trailing zeros of 100! Vlastimil Brom <vlastimil.brom@gmail.com> - 2016-01-02 13:44 +0100
Re: Trailing zeros of 100! "yehudak ." <katye2007@gmail.com> - 2016-01-02 15:14 +0200
Re: Trailing zeros of 100! Robin Koch <robin.koch@t-online.de> - 2016-01-02 16:57 +0100
Re: Trailing zeros of 100! Tony van der Hoff <tony@vanderhoff.org> - 2016-01-02 17:09 +0100
Re: Trailing zeros of 100! Robin Koch <robin.koch@t-online.de> - 2016-01-02 17:56 +0100
Re: Trailing zeros of 100! Bernardo Sulzbach <mafagafogigante@gmail.com> - 2016-01-02 15:24 -0200
Re: Trailing zeros of 100! Chris Angelico <rosuav@gmail.com> - 2016-01-03 08:57 +1100
Re: Trailing zeros of 100! Robin Koch <robin.koch@t-online.de> - 2016-01-03 01:02 +0100
Re: Trailing zeros of 100! Ben Finney <ben+python@benfinney.id.au> - 2016-01-03 11:20 +1100
Re: Trailing zeros of 100! srinivas devaki <mr.eightnoteight@gmail.com> - 2016-01-03 07:16 +0530
Re: Trailing zeros of 100! Tony van der Hoff <tony@vanderhoff.org> - 2016-01-03 11:53 +0100
Re: Trailing zeros of 100! Joel Goldstick <joel.goldstick@gmail.com> - 2016-01-02 09:57 -0500
Re: Trailing zeros of 100! Vlastimil Brom <vlastimil.brom@gmail.com> - 2016-01-02 16:24 +0100
Re: Trailing zeros of 100! Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-01-02 15:30 +0000
Re: Trailing zeros of 100! Robin Koch <robin.koch@t-online.de> - 2016-01-02 16:54 +0100
Re: Trailing zeros of 100! Peter Otten <__peter__@web.de> - 2016-01-02 18:18 +0100
Re: Trailing zeros of 100! Tim Chase <python.list@tim.thechases.com> - 2016-01-02 10:33 -0600
Re: Trailing zeros of 100! "yehudak ." <katye2007@gmail.com> - 2016-01-02 19:34 +0200
Re: Trailing zeros of 100! Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-01-02 17:44 +0000
Re: Trailing zeros of 100! Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-03 00:36 +0100
Re: Trailing zeros of 100! Serhiy Storchaka <storchaka@gmail.com> - 2016-01-02 20:28 +0200
Re: Trailing zeros of 100! Vlastimil Brom <vlastimil.brom@gmail.com> - 2016-01-02 19:29 +0100
Re: Trailing zeros of 100! "yehudak ." <katye2007@gmail.com> - 2016-01-02 22:02 +0200
Re: Trailing zeros of 100! "yehudak ." <katye2007@gmail.com> - 2016-01-02 22:09 +0200
Re: Trailing zeros of 100! Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-01-02 21:25 +0000
Re: Trailing zeros of 100! Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-01-02 21:26 +0000
Page 2 of 2 — ← Prev page 1 [2]
| From | "yehudak ." <katye2007@gmail.com> |
|---|---|
| Date | 2016-01-02 19:34 +0200 |
| Message-ID | <mailman.175.1451756083.11925.python-list@python.org> |
| In reply to | #101139 |
vbr, I tried using .pop() but could not get what I wanted .Also, I can't see an advantage in reversing the number. Would you care to write explicitly the program for me (and probably for other too)? Brute Force is the style I'm thinking about. Sorry, but I learn most from viewing the code. Appreciated, Yehuda On Sat, Jan 2, 2016 at 5:24 PM, Vlastimil Brom <vlastimil.brom@gmail.com> wrote: > 2016-01-02 14:14 GMT+01:00 yehudak . <katye2007@gmail.com>: > > Vlastimil, > > Thank you so much, but... > > All that is Chinese for me. > > Can you show a 'normal' Python code for me? > > > > Yehuda > > > > On Sat, Jan 2, 2016 at 2:44 PM, Vlastimil Brom <vlastimil.brom@gmail.com > > > > wrote: > >> > >> 2016-01-02 12:49 GMT+01:00 <katye2007@gmail.com>: > >> > Hi, newbie here! > >> > I'm trying to write a python program to find how many trailing zeros > are > >> > in 100! (factorial of 100). > >> > I used factorial from the math module, but my efforts to continue > >> > failed. Please help. > >> > > >> > Thank you, > >> > Yehuda > >> > -- > >> > https://mail.python.org/mailman/listinfo/python-list > >> > >> Hi, > >> rather an illustration of the available tools in python, than a > >> (submittable) solution: > >> > >> >>> import re, math > >> >>> len(re.search(r"0*$", str(math.factorial(100))).group()) > >> 24 > >> [or the same code on more lines with some indentation - if it is > >> preserved via e-mail] > >> >>> len( > >> ... re.search( > >> ... r"0*$", > >> ... str( > >> ... math.factorial(100) > >> ... ) > >> ... ).group() > >> ... ) > >> 24 > >> >>> > >> > >> I.e. You need the length of the string resulting as the match of the > >> regular expression search for a pattern representing zero or more "0" > >> at the end of the input text, which is the string version of 100! > >> > >> Of course, there are other ways to get this result :-) > >> > >> regards, > >> vbr > > > > > Hi, > If you eventually have this as an assignment or other kind of > (self)learning task, you would want to approach this with the methods > you know, or are supposed to use. > For the math context, you may find this explanations useful: > http://www.purplemath.com/modules/factzero.htm > a rather straightforward python implementation of this seems to be > e.g. this recipe: > > http://code.activestate.com/recipes/577844-calculate-trailing-zeroes-in-a-factorial/ > Note, that you don't need to calculate the value of the factorial > itself using this way. > If you have problems with following or understanding the code, you may > show your own attempts and tell what problems you encounter with your > approach. > > My previous code sample is based on another - "brute-force" approach, > the factorial is calculated (e.g. via the math module as you have > found), then the integer is converted to a string, afterwards the part > of the result consisting only of zeros - at the end of the string is > matched with a regular expression and finally the length of it is > determined. > > Regular expressions might be handy, but are not necesarilly elementary > stuff for a newcomer in python programming. > You can count the trailing zeros in other ways too - as was suggested > - you can reverse the string and count from the beginning then, > stopping before the first non-zero digit. > The most straightforward way could be to loop (characterwise) through > the (reversed) string, check each character whether it equals to "0" > and stop as soon as there is another digit. > > hth, > vbr >
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2016-01-02 17:44 +0000 |
| Message-ID | <mailman.176.1451756683.11925.python-list@python.org> |
| In reply to | #101139 |
On 02/01/2016 17:34, yehudak . wrote: > vbr, > I tried using .pop() but could not get what I wanted .Also, I can't see an > advantage in reversing the number. > Would you care to write explicitly the program for me (and probably for > other too)? > Brute Force is the style I'm thinking about. > > Sorry, but I learn most from viewing the code. > > Appreciated, > Yehuda > > On Sat, Jan 2, 2016 at 5:24 PM, Vlastimil Brom <vlastimil.brom@gmail.com> > wrote: > >> 2016-01-02 14:14 GMT+01:00 yehudak . <katye2007@gmail.com>: >>> Vlastimil, >>> Thank you so much, but... >>> All that is Chinese for me. >>> Can you show a 'normal' Python code for me? >>> >>> Yehuda >>> >>> On Sat, Jan 2, 2016 at 2:44 PM, Vlastimil Brom <vlastimil.brom@gmail.com >>> >>> wrote: >>>> >>>> 2016-01-02 12:49 GMT+01:00 <katye2007@gmail.com>: >>>>> Hi, newbie here! >>>>> I'm trying to write a python program to find how many trailing zeros >> are >>>>> in 100! (factorial of 100). >>>>> I used factorial from the math module, but my efforts to continue >>>>> failed. Please help. >>>>> >>>>> Thank you, >>>>> Yehuda >>>>> -- >>>>> https://mail.python.org/mailman/listinfo/python-list >>>> >>>> Hi, >>>> rather an illustration of the available tools in python, than a >>>> (submittable) solution: >>>> >>>>>>> import re, math >>>>>>> len(re.search(r"0*$", str(math.factorial(100))).group()) >>>> 24 >>>> [or the same code on more lines with some indentation - if it is >>>> preserved via e-mail] >>>>>>> len( >>>> ... re.search( >>>> ... r"0*$", >>>> ... str( >>>> ... math.factorial(100) >>>> ... ) >>>> ... ).group() >>>> ... ) >>>> 24 >>>>>>> >>>> >>>> I.e. You need the length of the string resulting as the match of the >>>> regular expression search for a pattern representing zero or more "0" >>>> at the end of the input text, which is the string version of 100! >>>> >>>> Of course, there are other ways to get this result :-) >>>> >>>> regards, >>>> vbr >>> >>> >> Hi, >> If you eventually have this as an assignment or other kind of >> (self)learning task, you would want to approach this with the methods >> you know, or are supposed to use. >> For the math context, you may find this explanations useful: >> http://www.purplemath.com/modules/factzero.htm >> a rather straightforward python implementation of this seems to be >> e.g. this recipe: >> >> http://code.activestate.com/recipes/577844-calculate-trailing-zeroes-in-a-factorial/ >> Note, that you don't need to calculate the value of the factorial >> itself using this way. >> If you have problems with following or understanding the code, you may >> show your own attempts and tell what problems you encounter with your >> approach. >> >> My previous code sample is based on another - "brute-force" approach, >> the factorial is calculated (e.g. via the math module as you have >> found), then the integer is converted to a string, afterwards the part >> of the result consisting only of zeros - at the end of the string is >> matched with a regular expression and finally the length of it is >> determined. >> >> Regular expressions might be handy, but are not necesarilly elementary >> stuff for a newcomer in python programming. >> You can count the trailing zeros in other ways too - as was suggested >> - you can reverse the string and count from the beginning then, >> stopping before the first non-zero digit. >> The most straightforward way could be to loop (characterwise) through >> the (reversed) string, check each character whether it equals to "0" >> and stop as soon as there is another digit. >> >> hth, >> vbr >> You'll learn far more if you try writing code and then get help if it doesn't work, rather than get other people to write the code for you. Please don't top post, it's extremely annoying when trying to follow long threads. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2016-01-03 00:36 +0100 |
| Message-ID | <1507703.vSOp0aCa7u@PointedEars.de> |
| In reply to | #101165 |
Mark Lawrence wrote: > Please don't top post, it's extremely annoying when trying to follow > long threads. As are full quotes. -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Serhiy Storchaka <storchaka@gmail.com> |
|---|---|
| Date | 2016-01-02 20:28 +0200 |
| Message-ID | <mailman.177.1451759325.11925.python-list@python.org> |
| In reply to | #101139 |
On 02.01.16 18:33, Tim Chase wrote: > or mathematically: > > sum(1 for _ in itertools.takewhile( > lambda x: n % (10**x) == 0, > itertools.count(1))) The mathematician would prove that the result is not larger than 100/4.
[toc] | [prev] | [next] | [standalone]
| From | Vlastimil Brom <vlastimil.brom@gmail.com> |
|---|---|
| Date | 2016-01-02 19:29 +0100 |
| Message-ID | <mailman.178.1451759380.11925.python-list@python.org> |
| In reply to | #101139 |
2016-01-02 18:34 GMT+01:00 yehudak . <katye2007@gmail.com>:
[partly edited for bottom posting]
> On Sat, Jan 2, 2016 at 5:24 PM, Vlastimil Brom <vlastimil.brom@gmail.com>
> wrote:
>>
>> 2016-01-02 14:14 GMT+01:00 yehudak . <katye2007@gmail.com>:
>> >
[...]>> > On Sat, Jan 2, 2016 at 2:44 PM, Vlastimil Brom
>> > <vlastimil.brom@gmail.com>
>> > wrote:
>> >>
>> >> 2016-01-02 12:49 GMT+01:00 <katye2007@gmail.com>:
>> >> > Hi, newbie here!
>> >> > I'm trying to write a python program to find how many trailing zeros
>> >> > are
>> >> > in 100! (factorial of 100).
>> >> > I used factorial from the math module, but my efforts to continue
>> >> > failed. Please help.
>> >> >
>> >> > Thank you,
>> >> > Yehuda
>> >> > --
>> >> > https://mail.python.org/mailman/listinfo/python-list
>> >>
>> > [...]
>> >
>> Hi,
>> If you eventually have this as an assignment or other kind of
>> (self)learning task, you would want to approach this with the methods
>> you know, or are supposed to use.
>> For the math context, you may find this explanations useful:
>> http://www.purplemath.com/modules/factzero.htm
>> a rather straightforward python implementation of this seems to be
>> e.g. this recipe:
>>
>> http://code.activestate.com/recipes/577844-calculate-trailing-zeroes-in-a-factorial/
>> Note, that you don't need to calculate the value of the factorial
>> itself using this way.
>> If you have problems with following or understanding the code, you may
>> show your own attempts and tell what problems you encounter with your
>> approach.
>>
>> My previous code sample is based on another - "brute-force" approach,
>> the factorial is calculated (e.g. via the math module as you have
>> found), then the integer is converted to a string, afterwards the part
>> of the result consisting only of zeros - at the end of the string is
>> matched with a regular expression and finally the length of it is
>> determined.
>>
>> Regular expressions might be handy, but are not necesarilly elementary
>> stuff for a newcomer in python programming.
>> You can count the trailing zeros in other ways too - as was suggested
>> - you can reverse the string and count from the beginning then,
>> stopping before the first non-zero digit.
>> The most straightforward way could be to loop (characterwise) through
>> the (reversed) string, check each character whether it equals to "0"
>> and stop as soon as there is another digit.
>>
>> hth,
>> vbr
>
>
> vbr,
> I tried using .pop() but could not get what I wanted .Also, I can't see an
> advantage in reversing the number.
> Would you care to write explicitly the program for me (and probably for
> other too)?
> Brute Force is the style I'm thinking about.
>
> Sorry, but I learn most from viewing the code.
>
> Appreciated,
> Yehuda
>
Hi,
reversing the string would be useful for directly looping over the
string (the interesting zeros would be at the beginning of the
reversed string.
If you use pop() on a list of the digits, the items are taken from the
end of the list by default, hence no reversing is needed.
What problems do you have with this route? (you will need to convert
from the integer result to string, then to list and use pop() and
count the steps until you reach a non-zero digit)
If you need this kind of soulution (computing the factorial,
converting to string, counting the trailing zero digits), I believe,
the most easily comprehensible version was posted by Tim Chase a bit
earlier.
In the interactive interpreter, with some intermediate steps added, it
can look like this:
>>> from math import factorial
>>> fact100_int = factorial(100)
>>> fact100_string = str(fact100_int)
>>> fact100_string_without_trailing_zeros = fact100_string.rstrip("0")
>>> len(fact100_string) - len(fact100_string_without_trailing_zeros)
24
>>>
[aditional info on the rstrip method of any string ("abcd" used for
illustration here): ]
>>> print("abcd".rstrip.__doc__)
S.rstrip([chars]) -> str
Return a copy of the string S with trailing whitespace removed.
If chars is given and not None, remove characters in chars instead.
>>>
It should be noted that the approaches which involve computing of the
factorial itself have much lower limits on the size compared to the
algorithmic ones, but for the given case both are sufficient.
hth,
vbr
[toc] | [prev] | [next] | [standalone]
| From | "yehudak ." <katye2007@gmail.com> |
|---|---|
| Date | 2016-01-02 22:02 +0200 |
| Message-ID | <mailman.180.1451768963.11925.python-list@python.org> |
| In reply to | #101139 |
Hello vbr,
That's EXACTLY what I needed. rstrip is new for me so I'm going to Dr.
Google to learn.
On my efforts I was struggling with .pop() but wasn't very successful...
Thank you so much,
Yehuda
On Sat, Jan 2, 2016 at 8:29 PM, Vlastimil Brom <vlastimil.brom@gmail.com>
wrote:
> 2016-01-02 18:34 GMT+01:00 yehudak . <katye2007@gmail.com>:
> [partly edited for bottom posting]
> > On Sat, Jan 2, 2016 at 5:24 PM, Vlastimil Brom <vlastimil.brom@gmail.com
> >
> > wrote:
> >>
> >> 2016-01-02 14:14 GMT+01:00 yehudak . <katye2007@gmail.com>:
> >> >
> [...]>> > On Sat, Jan 2, 2016 at 2:44 PM, Vlastimil Brom
> >> > <vlastimil.brom@gmail.com>
> >> > wrote:
> >> >>
> >> >> 2016-01-02 12:49 GMT+01:00 <katye2007@gmail.com>:
> >> >> > Hi, newbie here!
> >> >> > I'm trying to write a python program to find how many trailing
> zeros
> >> >> > are
> >> >> > in 100! (factorial of 100).
> >> >> > I used factorial from the math module, but my efforts to continue
> >> >> > failed. Please help.
> >> >> >
> >> >> > Thank you,
> >> >> > Yehuda
> >> >> > --
> >> >> > https://mail.python.org/mailman/listinfo/python-list
> >> >>
> >> > [...]
> >> >
> >> Hi,
> >> If you eventually have this as an assignment or other kind of
> >> (self)learning task, you would want to approach this with the methods
> >> you know, or are supposed to use.
> >> For the math context, you may find this explanations useful:
> >> http://www.purplemath.com/modules/factzero.htm
> >> a rather straightforward python implementation of this seems to be
> >> e.g. this recipe:
> >>
> >>
> http://code.activestate.com/recipes/577844-calculate-trailing-zeroes-in-a-factorial/
> >> Note, that you don't need to calculate the value of the factorial
> >> itself using this way.
> >> If you have problems with following or understanding the code, you may
> >> show your own attempts and tell what problems you encounter with your
> >> approach.
> >>
> >> My previous code sample is based on another - "brute-force" approach,
> >> the factorial is calculated (e.g. via the math module as you have
> >> found), then the integer is converted to a string, afterwards the part
> >> of the result consisting only of zeros - at the end of the string is
> >> matched with a regular expression and finally the length of it is
> >> determined.
> >>
> >> Regular expressions might be handy, but are not necesarilly elementary
> >> stuff for a newcomer in python programming.
> >> You can count the trailing zeros in other ways too - as was suggested
> >> - you can reverse the string and count from the beginning then,
> >> stopping before the first non-zero digit.
> >> The most straightforward way could be to loop (characterwise) through
> >> the (reversed) string, check each character whether it equals to "0"
> >> and stop as soon as there is another digit.
> >>
> >> hth,
> >> vbr
> >
> >
> > vbr,
> > I tried using .pop() but could not get what I wanted .Also, I can't see
> an
> > advantage in reversing the number.
> > Would you care to write explicitly the program for me (and probably for
> > other too)?
> > Brute Force is the style I'm thinking about.
> >
> > Sorry, but I learn most from viewing the code.
> >
> > Appreciated,
> > Yehuda
> >
> Hi,
> reversing the string would be useful for directly looping over the
> string (the interesting zeros would be at the beginning of the
> reversed string.
> If you use pop() on a list of the digits, the items are taken from the
> end of the list by default, hence no reversing is needed.
> What problems do you have with this route? (you will need to convert
> from the integer result to string, then to list and use pop() and
> count the steps until you reach a non-zero digit)
>
> If you need this kind of soulution (computing the factorial,
> converting to string, counting the trailing zero digits), I believe,
> the most easily comprehensible version was posted by Tim Chase a bit
> earlier.
> In the interactive interpreter, with some intermediate steps added, it
> can look like this:
>
> >>> from math import factorial
> >>> fact100_int = factorial(100)
> >>> fact100_string = str(fact100_int)
> >>> fact100_string_without_trailing_zeros = fact100_string.rstrip("0")
> >>> len(fact100_string) - len(fact100_string_without_trailing_zeros)
> 24
> >>>
>
> [aditional info on the rstrip method of any string ("abcd" used for
> illustration here): ]
> >>> print("abcd".rstrip.__doc__)
> S.rstrip([chars]) -> str
>
> Return a copy of the string S with trailing whitespace removed.
> If chars is given and not None, remove characters in chars instead.
> >>>
>
> It should be noted that the approaches which involve computing of the
> factorial itself have much lower limits on the size compared to the
> algorithmic ones, but for the given case both are sufficient.
>
> hth,
> vbr
>
[toc] | [prev] | [next] | [standalone]
| From | "yehudak ." <katye2007@gmail.com> |
|---|---|
| Date | 2016-01-02 22:09 +0200 |
| Message-ID | <mailman.181.1451768963.11925.python-list@python.org> |
| In reply to | #101139 |
Hi again,
I looked a little deeper at your code. Smart solution and kudos.
Yehuda
On Sat, Jan 2, 2016 at 10:02 PM, yehudak . <katye2007@gmail.com> wrote:
> Hello vbr,
> That's EXACTLY what I needed. rstrip is new for me so I'm going to Dr.
> Google to learn.
>
> On my efforts I was struggling with .pop() but wasn't very successful...
>
> Thank you so much,
> Yehuda
>
> On Sat, Jan 2, 2016 at 8:29 PM, Vlastimil Brom <vlastimil.brom@gmail.com>
> wrote:
>
>> 2016-01-02 18:34 GMT+01:00 yehudak . <katye2007@gmail.com>:
>> [partly edited for bottom posting]
>> > On Sat, Jan 2, 2016 at 5:24 PM, Vlastimil Brom <
>> vlastimil.brom@gmail.com>
>> > wrote:
>> >>
>> >> 2016-01-02 14:14 GMT+01:00 yehudak . <katye2007@gmail.com>:
>> >> >
>> [...]>> > On Sat, Jan 2, 2016 at 2:44 PM, Vlastimil Brom
>> >> > <vlastimil.brom@gmail.com>
>> >> > wrote:
>> >> >>
>> >> >> 2016-01-02 12:49 GMT+01:00 <katye2007@gmail.com>:
>> >> >> > Hi, newbie here!
>> >> >> > I'm trying to write a python program to find how many trailing
>> zeros
>> >> >> > are
>> >> >> > in 100! (factorial of 100).
>> >> >> > I used factorial from the math module, but my efforts to continue
>> >> >> > failed. Please help.
>> >> >> >
>> >> >> > Thank you,
>> >> >> > Yehuda
>> >> >> > --
>> >> >> > https://mail.python.org/mailman/listinfo/python-list
>> >> >>
>> >> > [...]
>> >> >
>> >> Hi,
>> >> If you eventually have this as an assignment or other kind of
>> >> (self)learning task, you would want to approach this with the methods
>> >> you know, or are supposed to use.
>> >> For the math context, you may find this explanations useful:
>> >> http://www.purplemath.com/modules/factzero.htm
>> >> a rather straightforward python implementation of this seems to be
>> >> e.g. this recipe:
>> >>
>> >>
>> http://code.activestate.com/recipes/577844-calculate-trailing-zeroes-in-a-factorial/
>> >> Note, that you don't need to calculate the value of the factorial
>> >> itself using this way.
>> >> If you have problems with following or understanding the code, you may
>> >> show your own attempts and tell what problems you encounter with your
>> >> approach.
>> >>
>> >> My previous code sample is based on another - "brute-force" approach,
>> >> the factorial is calculated (e.g. via the math module as you have
>> >> found), then the integer is converted to a string, afterwards the part
>> >> of the result consisting only of zeros - at the end of the string is
>> >> matched with a regular expression and finally the length of it is
>> >> determined.
>> >>
>> >> Regular expressions might be handy, but are not necesarilly elementary
>> >> stuff for a newcomer in python programming.
>> >> You can count the trailing zeros in other ways too - as was suggested
>> >> - you can reverse the string and count from the beginning then,
>> >> stopping before the first non-zero digit.
>> >> The most straightforward way could be to loop (characterwise) through
>> >> the (reversed) string, check each character whether it equals to "0"
>> >> and stop as soon as there is another digit.
>> >>
>> >> hth,
>> >> vbr
>> >
>> >
>> > vbr,
>> > I tried using .pop() but could not get what I wanted .Also, I can't see
>> an
>> > advantage in reversing the number.
>> > Would you care to write explicitly the program for me (and probably for
>> > other too)?
>> > Brute Force is the style I'm thinking about.
>> >
>> > Sorry, but I learn most from viewing the code.
>> >
>> > Appreciated,
>> > Yehuda
>> >
>> Hi,
>> reversing the string would be useful for directly looping over the
>> string (the interesting zeros would be at the beginning of the
>> reversed string.
>> If you use pop() on a list of the digits, the items are taken from the
>> end of the list by default, hence no reversing is needed.
>> What problems do you have with this route? (you will need to convert
>> from the integer result to string, then to list and use pop() and
>> count the steps until you reach a non-zero digit)
>>
>> If you need this kind of soulution (computing the factorial,
>> converting to string, counting the trailing zero digits), I believe,
>> the most easily comprehensible version was posted by Tim Chase a bit
>> earlier.
>> In the interactive interpreter, with some intermediate steps added, it
>> can look like this:
>>
>> >>> from math import factorial
>> >>> fact100_int = factorial(100)
>> >>> fact100_string = str(fact100_int)
>> >>> fact100_string_without_trailing_zeros = fact100_string.rstrip("0")
>> >>> len(fact100_string) - len(fact100_string_without_trailing_zeros)
>> 24
>> >>>
>>
>> [aditional info on the rstrip method of any string ("abcd" used for
>> illustration here): ]
>> >>> print("abcd".rstrip.__doc__)
>> S.rstrip([chars]) -> str
>>
>> Return a copy of the string S with trailing whitespace removed.
>> If chars is given and not None, remove characters in chars instead.
>> >>>
>>
>> It should be noted that the approaches which involve computing of the
>> factorial itself have much lower limits on the size compared to the
>> algorithmic ones, but for the given case both are sufficient.
>>
>> hth,
>> vbr
>>
>
>
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2016-01-02 21:25 +0000 |
| Message-ID | <mailman.182.1451769960.11925.python-list@python.org> |
| In reply to | #101139 |
On 02/01/2016 20:02, yehudak . wrote: > Hello vbr, > That's EXACTLY what I needed. rstrip is new for me so I'm going to Dr. > Google to learn. > > On my efforts I was struggling with .pop() but wasn't very successful... > > Thank you so much, > Yehuda > How many times do you have to be asked to not top post? Just how thick are you? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2016-01-02 21:26 +0000 |
| Message-ID | <mailman.183.1451770207.11925.python-list@python.org> |
| In reply to | #101139 |
On 02/01/2016 20:09, yehudak . wrote: > Hi again, > I looked a little deeper at your code. Smart solution and kudos. > > Yehuda > Still top posting? Thanks a bunch. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.python
csiph-web