Leetcode 728 complier issue with mutliple functions

  • #1
402
14

Summary:

I am having an issue compiling a solution for a leetcode problem, which compiles fine on an online compiler.

Main Question or Discussion Point

I just submitted a solution for a leetcode problem, displayed below.

A self-dividing number is a number that is divisible by every digit it contains.

For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.

Also, a self-dividing number is not allowed to contain the digit zero.

Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.
Example 1:

Input:

left = 1, right = 22
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
The boundaries of each input argument are 1 <= left <= right <= 10000.
My solution is broken into three separate functions as shown below.

Python:
def Checker(dig, number):
    """
    Purpose: Function check if the given number is divisible by each
    digit in the given number.
   
    Parameters:
    dig (int) : Last digit of the given number i.e if n=128 then the first digit
    to be compared would be 8, second 2 etc per iteration.
   
    number (int) : Number to check if self divisible.
   
    Returns :
    bool : True or False. Ture if the number is divisible by the digit and False otherwise.
    """
    if number==0:
        return(False)
    else:
        return(dig !=0 and dig % number==0)


def All_Divsable(n):
    """
    Purpose: Function take the give number n and then take the end digit and checks if the number is divisible by the end didigt and repeats until . temp ==0.
   
    Parameters:
    n (int) : number to be check if self divisible
   
    Returns :
    bool : True or False. Ture if the number is sefl divisable False otherwise."""
    temp=n
    while temp > 0:
        temp=temp % 10
        if (Checker(n,temp)==False):
            return(False)
        temp //=10
    return(True)

def res(left, right):
    L=[]
    for i in range(left,right+1):
        if All_Divsable(i)==True:
            L.append(i)
    return(L)

On the site : https://www.programiz.com/python-programming/online-compiler/

This complies fine when I run there example case and get the same output.
Output : [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 21, 22]
However, when I copy the code into leetcode I get the following:

Leetcode:
class Solution(object):
    def selfDividingNumbers(self, left, right):
        def Checker(dig, number):
            """
            Purpose: Function check if the given number is divisible by each
            digit in the given number.

            Parameters:
            dig (int): Last digit of the given number i.e if n=128 then the first digit
            to be compared would be 8, second 2, etc per iteration.

            number (int): Number to check if self divisible.

            Returns :
            bool : True or False. Ture if the number is divisible by the digit and False otherwise.
            """
            if number==0:
                return(False)
            else:
                return(dig !=0 and dig % number==0)
       
       
        def All_Divsable(n):
            """
            Purpose: Function take the give number n and then take the end digit and checks if the number is divisible by the end didigt and repeats until . temp ==0.

            Parameters:
            n (int) : number to be check if self divisible

            Returns :
            bool : True or False. Ture if the number is sefl divisable False otherwise."""
            temp=n
            while temp > 0:
                temp=temp % 10
                if (Checker(n,temp)==False):
                    return(False)
                temp //=10
            return(True)
       
            L=[]
            for i in range(left,right+1):
                if All_Divsable(i)==True:
                    L.append(i)
            return(L)
And the output when compiled is :

1583255240959.png


But I cant seem to understand why it works fine in the online compiler but not there. Any ideas?
 

Answers and Replies

  • #2
pbuk
Science Advisor
Gold Member
1,457
437
What's the point of doing a code challenge and asking here for the answer?
Check your indentation from line 40-44
 
  • #3
402
14
What's the point of doing a code challenge and asking here for the answer?
Check your indentation from line 40-44
I am not asking for the answer, my sol works fine as was demonstrated.
Output : [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 21, 22]
I don't understand why it works fine on the online compiler and not on there.
 
  • #4
pbuk
Science Advisor
Gold Member
1,457
437
I am not asking for the answer, my sol works fine as was demonstrated.

I don't understand why it works fine on the online compiler and not on there.
Do you really think the code is exactly the same? There are quite a few differences, and if you click the blue button that says "Spoiler" you will find I have pointed you to where the critical one is.
 
  • Like
Likes sysprog
  • #5
pbuk
Science Advisor
Gold Member
1,457
437
You do realise the significance of the code at the beginning of the second box?
Python:
class Solution(object):
    def selfDividingNumbers(self, left, right):
 
  • Like
Likes sysprog
  • #6
402
14
You do realise the significance of the code at the beginning of the second box?
Python:
class Solution(object):
    def selfDividingNumbers(self, left, right):
Not in entirely, I not really covered oo atm, and tbh never had to use it when done any past question I have done on Leetcode.
 
  • #7
pbuk
Science Advisor
Gold Member
1,457
437
When do you think the code in lines 40-44 is going to execute?
 
  • Like
Likes sysprog
  • #8
402
14
When do you think the code in lines 40-44 is going to execute?
Well, I thought it would be the last thing to run. As the other two functions have to be initialised before it for the function to run as a whole.
 
  • #9
pbuk
Science Advisor
Gold Member
1,457
437
[Edited for clarity] Line 40 in the "Leetcode" code block in your OP has the same indentation as line 38 which is a return statement so it will never be reached (in the 'marked up' code below these are lines 36 and 33).

Let's look at what is going on:
Python:
# This starts a definition of a class.
class Solution(object):

    # This starts the definition of a member function of the Solution class. It can be invoked
    # as Solution().selfDividingNumbers(1, 22) which is presumably what the test code does.
    def selfDividingNumbers(self, left, right):

        # This starts the definition of an inner function which can only be accessed within the
        # definintion of its parent, selfDividingNumbers.
        def Checker(dig, number):
            # ...
            # This ends the definition of Checker but the else statement is unnecessary and
            # potentially confusing because the last line of the function has extra indentation.
            if number==0:
                # The brackets are unnecessary here and misleading as it looks as though you
                # are trying to call a function named return.
                return(False)
            else:
                # Agian the brackets are unnecessary but because of the compound statement
                # they do add a bit of clarity; personally I wouldn't use them in this case but if
                # you are going to use them you should put a space after return to avoid it
                # looking like a function call.
                return(dig !=0 and dig % number==0)
            # This is a better way. Note also the consistent use of spacing around ==.
            if number == 0:
                return False
            return (dig !=0 and dig % number == 0)
     
        # This starts the definition of another inner function.
        def All_Divsable(n):
            # ...
            # This statement will always execute
            return(True)

            # This statement will never be reached!
            L=[]
            for i in range(left,right+1):
                if All_Divsable(i)==True:
                    L.append(i)
            return(L)
 
Last edited:
  • #10
1,473
785
Line 40 has the same indentation as line 38 which is a return statement so it will never be reached.
Line 40 is a return statement and line 38 isn't.
 
  • #11
PeterDonis
Mentor
Insights Author
2019 Award
29,718
8,973
Line 40 has the same indentation as line 38 which is a return statement so it will never be reached.
I think you mean "line 33" instead of "line 38" here.
 
  • Like
Likes sysprog
  • #12
pbuk
Science Advisor
Gold Member
1,457
437
Line 40 is a return statement and line 38 isn't.
I think you mean "line 33" instead of "line 38" here.
Thanks, I have edited my post to make it clearer.
 
  • #13
Tom.G
Science Advisor
3,248
1,995
Can it be this simple?

The original code has a "def res..." on line 38.
That statement is missing in the others.
 

Related Threads on Leetcode 728 complier issue with mutliple functions

  • Last Post
Replies
13
Views
3K
  • Last Post
Replies
4
Views
2K
Replies
1
Views
1K
  • Last Post
Replies
2
Views
1K
  • Last Post
Replies
13
Views
2K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
15
Views
5K
Replies
6
Views
3K
Replies
13
Views
969
Top