Hướng dẫn check digit in number python - kiểm tra chữ số trong số python

Đã vài năm kể từ khi câu hỏi này được hỏi, nhưng tôi đã biên soạn một điểm chuẩn của một số phương pháp để tính độ dài của một số nguyên.

def libc_size(i): 
    return libc.snprintf(buf, 100, c_char_p(b'%i'), i) # equivalent to `return snprintf(buf, 100, "%i", i);`

def str_size(i):
    return len(str(i)) # Length of `i` as a string

def math_size(i):
    return 1 + math.floor(math.log10(i)) # 1 + floor of log10 of i

def exp_size(i):
    return int("{:.5e}".format(i).split("e")[1]) + 1 # e.g. `1e10` -> `10` + 1 -> 11

def mod_size(i):
    return len("%i" % i) # Uses string modulo instead of str(i)

def fmt_size(i):
    return len("{0}".format(i)) # Same as above but str.format

(Hàm LIBC yêu cầu một số thiết lập mà tôi chưa bao gồm)

Time for libc size:      1.2204 μs
Time for string size:    309.41 ns
Time for math size:      329.54 ns
Time for exp size:       1.4902 μs
Time for mod size:       249.36 ns
Time for fmt size:       336.63 ns
In order of speed (fastest first):
+ mod_size (1.000000x)
+ str_size (1.240835x)
+ math_size (1.321577x)
+ fmt_size (1.350007x)
+ libc_size (4.894290x)
+ exp_size (5.976219x)
1 là cảm ơn Brian Preslopsky,
Time for libc size:      1.2204 μs
Time for string size:    309.41 ns
Time for math size:      329.54 ns
Time for exp size:       1.4902 μs
Time for mod size:       249.36 ns
Time for fmt size:       336.63 ns
In order of speed (fastest first):
+ mod_size (1.000000x)
+ str_size (1.240835x)
+ math_size (1.321577x)
+ fmt_size (1.350007x)
+ libc_size (4.894290x)
+ exp_size (5.976219x)
2 là nhờ GeekTantra, và
Time for libc size:      1.2204 μs
Time for string size:    309.41 ns
Time for math size:      329.54 ns
Time for exp size:       1.4902 μs
Time for mod size:       249.36 ns
Time for fmt size:       336.63 ns
In order of speed (fastest first):
+ mod_size (1.000000x)
+ str_size (1.240835x)
+ math_size (1.321577x)
+ fmt_size (1.350007x)
+ libc_size (4.894290x)
+ exp_size (5.976219x)
3 là nhờ John La Rooy

Đây là kết quả:

Time for libc size:      1.2204 μs
Time for string size:    309.41 ns
Time for math size:      329.54 ns
Time for exp size:       1.4902 μs
Time for mod size:       249.36 ns
Time for fmt size:       336.63 ns
In order of speed (fastest first):
+ mod_size (1.000000x)
+ str_size (1.240835x)
+ math_size (1.321577x)
+ fmt_size (1.350007x)
+ libc_size (4.894290x)
+ exp_size (5.976219x)

(Tuyên bố miễn trừ trách nhiệm: Hàm được chạy trên các đầu vào từ 1 đến 1.000.000)

Dưới đây là kết quả cho

Time for libc size:      1.2204 μs
Time for string size:    309.41 ns
Time for math size:      329.54 ns
Time for exp size:       1.4902 μs
Time for mod size:       249.36 ns
Time for fmt size:       336.63 ns
In order of speed (fastest first):
+ mod_size (1.000000x)
+ str_size (1.240835x)
+ math_size (1.321577x)
+ fmt_size (1.350007x)
+ libc_size (4.894290x)
+ exp_size (5.976219x)
4 đến
Time for libc size:      1.2204 μs
Time for string size:    309.41 ns
Time for math size:      329.54 ns
Time for exp size:       1.4902 μs
Time for mod size:       249.36 ns
Time for fmt size:       336.63 ns
In order of speed (fastest first):
+ mod_size (1.000000x)
+ str_size (1.240835x)
+ math_size (1.321577x)
+ fmt_size (1.350007x)
+ libc_size (4.894290x)
+ exp_size (5.976219x)
5:

Time for libc size:      1.4686 μs
Time for string size:    395.76 ns
Time for math size:      485.94 ns
Time for exp size:       1.6826 μs
Time for mod size:       364.25 ns
Time for fmt size:       453.06 ns
In order of speed (fastest first):
+ mod_size (1.000000x)
+ str_size (1.086498x)
+ fmt_size (1.243817x)
+ math_size (1.334066x)
+ libc_size (4.031780x)
+ exp_size (4.619188x)

Như bạn có thể thấy,

Time for libc size:      1.2204 μs
Time for string size:    309.41 ns
Time for math size:      329.54 ns
Time for exp size:       1.4902 μs
Time for mod size:       249.36 ns
Time for fmt size:       336.63 ns
In order of speed (fastest first):
+ mod_size (1.000000x)
+ str_size (1.240835x)
+ math_size (1.321577x)
+ fmt_size (1.350007x)
+ libc_size (4.894290x)
+ exp_size (5.976219x)
6 (
Time for libc size:      1.2204 μs
Time for string size:    309.41 ns
Time for math size:      329.54 ns
Time for exp size:       1.4902 μs
Time for mod size:       249.36 ns
Time for fmt size:       336.63 ns
In order of speed (fastest first):
+ mod_size (1.000000x)
+ str_size (1.240835x)
+ math_size (1.321577x)
+ fmt_size (1.350007x)
+ libc_size (4.894290x)
+ exp_size (5.976219x)
7) là nhanh nhất, nhanh hơn một chút so với sử dụng
Time for libc size:      1.2204 μs
Time for string size:    309.41 ns
Time for math size:      329.54 ns
Time for exp size:       1.4902 μs
Time for mod size:       249.36 ns
Time for fmt size:       336.63 ns
In order of speed (fastest first):
+ mod_size (1.000000x)
+ str_size (1.240835x)
+ math_size (1.321577x)
+ fmt_size (1.350007x)
+ libc_size (4.894290x)
+ exp_size (5.976219x)
8 và nhanh hơn đáng kể so với những người khác.

Ví dụ 1: Số lượng chữ số trong một số nguyên sử dụng trong khi vòng lặp

num = 3452
count = 0

while num != 0:
    num //= 10
    count += 1

print("Number of digits: " + str(count))

Đầu ra

Number of digits: 4

Trong chương trình này, vòng lặp trong khi được lặp lại cho đến khi biểu thức kiểm tra

Time for libc size:      1.2204 μs
Time for string size:    309.41 ns
Time for math size:      329.54 ns
Time for exp size:       1.4902 μs
Time for mod size:       249.36 ns
Time for fmt size:       336.63 ns
In order of speed (fastest first):
+ mod_size (1.000000x)
+ str_size (1.240835x)
+ math_size (1.321577x)
+ fmt_size (1.350007x)
+ libc_size (4.894290x)
+ exp_size (5.976219x)
9 được đánh giá thành 0 (sai).

  1. Sau lần lặp đầu tiên,
    Time for libc size:      1.4686 μs
    Time for string size:    395.76 ns
    Time for math size:      485.94 ns
    Time for exp size:       1.6826 μs
    Time for mod size:       364.25 ns
    Time for fmt size:       453.06 ns
    In order of speed (fastest first):
    + mod_size (1.000000x)
    + str_size (1.086498x)
    + fmt_size (1.243817x)
    + math_size (1.334066x)
    + libc_size (4.031780x)
    + exp_size (4.619188x)
    
    0 sẽ được chia cho 10 và giá trị của nó sẽ là 345. Sau đó,
    Time for libc size:      1.4686 μs
    Time for string size:    395.76 ns
    Time for math size:      485.94 ns
    Time for exp size:       1.6826 μs
    Time for mod size:       364.25 ns
    Time for fmt size:       453.06 ns
    In order of speed (fastest first):
    + mod_size (1.000000x)
    + str_size (1.086498x)
    + fmt_size (1.243817x)
    + math_size (1.334066x)
    + libc_size (4.031780x)
    + exp_size (4.619188x)
    
    1 được tăng lên 1.
  2. Sau lần lặp thứ hai, giá trị của
    Time for libc size:      1.4686 μs
    Time for string size:    395.76 ns
    Time for math size:      485.94 ns
    Time for exp size:       1.6826 μs
    Time for mod size:       364.25 ns
    Time for fmt size:       453.06 ns
    In order of speed (fastest first):
    + mod_size (1.000000x)
    + str_size (1.086498x)
    + fmt_size (1.243817x)
    + math_size (1.334066x)
    + libc_size (4.031780x)
    + exp_size (4.619188x)
    
    0 sẽ là 34 và
    Time for libc size:      1.4686 μs
    Time for string size:    395.76 ns
    Time for math size:      485.94 ns
    Time for exp size:       1.6826 μs
    Time for mod size:       364.25 ns
    Time for fmt size:       453.06 ns
    In order of speed (fastest first):
    + mod_size (1.000000x)
    + str_size (1.086498x)
    + fmt_size (1.243817x)
    + math_size (1.334066x)
    + libc_size (4.031780x)
    + exp_size (4.619188x)
    
    1 được tăng lên 2.
  3. Sau lần lặp thứ ba, giá trị của
    Time for libc size:      1.4686 μs
    Time for string size:    395.76 ns
    Time for math size:      485.94 ns
    Time for exp size:       1.6826 μs
    Time for mod size:       364.25 ns
    Time for fmt size:       453.06 ns
    In order of speed (fastest first):
    + mod_size (1.000000x)
    + str_size (1.086498x)
    + fmt_size (1.243817x)
    + math_size (1.334066x)
    + libc_size (4.031780x)
    + exp_size (4.619188x)
    
    0 sẽ là 3 và
    Time for libc size:      1.4686 μs
    Time for string size:    395.76 ns
    Time for math size:      485.94 ns
    Time for exp size:       1.6826 μs
    Time for mod size:       364.25 ns
    Time for fmt size:       453.06 ns
    In order of speed (fastest first):
    + mod_size (1.000000x)
    + str_size (1.086498x)
    + fmt_size (1.243817x)
    + math_size (1.334066x)
    + libc_size (4.031780x)
    + exp_size (4.619188x)
    
    1 được tăng lên 3.
  4. Sau lần lặp thứ tư, giá trị của
    Time for libc size:      1.4686 μs
    Time for string size:    395.76 ns
    Time for math size:      485.94 ns
    Time for exp size:       1.6826 μs
    Time for mod size:       364.25 ns
    Time for fmt size:       453.06 ns
    In order of speed (fastest first):
    + mod_size (1.000000x)
    + str_size (1.086498x)
    + fmt_size (1.243817x)
    + math_size (1.334066x)
    + libc_size (4.031780x)
    + exp_size (4.619188x)
    
    0 sẽ là 0 và
    Time for libc size:      1.4686 μs
    Time for string size:    395.76 ns
    Time for math size:      485.94 ns
    Time for exp size:       1.6826 μs
    Time for mod size:       364.25 ns
    Time for fmt size:       453.06 ns
    In order of speed (fastest first):
    + mod_size (1.000000x)
    + str_size (1.086498x)
    + fmt_size (1.243817x)
    + math_size (1.334066x)
    + libc_size (4.031780x)
    + exp_size (4.619188x)
    
    1 được tăng lên 4.
  5. Sau đó, biểu thức kiểm tra được đánh giá là sai và vòng lặp chấm dứt.

Ví dụ 2: Sử dụng các phương thức sẵn có

num = 123456
print(len(str(num)))

Đầu ra

6

Trong chương trình này, vòng lặp trong khi được lặp lại cho đến khi biểu thức kiểm tra

Time for libc size:      1.2204 μs
Time for string size:    309.41 ns
Time for math size:      329.54 ns
Time for exp size:       1.4902 μs
Time for mod size:       249.36 ns
Time for fmt size:       336.63 ns
In order of speed (fastest first):
+ mod_size (1.000000x)
+ str_size (1.240835x)
+ math_size (1.321577x)
+ fmt_size (1.350007x)
+ libc_size (4.894290x)
+ exp_size (5.976219x)
9 được đánh giá thành 0 (sai).

  1. Làm thế nào để
  2. Python làm thế nào
  3. Tìm số chữ số trong một số trong Python

Được tạo ra: Tháng 6 năm 07, 2021 | Cập nhật: Tháng 8 đến 10 tháng 8 năm 2021

  1. Tìm số chữ số bên trong một số có hàm
    num = 3452
    count = 0
    
    while num != 0:
        num //= 10
        count += 1
    
    print("Number of digits: " + str(count))
    0 trong Python
  2. Tìm số chữ số bên trong một số có hàm
    Time for libc size:      1.4686 μs
    Time for string size:    395.76 ns
    Time for math size:      485.94 ns
    Time for exp size:       1.6826 μs
    Time for mod size:       364.25 ns
    Time for fmt size:       453.06 ns
    In order of speed (fastest first):
    + mod_size (1.000000x)
    + str_size (1.086498x)
    + fmt_size (1.243817x)
    + math_size (1.334066x)
    + libc_size (4.031780x)
    + exp_size (4.619188x)
    
    9 trong Python

Hướng dẫn này sẽ giới thiệu các phương thức để đếm số chữ số bên trong một số trong Python.

Tìm số chữ số bên trong một số có hàm num = 3452 count = 0 while num != 0: num //= 10 count += 1 print("Number of digits: " + str(count))0 trong Python

Tìm số chữ số bên trong một số có hàm

Time for libc size:      1.4686 μs
Time for string size:    395.76 ns
Time for math size:      485.94 ns
Time for exp size:       1.6826 μs
Time for mod size:       364.25 ns
Time for fmt size:       453.06 ns
In order of speed (fastest first):
+ mod_size (1.000000x)
+ str_size (1.086498x)
+ fmt_size (1.243817x)
+ math_size (1.334066x)
+ libc_size (4.031780x)
+ exp_size (4.619188x)
9 trong Python

Hướng dẫn này sẽ giới thiệu các phương thức để đếm số chữ số bên trong một số trong Python.

import math
n = -10
if n > 0:
    digits = int(math.log10(n))+1
elif n == 0:
    digits = 1
elif n < 0:
    digits = int(math.log10(-n))+2
print(digits)

Output:

3

Hàm

num = 3452
count = 0

while num != 0:
    num //= 10
    count += 1

print("Number of digits: " + str(count))
0 bên trong mô -đun
num = 3452
count = 0

while num != 0:
    num //= 10
    count += 1

print("Number of digits: " + str(count))
4 của Python được sử dụng để tìm nhật ký của cơ sở 10 cho bất kỳ số được chỉ định nào. Vì các số nguyên cũng nằm trong cơ sở 10, chúng ta có thể có số lượng các chữ số bên trong số nguyên được chỉ định với phương pháp này.

Đoạn mã sau đây cho chúng ta thấy cách chúng ta có thể tìm thấy số chữ số bên trong một số có hàm

num = 3452
count = 0

while num != 0:
    num //= 10
    count += 1

print("Number of digits: " + str(count))
0.

Chúng tôi đã tính toán số chữ số bên trong số -10 với hàm

num = 3452
count = 0

while num != 0:
    num //= 10
    count += 1

print("Number of digits: " + str(count))
0 trong mã trên. Mã này cũng xử lý trường hợp số là 0, vì nhật ký của 0 không thể được tính toán. Trước tiên chúng tôi kiểm tra xem số lớn hơn 0. Nếu số lớn hơn 0, chúng tôi tính toán các chữ số bằng cách lấy nhật ký và thêm 1 vào kết quả. Quá trình này được thực hiện vì nhật ký của bất kỳ số nào là ít hơn 1 so với số chữ số bên trong số đó.

Tìm số chữ số bên trong một số có hàm Time for libc size: 1.4686 μs Time for string size: 395.76 ns Time for math size: 485.94 ns Time for exp size: 1.6826 μs Time for mod size: 364.25 ns Time for fmt size: 453.06 ns In order of speed (fastest first): + mod_size (1.000000x) + str_size (1.086498x) + fmt_size (1.243817x) + math_size (1.334066x) + libc_size (4.031780x) + exp_size (4.619188x) 9 trong Python

Hướng dẫn này sẽ giới thiệu các phương thức để đếm số chữ số bên trong một số trong Python.

Hàm

num = 3452
count = 0

while num != 0:
    num //= 10
    count += 1

print("Number of digits: " + str(count))
0 bên trong mô -đun
num = 3452
count = 0

while num != 0:
    num //= 10
    count += 1

print("Number of digits: " + str(count))
4 của Python được sử dụng để tìm nhật ký của cơ sở 10 cho bất kỳ số được chỉ định nào. Vì các số nguyên cũng nằm trong cơ sở 10, chúng ta có thể có số lượng các chữ số bên trong số nguyên được chỉ định với phương pháp này.

n = -100.90
digits = len(str(n))
print(digits)

Output:

Time for libc size:      1.2204 μs
Time for string size:    309.41 ns
Time for math size:      329.54 ns
Time for exp size:       1.4902 μs
Time for mod size:       249.36 ns
Time for fmt size:       336.63 ns
In order of speed (fastest first):
+ mod_size (1.000000x)
+ str_size (1.240835x)
+ math_size (1.321577x)
+ fmt_size (1.350007x)
+ libc_size (4.894290x)
+ exp_size (5.976219x)
0

Đoạn mã sau đây cho chúng ta thấy cách chúng ta có thể tìm thấy số chữ số bên trong một số có hàm

num = 3452
count = 0

while num != 0:
    num //= 10
    count += 1

print("Number of digits: " + str(count))
0.

Phương pháp

Time for libc size:      1.4686 μs
Time for string size:    395.76 ns
Time for math size:      485.94 ns
Time for exp size:       1.6826 μs
Time for mod size:       364.25 ns
Time for fmt size:       453.06 ns
In order of speed (fastest first):
+ mod_size (1.000000x)
+ str_size (1.086498x)
+ fmt_size (1.243817x)
+ math_size (1.334066x)
+ libc_size (4.031780x)
+ exp_size (4.619188x)
9 vượt trội hơn nhiều so với phương pháp
num = 3452
count = 0

while num != 0:
    num //= 10
    count += 1

print("Number of digits: " + str(count))
0 để tìm số chữ số bên trong số thập phân trong Python. Lý do là phương pháp
Time for libc size:      1.4686 μs
Time for string size:    395.76 ns
Time for math size:      485.94 ns
Time for exp size:       1.6826 μs
Time for mod size:       364.25 ns
Time for fmt size:       453.06 ns
In order of speed (fastest first):
+ mod_size (1.000000x)
+ str_size (1.086498x)
+ fmt_size (1.243817x)
+ math_size (1.334066x)
+ libc_size (4.031780x)
+ exp_size (4.619188x)
9 rõ ràng, súc tích và cũng xử lý các điểm thập phân nổi không giống như phương pháp
num = 3452
count = 0

while num != 0:
    num //= 10
    count += 1

print("Number of digits: " + str(count))
0, phức tạp không cần thiết và không xử lý các điểm thập phân nổi.

Bài viết liên quan - Số Python

  • Tạo một danh sách các số lẻ trong Python
  • Chuyển đổi thư thành số trong Python
  • Hiển thị một số với số không hàng đầu trong Python
  • Kiểm tra xem một ký tự là một số trong Python
  • Làm thế nào để bạn kiểm tra xem một số có một chữ số?

    Bool chứa Digitit (số int, int digit);Nếu số chứa chữ số, thì hàm sẽ trả về true.Nếu không, chức năng sẽ trả về sai. If number contains digit, then the function should return true . Otherwise, the function should return false .

    Làm thế nào để bạn tìm thấy số chữ số trong một số?

    Công thức sẽ là số nguyên của (log10 (số) + 1).Ví dụ, nếu số là 1245, thì nó trên 1000 và dưới 10000, do đó giá trị nhật ký sẽ nằm trong phạm vi 3 integer of (log10(number) + 1). For an example, if the number is 1245, then it is above 1000, and below 10000, so the log value will be in range 3 < log10(1245) < 4. Now taking the integer, it will be 3. Then add 1 with it to get number of digits.