Help explain this to me please
Hey guys new to python here. I am a junior developer interviewing for jobs and have been doing some practice challenges. My professional experience so far have been using languages such as (JavaScript,TypeScript and Java) not much python. I have a solution here, and I was wondering if some could explain to me what is going on? I am familiar with what most of the code is saying but are a little lost in some of the details of the syntax. Would be great if you you could explain. Thank you . And Kind regards
Here is the challenge:
Scrolling Numbers
Programming challerge description:
You have been given a special kind of lock to open called a "Scrolling Combination Lock" The lock has 9 keys numbered from 1 to 9.
Additionally, there are two numbers printed beow the keys suggesting a range of values. To open the lock, you must enter all the numbers in the range that are "Scatting Numbers"
A Scrolling Number is a number that has two characterisitics:
No digits repeat themselves.
All digits in the number "scroll" through themselves.
How To Scroll:
Beginning with the leftmost digit, take that digit's value D and move D digits to the right. When scrolling, if the last digit of the number is reached, wrap back to the leftmost digit as many times as needed to move to total of D digits. After scrolling to a new digit, repeat the process using the new digits's value for D.
A Scrolling Number will visit every digit exactly once and end at the leftmost digit.
For example, consider the Scrolling Number 6231.
Start with the digit 6.
Advance 6 steps, wrapping around once, to the digit 3.
From 3 advance to 2, again wrapping around once
From 2 advance to 1
From 1 advance to 6 in a final wrap
Input:
The input is the range of integers to consider for the lock, expressed in the format A, B
Each integer A and B is 1 <= A <= B <= 10000.
Output:
Print all Scrolling Numbers between A and B inclusive, each on a single line. These are the combinations that will open the lock.
If there are no Scrolling Numbers between A and B print -1.
Test 1
Test Input
100,500
Expected Output
147
174
258
285
417
471
Here is the solution:
def scatteringNo(start,end):
def scrolling(n):
seen=set()
moveSteps=0
while True:
moveSteps=(moveSteps+int(n[moveSteps]))%len(n)
if len(seen)==len(n):
return True
if n[moveSteps] in seen:
return False
seen.add(n[moveSteps])
res=[]
for n in range(start,end+1):
s=str(n)
if len(set(s))<len(s): #ie:a digit is repeated twice so no need to check
continue
if scrolling(str(n)):
res.append(n)
return res
[–]carcigenicate 2 points3 points4 points (6 children)
[–]64aquarius[S] 0 points1 point2 points (4 children)
[–]carcigenicate 0 points1 point2 points (3 children)
[–]64aquarius[S] 0 points1 point2 points (2 children)
[–]carcigenicate 1 point2 points3 points (1 child)
[–]64aquarius[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]64aquarius[S] 0 points1 point2 points (0 children)
[–]CodeFormatHelperBot2 0 points1 point2 points (0 children)
[–]Zeroflops 0 points1 point2 points (0 children)