Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tuples & Functions
#6
One should always look for hints in assignment text. In this one there is prominently displayed hint:

Quote:There is a app function for that!

And also important information:

Quote:To receive full credit on this problem, you should avoid building up a string containing the digits of the number. Intead, you should solve this problem by manipulating the nunmber itself.

So if converting to string out of the question what should be 'function for that'? How can we get digits out of number? There are //, % operators and there is built-in function divmod() which combines them both.

One can loop with divmod() and get the digits.

Just for fun ('return 0 if divisor is 0 otherwise return sum of boolean values for digit in number divisible by divisor'):

def count_divisible_digits(num, divisor):
    if divisor == 0:
        return 0
    num = abs(num)
    def digits(num): 
        while num:
            num, digit = divmod(num, 10)
            yield digit
    return sum(digit % divisor == 0 for digit in digits(num))


assert (count_divisible_digits(650899, 3)) == 4
assert (count_divisible_digits(-204, 5)) == 1
assert (count_divisible_digits(24, 5)) == 0
assert (count_divisible_digits(1, 0)) == 0
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Tuples & Functions - by nman52 - Nov-09-2020, 12:27 AM
RE: Tuples & Functions - by nman52 - Nov-09-2020, 01:16 AM
RE: Tuples & Functions - by nman52 - Nov-09-2020, 03:00 AM
RE: Tuples & Functions - by deanhystad - Nov-09-2020, 04:00 AM
RE: Tuples & Functions - by buran - Nov-09-2020, 05:03 AM
RE: Tuples & Functions - by perfringo - Nov-09-2020, 09:35 AM
RE: Tuples & Functions - by nman52 - Nov-14-2020, 10:50 AM
RE: Tuples & Functions - by perfringo - Nov-14-2020, 07:36 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020