Reputation: 53
How do I make 2d lists in run time when user inputs some number.
Upvotes: 0
Views: 65
Reputation: 51653
There are easy and difficult things:
horizontal ist trivial - simply join the inner list and replace the word and the word reversed by its upper case representation, then split the string into letters
vertical is mildly more difficult, but you can handle it by transposing the matrix via zip
and after replacing transpose again
vertical is handled in the linked question Find Letter of Words in a Matrix Diagonally
Here is how to do horizontal and vertical:
data = [['l', 'd', 'l', 'o', 'h', 'p'],
['i', 't', 'i', 'f', 'w', 'f'],
['g', 'n', 'r', 'k', 'q', 'u'],
['h', 'g', 'u', 'a', 'l', 'l'],
['t', 'c', 'v', 'g', 't', 'l'],
['d', 'r', 'a', 'w', 'c', 's']]
words = ['draw', 'full', 'hold', 'laugh', 'light', 'start', 'all', 'kid']
from pprint import pprint as p
for w in words:
print()
# horizontal
data1 = [list(''.join(line).replace(w,w.upper())) for line in data]
# horizontal, word reversed
data1 = [list(''.join(line).replace(w[::-1],w[::-1].upper())) for line in data1]
# vertical
data1 = list(zip(*[list(''.join(line).replace(w,w.upper()))
for line in zip(*data1)]))
# vertical, word reversed
data1 = list(zip(*[list(''.join(line).replace(w[::-1],w[::-1].upper()))
for line in zip(*data1)]))
if data1 != data:
print(f"Found: {w}:")
data = data1
p(data)
else:
print(f"No {w} - check diagonally")
Output:
Found: draw:
[('l', 'd', 'l', 'o', 'h', 'p'),
('i', 't', 'i', 'f', 'w', 'f'),
('g', 'n', 'r', 'k', 'q', 'u'),
('h', 'g', 'u', 'a', 'l', 'l'),
('t', 'c', 'v', 'g', 't', 'l'),
('D', 'R', 'A', 'W', 'c', 's')]
Found: full:
[('l', 'd', 'l', 'o', 'h', 'p'),
('i', 't', 'i', 'f', 'w', 'F'),
('g', 'n', 'r', 'k', 'q', 'U'),
('h', 'g', 'u', 'a', 'l', 'L'),
('t', 'c', 'v', 'g', 't', 'L'),
('D', 'R', 'A', 'W', 'c', 's')]
Found: hold:
[('l', 'D', 'L', 'O', 'H', 'p'),
('i', 't', 'i', 'f', 'w', 'F'),
('g', 'n', 'r', 'k', 'q', 'U'),
('h', 'g', 'u', 'a', 'l', 'L'),
('t', 'c', 'v', 'g', 't', 'L'),
('D', 'R', 'A', 'W', 'c', 's')]
Found: laugh:
[('l', 'D', 'L', 'O', 'H', 'p'),
('i', 't', 'i', 'f', 'w', 'F'),
('g', 'n', 'r', 'k', 'q', 'U'),
('H', 'G', 'U', 'A', 'L', 'L'),
('t', 'c', 'v', 'g', 't', 'L'),
('D', 'R', 'A', 'W', 'c', 's')]
No light - check diagonally
No start - check diagonally
No all - check diagonally
No kid - check diagonally
Upvotes: 1
Reputation: 175
For horizontal you can use this:
>>> for i in range(len(l)):# l is the list
temp = ''.join(l[i])
x = temp.find(word) #word is the user input eg. draw
y = len(word)
if x != -1:
for j in range(y):
l[i][j]=l[i][j].capitalize()
I'll try to make for vertical and diagonal as well
Upvotes: 1
Reputation: 445
this one:
l = [['l', 'd', 'l', 'o', 'h', 'p'],
['i', 't', 'i', 'f', 'w', 'f'],
['g', 'n', 'r', 'k', 'q', 'u'],
['h', 'g', 'u', 'a', 'l', 'l'],
['t', 'c', 'v', 'g', 't', 'l'],
['d', 'r', 'a', 'w', 'c', 's']]
word = input("Please enter a word: ")
for i in range(len(l)):
for j in range(len(l[i])):
if l[i][j] in word:
l[i][j]=l[i][j].capitalize()
print(l)
outs:
Please enter a word: hello
[['L', 'd', 'L', 'O', 'H', 'p'], ['i', 't', 'i', 'f', 'w', 'f'], ['g', 'n', 'r', 'k', 'q', 'u'], ['H', 'g', 'u', 'a', 'L', 'L'], ['t', 'c', 'v', 'g', 't', 'L'], ['d', 'r', 'a', 'w', 'c', 's']]
Upvotes: 0