简体   繁体   English

试图编写一个 function 来返回 n 中可被 m 整除的位数 - 太多 output - Python

[英]Trying to write a function that returns the number of digits in n that are divisble by m — Too much output - Python

I'm currently trying to write a function that will take n and m as an argument and then will return the number of digits in n that are divisible by m .我目前正在尝试编写一个 function ,它将nm作为参数,然后返回n中可被m整除的位数。 An example of this could be n = 305689 and m = 3. The answer here would be 4.这方面的一个例子可能是 n = 305689 和 m = 3。这里的答案是 4。

My program is able to get this result, but the interface I'm working is is saying that my program is running for too long or producing too much output.我的程序能够得到这个结果,但是我正在工作的界面是说我的程序运行时间太长或产生太多 output。

Here's my program so far:到目前为止,这是我的程序:

def count_divisible_digits(n, m):
    count = 0
    while n != 0:
        if (n % 10) % m == 0:
            count += 1
        #print("dbg", count, n)
        n = n // 10
    return count

Try converting the input n to string datatype and getting the count as follows.尝试将输入 n 转换为字符串数据类型并按如下方式获取计数。 This should be faster.这应该更快。

def count_divisible_digits(n, m):
  count = 0
  string = str(n)
  if n == 0 and m == 0:
    return 0
  elif m == 0:
    return 0
  elif n == 0: 
    return 1
  elif n < 0:
    x = 1
  else:
    x = 0
    
  for i in range(x, len(string)):
    if string[i] == '.' or 0 < int(string[i]) < m:
      continue
    elif int(string[i]) % m == 0:
      count += 1      
  return count

Try this again, miss the 0 ✅ check now;再试一次,错过0 ✅ 立即检查;

count =sum(1 for s in str(n) if int(s)%m==0 and int(s) >0)

暂无
暂无

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

相关问题 返回数字有多少位的函数digits(n),在python中返回一个随机值 - Function digits(n) that returns how many digits the number has , returns a random value in python Python-创建一个函数来检查一个数字是否可被其他数字整除 - Python - Creating a function to check if a number is divisble by other numbers 我正在尝试将随机数写入文件,然后从 Python 中的行创建一个列表,但列表也需要 &#39;\\n&#39; - I'm trying to write random numbers to a file and then make a list from lines in Python but list takes '\n' too 返回代表python中n的基数b表示形式的数字字符串的函数 - Function that returns a string of digits that represent the base b representation of n in python 在返回可被另一个整除的位数的 function 上没有得到正确的 output - Not getting the correct output on a function that returns the number of digits divisible by another 如何编写返回总位数和单个空格的 function - How to write a function which returns the total number of digits and single spaces 写一个函数,给定一个由 N 行 M 列整数组成的零索引矩阵 A,返回矩阵 A 的平衡点数 - Write a function given a zero-indexed matrix A consisting of N rows and M columns of integers, returns the number of equilibrium points of matrix A 尝试编写一个 function 生成长度为 N 的 M 个正态分布样本 - Trying to write a function generating M normally distributed samples of length N 我需要编写一个 python 程序来输入字母形式的电话号码,然后 output 以数字形式输入相应的电话号码 - I need to write a python program to enter a telephone number as letters and then output the corresponding telephone number in digits 写一个 function,给定自然数 n,m,确定最小的自然数 k 使得 n^k >= m,时间为 O(log k) - Write a function that, given natural numbers n, m, determines the smallest natural number k such that n^k >= m, in time O(log k)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM