Numbers of length N and value less than K

#forming_numbers

Gowtham Ghaandhi M Oct 04 2020 · 1 min read
Share this
Forming required numbers with the given digits and those are less than or equal to the given number.

Conditions on Input
1. List A is not empty
2. Elements of list A must be digits between 0 and 9
3. The value of B must be a digit from 0 and 9
4. The Value of C must be a positive integer.
5. Number of digits in C must be greater than or equal to the value of B

-----------------------------------------------------------------------------------------------------------------------

Solution

class Solution:
    # @param A : list of integers
    # @param B : integer
    # @param C : integer
    # @return an integer
    def __init__(self, A, B, C):
        self.A=list(set(A))  # Makes distinct elements in list A
        self.B=B
        self.C=C      
    def solve(self):
        if self.A==[]:
            raise Exception("Please provide non-empty list(A)")
        elif self.A!=[]:
            for i in self.A:
                if not(0<=i<9):
                    raise Exception("Please provide the values in the list(A) are only digits from 0 to 9")
        elif not(0<=self.B<=9):
            raise Exception("Please provide the values of B are only digits from 0 to 9")
        elif self.C<=0:
            raise Exception("Please provide the values of B are only digits from 0 to 9")
            
        from itertools import combinations_with_replacement 
        comb = combinations_with_replacement(self.A*2, self.B)
        List = []
        for i in list(comb):
            str_num=""
            for j in range(self.B):
                str_num = str_num+str(i[j])
            if len(str_num)>1:
                if str_num[0]!='0' and int(str_num)<self.C:
                    List.append(int(str_num))
            elif int(str_num)<self.C:
                List.append(int(str_num))
        
        print(sorted(list(set(List))))

Testing

---------------------------------------------------------------------------------------

Input_1

sol = Solution([0, 1, 5], 1, 2)
sol.solve()

Output_1
[0, 1]

---------------------------------------------------------------------------------------

Input_2

sol = Solution([0, 1, 2, 5], 2, 21)
sol.solve()

Output_2
[10, 11, 12, 15, 20]

---------------------------------------------------------------------------------------

Input_3

sol = Solution([0, 1, 2, 5, 7], 3, 500)
sol.solve()

Output_3
[100, 101, 102, 105, 107, 110, 111, 112, 115, 117, 120, 121, 122, 125, 127, 150, 151, 152, 155, 157, 170, 171, 172, 175, 177, 200, 201, 202, 205, 207, 211, 212, 215, 217, 220, 221, 222, 225, 227, 250, 251, 252, 255, 257, 270, 271, 272, 275, 277]

---------------------------------------------------------------------------------------

ipynb file link in GitHub:
https://github.com/muralispaiitm/Solutions-of-Programs/blob/main/03-10-2020%20-%2001%20Numbers%20of%20length%20N%20and%20value%20less%20than%20K.ipynb

* * *
Comments
Read next