-1

So I want to create 4 function: func_slideright() func_slideleft() func_up() and func_left()that will find and print all words from a grid like this.

[["r","a","w","b","i","t"],
["x","a","y","z","c","h"],
["p","q","b","e","i","e"],
["t","r","s","b","o","g"],
["u","w","x","v","i","t"],
["n","m","r","w","o","t"]]

so basically... func_slideright() will return every letter in the grid horizontally from left to right.. so the output should look like this.

n
um
twr
prxw
xqsvo

and so on... while func_slideleft() will print out the opposite of sliderright, so like...

t
ih
bce
wzig 

and so on.... while func_up() will return letters from top to bottom like

rxptun
aaqrwm
wybsxr

and so on... and func_left() returns all letters from left to right like

 rawbit
 xayczh

and so on... Also word: str is basically the word that I'm trying to find whether it's there on the grid or not.

However, I'm completely stuck on how to do this. No piece of code or program seems to click in my mind for some reason, so can anyone please help?? ** Also if u do generously help me, please consider writing in the simplest way possible and please an explaination as well so that I can learn and not ask repeated questions on here. Thank you :))**

Here is my code so far. Any help would be appreciated...

grid = [["r","a","w","b","i","t"],
["x","a","y","z","c","h"],
["p","q","b","e","i","e"],
["t","r","s","b","o","g"],
["u","w","x","v","i","t"],
["n","m","r","w","o","t"]]

def func_left(word, grid):
    word = list(word)
    for x in grid:
        if grid.index(word[x]) != 1:
           return word
print(func_left(word='raw', grid=[["r","a","w","b","i","t"],
["x","a","y","z","c","h"],
["p","q","b","e","i","e"],
["t","r","s","b","o","g"],
["u","w","x","v","i","t"],
["n","m","r","w","o","t"]]))

def func_slideleft(word, grid):
    word = list(word)
    ** I have no idea how to find this or how to find sliderright

def func_sliderright(word, grid):
    word = list(word)
    ** I have no idea how to find this or how to find sliderright

def func_up(word):
    word = list(word)
    for x in grid:
        return x[1]
Bruffff
  • 53
  • 7
  • It's also unclear what your function arguments mean. What is the `word` you pass to each function? How does that change the output? Your function names are also not intuitive. `func_left` for example, seems to return the lower diagonal values? – not_speshal Nov 06 '21 at 15:22
  • I have edited the word function and then changed func_left according to what I think is printing all characters from left to right. I apologize for the fact that my code looks like a mess – Bruffff Nov 06 '21 at 15:32
  • Does this answer your question? [Get all the diagonals in a matrix/list of lists in Python](https://stackoverflow.com/questions/6313308/get-all-the-diagonals-in-a-matrix-list-of-lists-in-python) – SuperStormer Nov 07 '21 at 02:00

1 Answers1

2

Assuming that you actually want all the diagonals in the matrix, here are 4 functions that will give the strings formed by going right, down, down-right, and up-right:

def diagsDownRight(M):
    diags,pad = [],[]
    while any(M):
        edge = [*next(zip(*reversed(M))),*M[0][1:]] # outer edge
        M    = [r[1:] for r in M[1:]]               # remaining matrix
        diags.append(pad+edge+pad)                  # diagonal columns
        pad.append("")                              # next padding
    return [*map("".join,zip(*diags))]              # join columns

def diagsUpRight(M):
    diags,pad = [],[]
    while any(M):
        edge = [*next(zip(*M)), *M[-1][1:]]
        M    = [r[1:] for r in M[:-1]]
        diags.append(pad+edge+pad)
        pad.append("")        
    return [*map("".join,zip(*diags))]

def rows(M):
    return ["".join(row) for row in M]
def cols(M):
    return ["".join(col) for col in zip(*M)]

Output:

mystery = [["r","a","w","b","i","t"],
           ["x","a","y","z","c","h"],
           ["p","q","b","e","i","e"],
           ["t","r","s","b","o","g"],
           ["u","w","x","v","i","t"],
           ["n","m","r","w","o","t"]]


print(diagsDownRight(mystery))
['n', 'um', 'twr', 'prxw', 'xqsvo', 'rabbit', 
 'ayeot', 'wzig', 'bce', 'ih', 't']

print(diagsUpRight(mystery))
['r', 'xa', 'paw', 'tqyb', 'urbzi', 'nwsect', 
 'mxbih', 'rvoe', 'wig', 'ot', 't']

print(rows(mystery))
['rawbit', 'xayzch', 'pqbeie', 'trsbog', 'uwxvit', 'nmrwot']

print(cols(mystery))
['rxptun', 'aaqrwm', 'wybsxr', 'bzebvw', 'icioio', 'thegtt']

The rows() and cols() functions are straightforward but the diagonals are a bit tricky.

For the diagonals, the matrix is broken down into L shaped slices which are padded on each side with empty characters. These slices are then combined to form the diagonal strings

For example:

Slice #1: (_ are empty padding characters)

r a w b i t --> r a w b i t --> n u t p x r a w b i t
x a y z c h --> x . . . . .
p q b e i e --> p . . . . .
t r s b o g --> t . . . . .
u w x v i t --> u . . . . .
n m r w o t --> n . . . . .

Slice #2
. . . . . . 
. a y z c h --> a y z c h   --> _ m w r q a y z c h _
. q b e i e --> q . . . .
. r s b o g --> r . . . .
. w x v i t --> w . . . .
. m r w o t --> m . . . .

Slice #3:
. . . . . . 
. . . . . . 
. . b e i e --> b e i e    --> _ _ r x s b e i e _ _
. . s b o g --> s . . .
. . x v i t --> x . . .
. . r w o t --> r . . .

Slice # 4                  --> _ _ _ w v b o g _ _ _
Slice # 5                  --> _ _ _ _ o i t _ _ _ _
Slice # 6                  --> _ _ _ _ _ t _ _ _ _ _

Then, concatenating the elements of each slice from 1 to 6 gives us the strings for each diagonal:

1 2 3 4 5 6
n _ _ _ _ _
u m _ _ _ _
t w r _ _ _
p r x w _ _
x q s v o _
r a b b i t
a y e o t _
w z i g _ _ 
b c e _ _ _
i h _ _ _ _
t _ _ _ _ _ 
Alain T.
  • 40,517
  • 4
  • 31
  • 51