[Tutor] Detect duplicate digits in a number

Manprit Singh manpritsinghece at gmail.com
Tue Feb 8 20:39:13 EST 2022


Dear Sir,

I have to produce a result False if a number contains repeating digits and
True if the number do not have any repeating digit:

Example :
if number is 2343 the result produced must be False
if number is 235 the result produced must be True

I have tried to implement it in  the following ways:
1) Using user defined function

def detect_repeat(num):
    lx = []
    while num:
        num, rem = divmod(num, 10)
        if rem  in lx:
            return False
        lx.append(rem)
    return True

ans = detect_repeat(2343)
print(ans)   # Results in False that is the desired result

2) Using str, set, len

len(set(str(2343))) == len(str(2343))
that also produces the  desired result -False

The second solution according to me lacks readability and again there are
lots of conversion str, set and all .
The  first solution, I feel , is good.

Just need your opinion and guidance . In the first solution If you can see,
there are two return statements . Is this practice considered ok ?

Regards
Manprit Singh


More information about the Tutor mailing list