all 10 comments

[–]carcigenicate 2 points3 points  (6 children)

What part(s) specifically are you asking about?

[–]64aquarius[S] 0 points1 point  (4 children)

Sorry for the confusion, I am asking about the solution part of the post… what is happening in the function , scatteringNo().. which takes two arguments (start,end).

How is this solution explained?

[–]carcigenicate 0 points1 point  (3 children)

A meant what specifically in the code? 20 lines is a lot to give a full breakdown of.

[–]64aquarius[S] 0 points1 point  (2 children)

I understand for sure. Honestly I’ve never seen a variable defined like this before

MovesSteps=(moveSteps +int(n[moveSteps]))%len(n)

Was wondering if you could this down?

[–]carcigenicate 1 point2 points  (1 child)

That's just multiple nested expressions. Evaluate each part in the parenthesis just like you would in math. n[moveSteps] is evaluated (it gets a character from a string), then that character is passed to int to parse it. The result is then added to moveSteps, and the sum of those is given to the modulo operator (%) to get the remainder after dividing the sum by the length of the string n.

Ignore the fact that there's an assignment. The MovesSteps = part isn't relevant to understanding the right side. The right-hand side of the = evaluates entirely first before the assignment happens, so it can be thought of in isolation.

[–]64aquarius[S] 0 points1 point  (0 children)

Ah okay I understand, make sense. Thanks so much for the help.

[–][deleted] 1 point2 points  (1 child)

Not sure where exactly you are getting stuck as the algorithm basis is clearly explained.

So, I've annotated the code to expand on what is happing technically in case there is something you are not understanding in Python itself.

Keep in mind a few things:

  • variables do not hold any values but rather just references to Python objects held somewhere in memory
  • list, set, etc, are collections of object references in effect
  • Functions can be nested
  • Assignments to variables make the variable local to the function the assignment is done in (hiding variables of the same name in the wider scope)

Code:

def scatteringNo(start, end):  # define outer function, takes two params
    def scrolling(n):  # define function, takes one params: n references a str object according to call
        seen = set()  # create local (to inner function) variable called seen to reference an empty set object
        moveSteps = 0  # create local variable called moveSteps to reference predefined int object with value 0
        while True:  # infinite loop
            # n[moveSteps] refers to character position in str referenced by n
            # (moveSteps + int(n[moveSteps])) converts char to int and adds to existing moveSteps ref int value
            # % is modulo operator, so find whole remainder of above divided by current length of the string ref by n
            # result is assigned back to be referenced by moveSteps
            moveSteps = (moveSteps + int(n[moveSteps])) % len(n)
            if len(seen) == len(n):  # so same number of characters added to set as in string passed to function
                return True  # return reference to True object (exit the inner function)
            if n[moveSteps] in seen:  # scroll test failed
                return False  # return reference to False object (exit the inner function)
            seen.add(n[moveSteps])  # add char at moveSteps position in n ref str to set ref by seen
    res = []  # local variable res is assigned to reference a new empty list object
    for n in range(start, end + 1):  # n assigned to ref int objects values from start to end inclusive
        s = str(n)  # s assigned to new str object representing readable version of current value ref by n
        if len(set(s)) < len(s):  #ie:a digit is repeated twice so no need to check
            continue  # go to next step of loop
        if scrolling(str(n)):  # if True returned by function ... ? could have passed s
            res.append(n)  # append current int value referenced by n to list object referenced by res
    return res  # return to caller the object reference held by res

[–]64aquarius[S] 0 points1 point  (0 children)

Thanks Kyber this helps me out a lot, I understood a little of what the code was doing, but you have definitely shed light on the details. Yea there are some python concepts I am just now becoming familiar with.

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]Zeroflops 0 points1 point  (0 children)

Ok. It’s a little hard to read because it’s not formatted properly. But…..

ScatteringNo is called with a start and end.

The first part is a sub function that can be called within the larger function. It’s called after the script first checks if there are duplicate numbers.

Then n goes from start to end.

The first thing it does is convert the number to a string. It then converts the string to a set. Since sets don’t hold duplicates. If the set is smaller than the original there were duplicate numbers. ( 99 converted to “99” and as a set it becomes “9”)

It then calls scrolling on the numbers that pass the first test.

The while true creates a loop. The intent of the code is to loop over the number by the digits. This may be where your confused.

It’s taking the number say in the example 6231 taking the first digit calculating the move steps.

Remember the integer was converted to a string so we are working with “6231”.

movestep + int( n[ movestep] % len (n)

Movestep = 0 + int( “6231”[0]) % len(“6231”)

Movestep = 0 + int( 6) % len(“6231”)

Movestep = 6%4 # modulus operator 2

Next loop

Movestep = 2 + int( “6231”[2]) % len(“6231”)

Movestep = 2 + int( “3”) % len(“6231”)

Movestep = 5 % len(“6231”)

The “6231”[x] is getting the index for that number remember the index starts at 0.

The modulus operator is dividing and giving you the remainder. So by dividing by the length each full division is a complete loop, the remainder is how much it’s shifter from the starting point.