How to create a for loop that goes through all diagonal possibilities of a list?
You can transform the list by shifting each of the string as list by 1 more than the last - and fill the created space with filler (Using '0'
in this case):
mearray = np.array([[e for e in g] for g in grid])
words = ["HORSE","COW","DOG","ET"] # don't ask
I used numpy
because I'm more used to it and it's easier to show here, but it could certainly be done in regular list comprehension. After this, your array is now a numpy array:
[['H' 'X' 'X' 'W' 'X' 'X' 'X' 'X' 'X' 'D']
['X' 'O' 'X' 'X' 'O' 'X' 'X' 'X' 'O' 'X']
['X' 'X' 'R' 'X' 'X' 'C' 'X' 'G' 'X' 'X']
['X' 'X' 'X' 'S' 'X' 'X' 'X' 'X' 'X' 'T']
['X' 'X' 'X' 'X' 'E' 'X' 'X' 'X' 'E' 'X']]
Transform this by adding in shifting fillers:
leng = len(mearray)
def pad_with(vector, pad_width, iaxis, kwargs):
pad_value = kwargs.get('padder', '0')
vector[:pad_width[0]] = pad_value
vector[-pad_width[1]:] = pad_value
return vector
np.array([np.pad(mearray[i], (leng-i, i+1), pad_with) for i in range(leng)])
Your array is now:
[['0' '0' '0' '0' '0' 'H' 'X' 'X' 'W' 'X' 'X' 'X' 'X' 'X' 'D' '0']
['0' '0' '0' '0' 'X' 'O' 'X' 'X' 'O' 'X' 'X' 'X' 'O' 'X' '0' '0']
['0' '0' '0' 'X' 'X' 'R' 'X' 'X' 'C' 'X' 'G' 'X' 'X' '0' '0' '0']
['0' '0' 'X' 'X' 'X' 'S' 'X' 'X' 'X' 'X' 'X' 'T' '0' '0' '0' '0']
['0' 'X' 'X' 'X' 'X' 'E' 'X' 'X' 'X' 'E' 'X' '0' '0' '0' '0' '0']]
You can clearly see that HORSE
and COW
are normalized. You'll need to do this again by switching the filler direction so you'll have GOD
and ET
:
Reverse direction: np.array([np.pad(mearray[i], (i+1, leng-i), pad_with) for i in range(leng)])
[['0' 'H' 'X' 'X' 'W' 'X' 'X' 'X' 'X' 'X' 'D' '0' '0' '0' '0' '0']
['0' '0' 'X' 'O' 'X' 'X' 'O' 'X' 'X' 'X' 'O' 'X' '0' '0' '0' '0']
['0' '0' '0' 'X' 'X' 'R' 'X' 'X' 'C' 'X' 'G' 'X' 'X' '0' '0' '0']
['0' '0' '0' '0' 'X' 'X' 'X' 'S' 'X' 'X' 'X' 'X' 'X' 'T' '0' '0']
['0' '0' '0' '0' '0' 'X' 'X' 'X' 'X' 'E' 'X' 'X' 'X' 'E' 'X' '0']]
Now you can see GOD
and ET
(upside down) in your matrix. You should be able to use your original function to retrieve them.
How can I go through every diagonal of a 2D array in C#?
generic solution
public static IList<IList<T>> GetSecondaryDiagonals<T>(this T[,] array2d)
{
int rows = array2d.GetLength(0);
int columns = array2d.GetLength(1);
var result = new List<IList<T>>();
// number of secondary diagonals
int d = rows + columns - 1;
int r, c;
// go through each diagonal
for (int i = 0; i < d; i++)
{
// row to start
if (i < columns)
r = 0;
else
r = i - columns + 1;
// column to start
if (i < columns)
c = i;
else
c = columns - 1;
// items from diagonal
var diagonalItems = new List<T>();
do
{
diagonalItems.Add(array2d[r, c]);
r++;
c--;
}
while (r < rows && c >= 0);
result.Add(diagonalItems);
}
return result;
}
example of usage
private static void Main()
{
var T1 = new char[,] // more rows than columns
{
{'a', 'b', 'd'},
{'c', 'e', 'g'},
{'f', 'h', 'j'},
{'i', 'k', 'l'},
};
var T2 = new int[,] // more columns than rows
{
{1, 2, 4, 7},
{3, 5, 8, 0},
{6, 9, 1, 2},
};
Print(T1.GetSecondaryDiagonals());
Print(T2.GetSecondaryDiagonals());
Console.ReadKey();
}
static void Print<T> (IList<IList<T>> list)
{
Console.WriteLine();
foreach (var sublist in list)
{
foreach (var item in sublist)
{
Console.Write(item);
Console.Write(' ');
}
Console.WriteLine();
}
}
output
a
b c
d e f
g h i
j k
l
1
2 3
4 5 6
7 8 9
0 1
2
looping through a 2D array (diagonal) c++
You can of course check in each direction, e.g.
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
if (check_north_east(array, i, j))
++count;
if (check_north_west(array, i, j))
++count;
if (check_south_east(array, i, j))
++count;
if (check_south_west(array, i, j))
++count;
}
}
The compiler will happily go beyond the array bounds. So you must make sure, the code won't do it, and check yourself
const int NROWS = 8, NCOLS = 8;
bool check_north_east(char array[][NCOLS], int row, int col)
{
if (row <= 0 || col >= NCOLS - 1)
return false;
return array[row - 1][col + 1] == 'x';
}
Loop through main diagonal (/) as well as the cells underneath it in a 2D array
r
iterates over row indexes.i
iterates over column indexes.
As the row index goes up, the column index should go down.
Because,
as you go down, you want to paint wider and wider sections.
As wide as r + 1
.
I think the logic will be easier to understand if the column indexes go from 0 until r
, and you paint the cells from right to left.
int min = Math.min(colorArray.length, colorArray[0].length);
for (int r = 0; r < min; r++) {
char[] row = colorArray[r + Math.max(0, colorArray.length - min)];
for (int i = 0; i <= r; i++) {
row[row.length - 1 - i] = true;
gui.update(colorArray);
}
}
Related Topics
Python Overflowerror: Int Too Large to Convert to Float
How to Connect to a Remote Windows Machine to Execute Commands Using Python
How to Get a Fields Particular Value of Json in Python
Pandas - Drop Last Column of Dataframe
Python: Getting Around Division by Zero
How to Assign and Use Column Headers in Spark
Access Is Denied When Trying to Pip Install a Package on Windows
Pip Installation /Usr/Local/Opt/Python/Bin/Python2.7: Bad Interpreter: No Such File or Directory
Remove Very First Row in Pandas
How to Remove a Single Quotes from a List
Pip Install Pandas: Installing Dependencies Error
Conditional Row Read of CSV in Pandas
Python Serial: How to Use the Read or Readline Function to Read More Than 1 Character At a Time
What Is the Easiest Way to Convert Ndarray into Cv::Mat