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 . <katye2...@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.b...@gmail.com> > wrote: > >> 2016-01-02 18:34 GMT+01:00 yehudak . <katye2...@gmail.com>: >> [partly edited for bottom posting] >> > On Sat, Jan 2, 2016 at 5:24 PM, Vlastimil Brom < >> vlastimil.b...@gmail.com> >> > wrote: >> >> >> >> 2016-01-02 14:14 GMT+01:00 yehudak . <katye2...@gmail.com>: >> >> > >> [...]>> > On Sat, Jan 2, 2016 at 2:44 PM, Vlastimil Brom >> >> > <vlastimil.b...@gmail.com> >> >> > wrote: >> >> >> >> >> >> 2016-01-02 12:49 GMT+01:00 <katye2...@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 >> > > -- https://mail.python.org/mailman/listinfo/python-list