简体   繁体   English

试图找出一个数字中有多少位是另一个数字的倍数

[英]Trying to figure out how many digits in a number are a multiple of another digit

I'm trying to write a function that takes in two elements n and m and will try to give a count of the digits in n that are multiples of m .我正在尝试编写一个 function ,它包含两个元素nm ,并将尝试给出nm的倍数的数字计数。

For example, if n = 650899, m = 3 , the answer is 4 because the digits {6, 0, 9, 9} are all evenly divisible by 3 , while the digits {5, 8} are not:例如,如果n = 650899, m = 3 ,则答案为4 ,因为数字{6, 0, 9, 9}都可以被3整除,而数字{5, 8}则不能:

Calculation         Cumulative count
----------------    ----------------
6 / 3 = 2                  1
5 / 3 = 1.666...
0 / 3 = 0                  2
8 / 3 = 2.666...
9 / 3 = 3                  3
9 / 3 = 3                  4 <- Answer

I'm trying to do this without using strings at all (such as by checking individual characters within the string).我试图在不使用字符串的情况下做到这一点(例如通过检查字符串中的单个字符)。 Does anyone know how I can manipulate just the number by itself?有谁知道我如何自己操纵数字?

Try the following:尝试以下操作:

def solution(n: int, m: int) -> int:
    """
    Check how many digits of n, are divisible by m.
    """
    
    ans = 0                    # initiate counter
    while n:
        # generate quotient and remainder when divided by 10
        n, r = divmod(n, 10)
        # increase counter by 1, if remainder is divisible by m
        ans += (r % m) == 0
    return ans

>>> solution(n=650899, m=3)
4

Okay, if you're after a count of the digits in your larger number that are an integer multiple of another digit, that can be done with modulo and division:好的,如果您要计算较大数字中的数字,即 integer 是另一个数字的倍数,则可以通过模数和除法来完成:

def digitsThatAreMults(checkNum, digit):
    # Init count to zero, we'll increment for every digit that matches.

    count = 0

    # Continue until no digits left.

    while checkNum != 0:
        # Digit is a multiple if no remainder when dividing.

        if (checkNum % 10) % digit == 0:
            count += 1

        # Debugging line to see in action.

        print("dbg", count, checkNum)

         # Strip off digit you just tested.

        checkNum = checkNum // 10

    # Now just return the count of the matches.

    return count

# Test harness, you may want to add some more test cases for decent coverage :-)

print(digitsThatAreMults(650899, 3))

With the debug line in there, you can see how it works, checking each digit (the one at the end) and increasing the count if it's a multiple:使用那里的调试行,您可以看到它是如何工作的,检查每个数字(最后一个)并增加计数(如果它是倍数):

dbg 1 650899
dbg 2 65089
dbg 2 6508
dbg 3 650
dbg 3 65
dbg 4 6
4

Once you're happy with code, you can delete the debug line, though I tend to leave things in there and just comment them out, since you often have to come back and debug the code for some bizarre edge case you didn't think of:-)一旦你对代码感到满意,你就可以删除调试行,虽然我倾向于把东西留在里面,只是把它们注释掉,因为你经常不得不回来调试一些你没想到的奇怪边缘情况的代码的:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法弄清楚如何链接多个嵌套的多对多关系 - Can't figure out how to link multiple nesting many to many relationships 我试图弄清楚如何计算在Python的字符串句子中字母大写的次数 - I'm trying to figure out how to count how many times a letter is capitalized in a string sentence in Python 在函数中循环 5 位数字的数字 - Cycling the digits of a 5 digit number in a function 查找数字中有多少次 - Finding how many times a digit is in a number 如何将Python列表中的后续数字连接成双(或更多)数字 - How to join subsequent digits in a Python list into a double (or more) digit number 如何生成一个不以 0 开头且具有唯一数字的随机 4 位数字? - How to generate a random 4 digit number not starting with 0 and having unique digits? 给定以下约束,如何将某些数字连接到另一个数字? - How to concatenate some digits to another digit given the following constraints? Python-查找数字精确到多少位数? - Python - Find how many digits a number is accurate to? 如何找到一个数字的质因数?花了几个小时试图弄清楚为什么 if 和 else 会同时执行 - how to find the prime factors of a number?spent hours trying to figure out why both the if and else are executed simultaneously 试图弄清楚如何用随机数找到我的乌龟的最终距离 - Trying to figure out how to find the final distance on my turtle with random number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM