How do you find out if 2x2 square Is in 2d list in Python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do you find out if 2x2 square Is in 2d list in Python?

Hi, I have a small problem. I need to build a function, that would tell me if 2x2 square is in the entered 2d list (returns True or False). Let's say, that the 2x2 square would be word "help". It doesn't matter on the order of the letters. For example 2d list: [ ['x', 'e', 'h'] ['x', 'l', 'p'] ['x', 'x', 'x'] ] Returns True But you can have also 2d list like this: [ ['p', 'e', 'h'] ['x', 'l', 'p'] ['x', 'x', 'l'] ] Returns True aswell But 2d list like this: [ ['x', 'e', 'x'] ['x', 'p', 'x'] ['l', 'x', 'h'] ] Returns False Also it doesn't matter if the list is 3x3 or 50x70, i want the program to work with every possible 2d list. I will appreciate all your ideas. And also sorry for my bad english.

26th Oct 2022, 9:53 PM
CallMeJohny
6 Answers
+ 1
Okay so here's the live tester, https://www.sololearn.com/compiler-playground/cX41gjIKBEBq if you want to take a look.
27th Oct 2022, 2:49 AM
morl
morl - avatar
0
So some rough logic but perhaps something along this logic hasSquare(n,nVals,arr) for example. you can then iterate through arr based on n grabbing the rows and columns, checking to make sure it index is within the range of the array of course. grab a nxn column as the raw data and compare it to nVals, returning True as soon as it finds a match, or False if no match is found once it's done processing the arr.
26th Oct 2022, 10:54 PM
morl
morl - avatar
0
I actually don't understand your solution well 😅 i sent you a msg si we do not spam this comm section
26th Oct 2022, 11:12 PM
CallMeJohny
0
Messages don't work so well on desktop, or at all. I'll try to set this up as a public code bit you can run on my profile.
26th Oct 2022, 11:26 PM
morl
morl - avatar
0
Or you can add me on discord And send it there: CallMeJohny#0030 It's up to you
26th Oct 2022, 11:30 PM
CallMeJohny
0
CallMeJohny you would be doing a rolling window view on the array and comparing the sorted flattened view with your sorted flattened target array. https://code.sololearn.com/c8N5a0sCM0YO/?ref=app
27th Oct 2022, 3:36 AM
Bob_Li
Bob_Li - avatar