I have 4 nested loops that used based on the case given. The code is quite similar but there is a little difference in the comparison. Please check this code:
If Case 1:
for (i = 0; i < length; i++) {
for (j = 0; j < length; j++) {
if (array[i][j] == array[i][j+1])
// do something
}
}
If Case 2:
for (i = 0; i < length; i++) {
for (j = 4; j > 0; j--) {
if (array[i][j] == array[i][j-1])
// do something
}
}
If Case 3:
for (i = 0; i < length; i++) {
for (j = 0; j < length; j++) {
if (array[i][j] == array[i+1][j])
// do something
}
}
If Case 4:
for (i = 4; i > 0; i--) {
for (j = 0; j < length; j++) {
if (array[i][j] == array[i-1][j])
// do something
}
}
As you can see that the decrement and increment are different too. It is quite hard for me to figure out how to refactor this. They have similar code behavior (duplication) but they have different conditions in the for and if statement on each case.
Related
I'm trying to write a simple simplex solver for linear optimization problems, but I'm having trouble getting it working. Every time I run it I get a vector subscript out of range (which is quite easy to find), but I think that its probably a core issue somewhere else in my impl.
Here is my simplex solver impl:
bool pivot(vector<vector<double>>& tableau, int row, int col) {
int n = tableau.size();
int m = tableau[0].size();
double pivot_element = tableau[row][col];
if (pivot_element == 0) return false;
for (int j = 0; j < m; j++) {
tableau[row][j] /= pivot_element;
}
for (int i = 0; i < n; i++) {
if (i != row) {
double ratio = tableau[i][col];
for (int j = 0; j < m; j++) {
tableau[i][j] -= ratio * tableau[row][j];
}
}
}
return true;
}
int simplex(vector<vector<double>>& tableau, vector<double>& basic, vector<double>& non_basic) {
int n = tableau.size() - 1;
int m = tableau[0].size() - 1;
while (true) {
int col = -1;
for (int j = 0; j < m; j++) {
if (tableau[n][j] > 0) {
col = j;
break;
}
}
if (col == -1) break;
int row = -1;
double min_ratio = numeric_limits<double>::infinity();
for (int i = 0; i < n; i++) {
if (tableau[i][col] > 0) {
double ratio = tableau[i][m] / tableau[i][col];
if (ratio < min_ratio) {
row = i;
min_ratio = ratio;
}
}
}
if (row == -1) return -1;
if (!pivot(tableau, row, col)) return -1;
double temp = basic[row];
basic[row] = non_basic[col];
non_basic[col] = temp;
}
return 1;
}
Is declaring a new intger inside a loop changes the space complexity of the metohd?
for exampe if i'm looking at the follwoing 2 methods, is both of the methods space complexity is O(1)? or in the first method becuase I'm declaring the variable c over and over until the loop end it's space complexity is O(n)?
public static int what (int []a) {
int temp = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i; j < a.length; j++) {
**int c = f(a, i, j);**
if (c % 2 == 0) {
if (j - i + 1 > temp)
temp = j - i + 1;
}
}
}
return temp;
}
public static int what (int []a) {
int temp = 0;
**int c;**
for (int i = 0; i < a.length; i++) {
for (int j = i; j < a.length; j++) {
**c = f(a, i, j);**
if (c % 2 == 0) {
if (j - i + 1 > temp)
temp = j - i + 1;
}
}
}
return temp;
}
Not sure if it's relevant to the question but also attahced the f method.
private static int f (int[]a, int low, int high)
{
int res = 0;
for (int i=low; i<=high; i++)
res += a[i];
return res;
}
When you declare a variable inside the for loop it goes out of scope when the iteration ends and gets re declared in the next iteration so you are not declaring n variables, you are declaring a variable n times
I am trying to understand some concepts of performance in code.
If there is, for instance, a 2d boolean array with 5 trues inside it,
if we want to change all the elements in the array to false, which code would be faster?
number1:
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[0].length; j++) {
arr[i][j] = false;
}
}
number2:
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[0].length; j++) {
if (arr[i][j])
arr[i][j] = false;
}
}
You can measure it by using console.time() and console.timeEnd():
Number 1:
console.time('loop');
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[0].length; j++) {
arr[i][j] = false;
}
}
console.timeEnd('loop');
loop: 0.046142578125ms
Number 2:
console.time('loop');
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[0].length; j++) {
if (arr[i][j])
arr[i][j] = false;
}
}
console.timeEnd('loop');
loop: 0.031982421875ms
The array I used was arr[1][135] big and had 5 values in the second array that were true.
As you can see the loop with the if clause is faster.
The question I'm trying to solve is...
What is the value returned by the following function? Express your answer as a function of n.
int v = 0;
int n = 100;
for (int i = 1; i <= n ; i++)
{
for( int j = n + 1; j < 2 * n; j++)
{
v = v + 1;
}
}
System.out.println(v);
Seems like I'm missing something but I don't know what. =/ Thank you.
I don't want to give the whole answer away.
First of, our function looks like this.
int v = 0;
for (int i = 1; i <= n; i++) {
for (int j = (n + 1); j <= (n*2); j++) {
v++;
}
}
System.out.println(v);
So, depending on n what will v give us?
Let's try it, let's try for n = 1 to 20
for (int n = 1; n < 20; n++) {
int v = 0;
for (int i = 1; i <= n; i++) {
for (int j = (n + 1); j <= (n*2); j++) {
v++;
}
}
System.out.println(v);
}
Try that! If you still are curious give a poke here.
Hint #2. It is a very specific function, a very common.
I'm not quite getting pseudocode FOR loop thing.
What would be the correct code (in any language) for this pseudocode?
function myFunction(arr[])
for i = 0 to length(arr)
if (arr[i] > i) then
j = i
while (j < length(arr)) and (arr[j] >= j)
j = j + 1
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
i = 0
Here it is, in C# (or Java, if you decapitalize "Length"):
void myFunction(int[] arr)
{
for(int i = 0; i < arr.Length; i++)
{
if(arr[i] > i)
{
int j = i;
while(j < arr.Length && arr[j] >= j)
j = j + 1;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i = 0;
}
}
}
Using while instead of for:
void myFunction(int[] arr)
{
int i = 0;
while(i < arr.Length)
{
if(arr[i] > i)
{
int j = i;
while(j < arr.Length && arr[j] >= j)
j = j + 1;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i = 0;
}
i++;
}
}
Note the indentation structure of the pseudocode:
function myFunction(arr[])
for i = 0 to length(arr)
if (arr[i] > i) then
...
This means that everything underneath the for declaration is inside the loop, and everything underneath the if, including setting i to 0, will be inside the conditional block. Given this fact, it must be the case that i will start again at 1 if the if statement is entered. What would happen if the code were as below?
void myFunction(int[] arr)
{
int i = 0;
while(i < arr.Length)
{
if(arr[i] > i)
{
int j = i;
while(j < arr.Length && arr[j] >= j)
j = j + 1;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
i = 0;
}
}
}
In the case arr[i] <= i, the loop never terminates.
You should always assume the last operation of an iterative loop is the shifting of the index, unless explicitly stated otherwise. This is how a for loop must behave, and how a while implementation ought to (by convention).
The code itself looks to be an attempt at a bubble sort implementation, but is sorting based on comparison against the index, rather than the other elements. I am not sure what it is meant to achieve.