Leetcode 728 complier issue with mutliple functions

  • Thread starter Thread starter Taylor_1989
  • Start date Start date
  • Tags Tags
    Functions
AI Thread Summary
The discussion revolves around a coding challenge involving self-dividing numbers, which are numbers divisible by each of their digits, excluding zero. A solution was submitted that works correctly in an online compiler but fails on LeetCode. The code is structured into three functions: Checker, All_Divsable, and res. The issue arises from the indentation of the return statement in the LeetCode version, which prevents the main logic from executing. Specifically, the return statement is placed at the same level as a function definition, causing the function to terminate prematurely. The conversation highlights the importance of proper indentation in Python, especially within class methods, and points out that the original code structure differs from the LeetCode submission, leading to the confusion.
Taylor_1989
Messages
400
Reaction score
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:

[CODE lang="python" title="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)[/CODE]

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
What's the point of doing a code challenge and asking here for the answer?
Check your indentation from line 40-44
 
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.
 
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
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
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.
 
When do you think the code in lines 40-44 is going to execute?
 
  • Like
Likes sysprog
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.
 
[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:
[CODE lang="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)
[/CODE]
 
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.
 

Similar threads

Replies
9
Views
3K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
1
Views
4K
Replies
3
Views
3K
Back
Top