Leetcode 728 complier issue with mutliple functions

  • Thread starter Taylor_1989
  • Start date
  • Tags
    Functions
In summary: 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:
  • #1
Taylor_1989
402
14
TL;DR Summary
I am having an issue compiling a solution for a leetcode problem, which compiles fine on an online compiler.
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 can't seem to understand why it works fine in the online compiler but not there. Any ideas?
 
Technology news on Phys.org
  • #2
What's the point of doing a code challenge and asking here for the answer?
Check your indentation from line 40-44
 
  • #3
pbuk said:
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
Taylor_1989 said:
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
You do realize 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
pbuk said:
You do realize 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
When do you think the code in lines 40-44 is going to execute?
 
  • Like
Likes sysprog
  • #8
pbuk said:
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
[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
pbuk said:
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
pbuk said:
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
sysprog said:
Line 40 is a return statement and line 38 isn't.
PeterDonis said:
I think you mean "line 33" instead of "line 38" here.
Thanks, I have edited my post to make it clearer.
 
  • #13
Can it be this simple?

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

1. What is Leetcode 728 compiler issue with multiple functions?

Leetcode 728 compiler issue with multiple functions refers to a problem that occurs when trying to compile a program with multiple functions using the Leetcode 728 compiler. It can cause errors or unexpected behavior in the program.

2. How does Leetcode 728 compiler handle multiple functions?

The Leetcode 728 compiler follows a specific set of rules and syntax when compiling a program with multiple functions. It checks for any syntax errors and ensures that the functions are declared and called correctly. It also manages the memory allocation for the functions.

3. Why does Leetcode 728 compiler issue occur with multiple functions?

Leetcode 728 compiler issue can occur due to various reasons, such as incorrect syntax in the program, mismatched function declarations and calls, or conflicts between different functions. It can also occur if the compiler is outdated or not compatible with the program.

4. How can I fix Leetcode 728 compiler issue with multiple functions?

To fix Leetcode 728 compiler issue, you can try checking the syntax of your program and ensuring that all functions are declared and called correctly. If the issue persists, you can update your compiler to the latest version or switch to a different compiler that is compatible with your program.

5. Can Leetcode 728 compiler issue with multiple functions be prevented?

Yes, Leetcode 728 compiler issue with multiple functions can be prevented by following proper coding practices and regularly testing your code. It is also essential to use a compatible and updated compiler for your program and to fix any errors or warnings that arise during compilation.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
3
Replies
97
Views
7K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top