Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Trace the alphabet by moving Right, Down, Left and Up through a matrix
- a = ["ACRFGHOT",
- "BCDEVIQR",
- "PXDUKJPQ",
- "FYASLOPQ",
- "RTVHMNSR",
- "YSZVVUTB",
- "MDYXWUPV",
- "REWQSDEF"]
- rows = len(a)
- cols = len(a[0])
- letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ."
- idx = 0 # position within alphabet
- # note . as an end marker
- # dict of x,y moves per direction
- # where x is column, y is row
- # e.g. Right adds 1 to x, y stays same
- dirs = {"R": (1, 0), "D": (0, 1), "L": (-1, 0), "U": (0, -1)}
- x, y = -1, 0 # start 1 place to left of top left corner
- # loop until all alphabet found
- while letters[idx] != ".":
- for d in dirs.keys(): # R, D, L, U
- # get moves for a direction
- dx, dy = dirs[d]
- # compute temporary next pos
- dx += x
- dy += y
- # check column and row indexes are not out of bounds
- if -1 < dx < cols and -1 < dy < rows:
- # does this pos hold the next letter?
- if a[dy][dx] == letters[idx]:
- break # yes, quit inner loop
- print(d, letters[idx]) # show progress
- # confirm good position
- x, y = dx, dy
- # bump idx to next letter
- idx += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement