FInding index of values - list

I have a list of lists, [[-1,-1,1,1,1],[-1,-1,1,1,1]] is there some way to have the program return the indices that equal -1? Therefore when I put that list into the comprehension, it would return that the value = -1 at the indices [0][0], [0][1], [1][0], [1][1].

Track the indices with both indices of a nested for-loop: i and j
Track the occurance of the -1 with an if conditional, and return i and j.
Spoiler alert in Ruby, not sure your language:
twoDeeArr = [[-1,-1,1,1,1],[-1,-1,1,1,1]]
i = 0
while i < twoDeeArr.length do
j = 0
while j < twoDeeArr[i].length do
if twoDeeArr[i][j] == -1
puts "Danger Will Robinson: at #{i},#{j}"
end
j+= 1
end
i+= 1
end
### RETURNS
Danger Will Robinson: at 0,0
Danger Will Robinson: at 0,1
Danger Will Robinson: at 1,0
Danger Will Robinson: at 1,1

Related

C++ Variable mysteriously changed after calling unrelated function

I have the following code for a board game program that I am making. The function "copyMap" simply copies the contents of the first array map to the second array map_copy. In the function "solver", I have a loop that iterates over the game grid, and for each cell, call copyMap.
The issue is, when solver is called, it runs for 1 iteration then promptly crashes the program. Moreover, the iterators, x_pos and y_pos, which are supposed to start at 0 and 0, gets changed to 2 and 5 after calling copyMap and before the program crashes.
void copyMap(int map[][WIDTH], int map_copy[][WIDTH], int rows_to_copy) {
// copy the contents of the first rows_to_copy rows of map to map_copy
for( int i = 0 ; i < rows_to_copy ; i++ ) {
for( int j = 0 ; j < WIDTH ; j++ ) {
map_copy[i][j] = map[i][j];
cout<<i<<" "<<j<<endl;
}
}
}
int solver(int map[][WIDTH]) {
int map_copy[HEIGHT][WIDTH];
for(int x_pos = 0 ; x_pos < HEIGHT ; x_pos++ ) {
for(int y_pos = 0 ; y_pos < WIDTH ; y_pos++ ) {
copyMap(map, map_copy, MAX_ROWS);
cout<<x_pos<<" "<<y_pos<<endl;
}
}
}
This is the console output: (For the first iteration of the loop)
0 0
0 1
0 2
...
81 8
2 5
The game grid has 81 rows and 9 columns, so copyMap prints 0 0, 0 1, ..., 81 8.
Then, solver prints 2 5. However, the iterators are still supposed to be 0 0.
The iterators are not passed to the function copyMap at all. Why are their values being changed, and why is my program crashing?
I would appreciate any help on this issue, thank you.
Copymap iterates through each element. I don't think you need to call it inside a nested loop inside solver. You should take it outside of the loop and set a breakpoint inside the loop to watch x_pos and y_pos.
Solved, turns out map has 81 rows and 9 columns but map_copy only has 9 rows and 9 columns, so copyMap tried to access out-of-bounds memory locations.

How to resize 2d vector

#include <iostream>
#include <vector>
using namespace std;
int main(void)
{
vector<vector<int> > matrix;
matrix.resize(3, vector<int>(4, 1));
for (size_t i = 0; i < 3; i++)
{
for (size_t j = 0; j < 4; j++)
{
cout << matrix[i][j];
}
cout << endl;
}
matrix.resize(5, vector<int>(7, 0));
for (size_t i = 0; i < 5; i++)
{
for (size_t j = 0; j < 7; j++)
{
cout << matrix[i][j];
}
cout << endl;
}
return 0;
}
'''
As far as I know, when we are resizing a vector using "resize()" over than original capacity, values in original space will remain and new values are assigned to new space.
In the line matrix.resize(5, vector(7, 0)); If we execute that line I thought matrix would be like
1111000
1111000
1111000
0000000
0000000
something like this.
But the programs stops,
I want to know why it won't working.
matrix.resize(5, vector<int>(7, 0));
only add new vector of size 7 not modifying actual vector.
Just resize actual vectors to 7 with:
for (auto &row: matrix) row.resize(7);
so now is working:
1111000
1111000
1111000
0000000
0000000
i tested your codes using an online compiler https://onlinegdb.com/BkwcGuAAD
your columns are not resized (only the rows are resized). running your current code yields
1 1 1 1 0 0 33
1 1 1 1 0 0 33
1 1 1 1 0 0 49 << notice some columns have random numbers?
0 0 0 0 0 0 0
0 0 0 0 0 0 0
try resizing the columns too
matrix[0].resize(7,0);
matrix[1].resize(7,0);
matrix[2].resize(7,0);
matrix.resize(5, vector<int>(7, 0));
you should get something like the following
1 1 1 1 0 0 0
1 1 1 1 0 0 0
1 1 1 1 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
By the C++ Reference
(http://www.cplusplus.com/reference/vector/vector/resize/)
void resize (size_type n);
void resize (size_type n, const value_type& val);
Resizes the container so that it contains n elements.
If n is smaller than the current container size, the content is reduced to its first n elements, removing those beyond (and destroying them).
If n is greater than the current container size, the content is expanded by inserting at the end as many elements as needed to reach a size of n. If val is specified, the new elements are initialized as copies of val, otherwise, they are value-initialized.
If n is also greater than the current container capacity, an automatic reallocation of the allocated storage space takes place.
Notice that this function changes the actual content of the container by inserting or erasing elements from it.
I thought Compiler will understand if I put more longer vector inside of "val".
That is, I thought it would understand this kind of increased "n".
But Compiler will only watch whether "n" parameter itself is changed or not.
Because of this reason, my code wouldn't work properly.
if you want to increase size of vector using size() function, note that you should resize original row values on your hand.

Ordering an array based on 2D array of relations (higher, lower, doesn't matter)

I have been stuck with this problem for two days and I still can't get it right.
Basically, I have a 2D array with relations between certain numbers (in given range):
0 = the order doesn't matter
1 = the first number (number in left column) should be first
2 = the second number (number in upper row) should be first
So, I have some 2D array, for example this:
0 1 2 3 4 5 6
0 0 0 1 0 0 0 2
1 0 0 2 0 0 0 0
2 2 1 0 0 1 0 0
3 0 0 0 0 0 0 0
4 0 0 2 0 0 0 0
5 0 0 0 0 0 0 0
6 1 0 0 0 0 0 0
And my goal is to create a new array of given numbers (0 - 6) in such a way that it is following the rules from the 2D array (e.g. 0 is before 2 but it is after 6). I probably also have to check if such array exists and then create the array. And get something like this:
6 0 2 1 4 5
My Code
(It doesn't really matter, but I prefer c++)
So far I tried to start with ordered array 0123456 and then swap elements according to the table (but that obviously can't work). I also tried inserting the number in front of the other number according to the table, but it doesn't seem to work either.
// My code example
// I have:
// relArr[n][n] - array of relations
// resArr = {1, 2, ... , n} - result array
for (int i = 0; i < n; i++) {
for (int x = 0; x < n; x++) {
if (relArr[i][x] == 1) {
// Finding indexes of first (i) and second (x) number
int iI = 0;
int iX = 0;
while (resArr[iX] != x)
iX++;
while (resArr[iI] != i)
iI++;
// Placing the (i) before (x) and shifting array
int tmp, insert = iX+1;
if (iX < iI) {
tmp = resArr[iX];
resArr[iX] = resArr[iI];
while (insert < iI+1) {
int tt = resArr[insert];
resArr[insert] = tmp;
tmp = tt;
insert++;
}
}
} else if (relArr[i][x] == 2) {
int iI = 0;
int iX = 0;
while (resArr[iX] != x)
iX++;
while (resArr[iI] != i)
iI++;
int tmp, insert = iX-1;
if (iX > iI) {
tmp = resArr[iX];
resArr[iX] = resArr[iI];
while (insert > iI-1) {
int tt = resArr[insert];
resArr[insert] = tmp;
tmp = tt;
insert--;
}
}
}
}
}
I probably miss correct way how to check whether or not it is possible to create the array. Feel free to use vectors if you prefer them.
Thanks in advance for your help.
You seem to be re-ordering the output at the same time as you're reading the input. I think you should parse the input into a set of rules, process the rules a bit, then re-order the output at the end.
What are the constraints of the problem? If the input says that 0 goes before 1:
| 0 1
--+----
0 | 1
1 |
does it also guarantee that it will say that 1 comes after 0?
| 0 1
--+----
0 |
1 | 2
If so you can forget about the 2s and look only at the 1s:
| 0 1 2 3 4 5 6
--+--------------
0 | 1
1 |
2 | 1 1
3 |
4 |
5 |
6 | 1
From reading the input I would store a list of rules. I'd use std::vector<std::pair<int,int>> for this. It has the nice feature that yourPair.first comes before yourPair.second :)
0 before 2
2 before 1
2 before 4
6 before 0
You can discard any rules where the second value is never the first value of a different rule.
0 before 2
6 before 0
This list would then need to be sorted so that "... before x" and "x before ..." are guaranteed to be in that order.
6 before 0
0 before 2
Then move 6, 0, and 2 to the front of the list 0123456, giving you 6021345.
Does that help?
Thanks for the suggestion.
As suggested, only ones 1 are important in 2D array. I used them to create vector of directed edges and then I implemented Topological Sort. I decide to use this Topological Sorting Algorithm. It is basically Topological Sort, but it also checks for the cycle.
This successfully solved my problem.

How to find index of first and last column and first and last row which does not contain only zeros or only ones inside?

I have matrix of 0 and 1 packed inside vector<int> filled by rows (first row, second row and so on..). How to find index of first and last column and first and last row which does not contain only zeros or only ones inside ?
I have iterated four times with loop and compare but is there faster and more elegant way to do this ?
for example result here is columns with indexes 1 and 4 and rows with 1 and 4.
0 0 0 0 0
0 0 1 0 0
0 1 0 0 1
0 0 0 0 0
0 0 0 1 0
I would say, you just need two iterations: One iteration for rows and one iteration for columns.
If I understand the question right, this coding should show how it could be coded (just a draft):
int firstRow = -1;
int lastRow = -1;
for (row=0; row<numRows; row++) {
int cnt = 0;
for (col=0; col<numCols; col++) {
cnt += vec[row][col];
}
if (cnt != 0 && cnt != numCols) {
if (firstRow == -1) firstRow = row;
lastRow = row;
}
}
// output firstRow, lastRow
Do the same for columns.
This coding is not necessarily faster than four iterations. When four iterations are made from the start and the end, they are faster if the columns are nearer to the limits. But it saves you a little coding.

Transform an upper triangular matrix into a full matrix C++

How would look the code that can transform an upper triangular matrix into a full matrix.
The matrix is in a vector, not in a bidimensional array...
so the array
[ 1 2 3 4
0 5 6 7
0 0 8 9
0 0 0 10 ]
would become an array like:
[ 1 2 3 4
2 5 6 7
3 6 8 9
4 7 9 10 ]
could you provide some ideas, I was thinking in applying a kind of module or something...
There is one restriction, I am not using bidimensional arrays
I am usng a vector, so is a unidimensional array
First, you must understand the fundemtnal nature of a reflected matrix. For any i, j, the following assertion is true:
m[i][j] ≡ m[j][i]
So, you need some algorithm to make that true. May I suggest:
for(int i = 0; i < HEIGHT; ++i)
for(int j = 0; j < i; ++j)
m[i][j] = m[j][i];
Note the condition of the 2nd loop. By ensuring that j is always less than i, we restrict our activity to the bottom-left triangle.
Next, you must understand how you have implemented a two-dimensional matrix in a one-dimensional array. It appears that you have established the identity:
m[i][j] ≡ v[i*WIDTH+j]
Substituting, we have:
for(int i = 0; i < HEIGHT; ++i)
for(int j = 0; j < i; ++j)
v[i*WIDTH+j] = v[j*WIDTH+i];