-2

I want to check if there is a specific digit (let's say 8) in a specific integer (let's say 83) at a specific place (let's say second place). In this example, the answer is True. I wrote the following function :

def check_number(integer: int, digit: int, place: int) -> bool:
    if str(integer)[::-1][place-1] == str(digit) :
        return True
    return False

I want to have the more optimized way (in terms of speed) to do it as possible. Is my algorithm good or is there is better ?

LittleCoder
  • 391
  • 1
  • 13

1 Answers1

-1

A more optimized (though slightly uglier) approach would be to iterate over the integer and use division along with the modulus to check each digit:

import math

def check_number(integer: int, digit: int, place: int) -> bool:
    while place > 1:
        integer = math.floor(integer / 10)
        place -= 1

    if integer == 0:
        return False

    if integer % 10 == digit:
        return True
    else:
        return False

print(check_number(12345, 3, 3))  # True
print(check_number(12345, 1, 5))  # True
print(check_number(12345, 4, 1))  # False

In the above in order to "queue up" the digit to be checked, we iterate in a while loop and divide the input by 10 however many times is required to put the digit to be examined in place into the tens position. Then we check that value using the modulus. Should the input not have a digit in that place, we return false, and likewise we return false should the digit exist but not match the input.

Note that in general the above solution is preferable to a string based solution, because the overhead in creating and manipulating a string is fairly high, much higher than doing simple division of the input integer.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    Huh... Do you not know `//`? – Kelly Bundy Jun 22 '22 at 10:14
  • Thank you for your answer, but instead of using ```math.floor(integer / 10)```, in not ```integer // 10``` faster ? – LittleCoder Jun 22 '22 at 10:15
  • 1
    @LittleCoder I don't know what `//` does, I am newcomer to the Python programming language. – Tim Biegeleisen Jun 22 '22 at 10:16
  • ```//``` is the euclidean division in Python. ```a // b``` is like ```int(a / b)```, but probably faster. I don't really know. – LittleCoder Jun 22 '22 at 10:17
  • 1
    @LittleCoder It's most certainly faster. And also safer, as `/` becomes incorrect with a bit larger numbers. I'm also not at all convinced that this is faster than yours, even with `//`. – Kelly Bundy Jun 22 '22 at 10:18
  • Ok thank you, I will make a test between these two function and take the best. Thanks you again ! – LittleCoder Jun 22 '22 at 10:19
  • 1
    @LittleCoder How much faster than yours is this (I see you accepted it)? And how did you test, especially how large were the numbers/places? – Kelly Bundy Jun 22 '22 at 10:21
  • After a little test, it seems that your code (with ```//```) is about 2.5 times faster than mine. Well done ! I only test it on a 3-digit number, because in my case the biggest integer to test is 120, but speed becomes really important. – LittleCoder Jun 22 '22 at 10:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245824/discussion-between-littlecoder-and-kelly-bundy). – LittleCoder Jun 22 '22 at 15:38