Thursday, September 21, 2023
HomeSoftware EngineeringFiguring out Integer Depth in Python

Figuring out Integer Depth in Python


The problem

The depth of an integer n is outlined to be what number of multiples of n it’s essential to compute earlier than all 10 digits have appeared at the least as soon as in some a number of.

Instance:

let see n=42

A number of         worth         digits     remark
42*1              42            2,4 
42*2              84             8         4 existed
42*3              126           1,6        2 existed
42*4              168            -         all existed
42*5              210            0         2,1 existed
42*6              252            5         2 existed
42*7              294            9         2,4 existed
42*8              336            3         6 existed 
42*9              378            7         3,8 existed

Wanting on the above desk underneath digits column yow will discover all of the digits from “ to 9, Therefore it required 9 multiples of 42 to get all of the digits. So the depth of 42 is 9. Write a operate named computeDepth which computes the depth of its integer argument. Solely optimistic numbers higher than zero will probably be handed as an enter.

The answer in Python code

Possibility 1:

def compute_depth(n):
    i = 0
    digits = set()
    whereas len(digits) < 10:
        i += 1
        digits.replace(str(n * i))
    return i

Possibility 2:

def compute_depth(n):
    s, i = set(str(n)), 1
    whereas len(s) < 10:
        i += 1
        s |= set(str(n*i))
    return i

Possibility 3:

from itertools import depend

def compute_depth(n):
    discovered = set()
    replace = discovered.replace
    return subsequent(i for i,x in enumerate(depend(n, n), 1) if replace(str(x)) or len(discovered) == 10)

Check instances to validate our answer

check.it("Fundamental checks")
check.assert_equals(compute_depth(1),10)
check.assert_equals(compute_depth(42),9)
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments