Newbie101
Newbie101

Reputation: 501

How to shift a list one more time than before each time?

For example - a left shift:

Here is a grid of letters for example

tiwxz          
shshq          
avidy          
bsvsc          

This is the effect I want to have. The X symbol fills in the space created by the movement.

XXXtiwxz         # no shift
XXshshqX         # one shift left
XavidyXX         # two shift left
bsvscXXX         # three shift left

How would I do a left shift and a right shift for grid?

This is all I have got:

def diags(grid, rev=False):
    n = len(grid)
    _grid = [list(row) + [None]*(n-1) for row in grid]  

Upvotes: 1

Views: 80

Answers (2)

Shukla
Shukla

Reputation: 91

This function will do the job. To confirm backside spaces you can print the length of the string. input_grid is the list of a row as an element.

def formater_fn(input_grid):
    right_len = len(input_grid) + len(input_grid[0])
    left_len = len(input_grid) + len(input_grid[0])
    for item in input_grid:
        right_item = item.rjust(right_len)
        new_item = right_item.ljust(left_len)
        right_len = right_len-1
        print new_item

Upvotes: 3

slider
slider

Reputation: 12990

You can pad the left and right sides with Xs (or Nones) based on the row's index which you can get with enumerate:

grid = [list(s) for s in 'tiwxz shshq avidy bsvsc'.split()]
new_grid = [(['X'] * (len(grid) - 1) + lst)[i:] + ['X'] * i for i, lst in enumerate(grid)]

Output

for l in new_grid:
    print(l)

# ['X', 'X', 'X', 't', 'i', 'w', 'x', 'z']
# ['X', 'X', 's', 'h', 's', 'h', 'q', 'X']
# ['X', 'a', 'v', 'i', 'd', 'y', 'X', 'X']
# ['b', 's', 'v', 's', 'c', 'X', 'X', 'X']

Upvotes: 3

Related Questions