Leetcode 728 complier issue with mutliple functions

  • Thread starter Thread starter Taylor_1989
  • Start date Start date
  • Tags Tags
    Functions
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
12 replies · 2K views
Taylor_1989
Messages
400
Reaction score
14
TL;DR
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?
 
Physics news on Phys.org
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   Reactions: 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   Reactions: 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.
 
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:
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.
 
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   Reactions: sysprog
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.
 
Can it be this simple?

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