Breaking up assertions - or not - assert

Is there a consensus about which of the following alternatives to go with (here exemplified in the C language)?
One assertion for all parameters:
int f(int m, int n)
{
assert((m >= 0) && (m <= mmax) && (n >= 0) && (n <= nmax));
...
}
One assertion per parameter:
int f(int m, int n)
{
assert((m >= 0) && (m <= mmax));
assert((n >= 0) && (n <= nmax));
...
}
Assertions with atomic conditions:
int f(int m, int n)
{
assert(m >= 0);
assert(m <= mmax);
assert(n >= 0);
assert(n <= nmax);
...
}

I personally prefer the third one, and not just for readability, but for future mainainability and debugging. Imagine that one of the assertions suddenly starts failing some time after the code has been written. With either of the first two, you don't know exactly which condition is false, when one of those assertions fails.
But with the third one, there is absolutely no ambiguity.

Related

How to minimize the times a recursive function is called

So for the following code I am trying to reduce the amount of time the function call itself so that it is more efficient. The purpose of the code is to perform exponentiation using recursion.
int expo(const int m, const unsigned int n)
{
funcCallCounter++; //counts how many times the function is called
if (n == 0)//base case
{
return 1;
}
else if (n % 2 == 0)// for even numbers
return expo(m*m, n / 2);
else
return m * expo(m, n - 1);//for odd numbers
}
Well this is my favourite approach for the recursive expo which will always give less calls than your approach
int expo(int a, int n) {
funcCallCounter++;
if (n == 0) {
return 1;
}
int r = expo(a, n / 2);
if (n % 2 == 0) {
//Even n
return r * r;
}
else {
// Odd n
return a *r*r;
}
}
You could use shifts to make your execution faster.
n % 2 can be replaced with n & 0x01
n / 2^k can be replaced with n >> k
A division is about 20 cycles while a shift is only 1-2 cycles.
However, maybe the compiler see taht by itself and make this optimisation already.
Best

CPLEX ILOG OPL Optimization

The model is optimizing the costs of Machines in Cell layout design
regarding the duplication and subcontracting.
Mod Const. is,
forall (k in 1..Cells, i in 1..nbMachines, j in 1..nbComps)
{
if (U[i][j][k] == 1 && A[k][i] < ((D[k][j]*S[k][j])*52))
DN[i][j][k] == 1;
SC[i][j][k] == 0;
INT[i][j][k] == 0;
}
forall (k in 1..Cells, i in 1..nbMachines, j in 1..nbComps)
{
if (V[i][j][k] == 1 && A[k][i] >= ((D[k][j]*S[k][j])*52))
DN[i][j][k] == 0;
SC[i][j][k] == 1;
INT[i][j][k] == 1;}
U , V are extracted in previous steps, A, D, S are input data.
The variables reqd. are DN, SC and INT.
Errors are those expressions are cannot be extracted, U, V are unbounded,
Please help in this regard,
Since U and V are decision variables, you should not write:
if (U[i][j][k] == 1 && A[k][i] < ((D[k][j]*S[k][j])*52))
DN[i][j][k] == 1;
Instead write:
((U[i][j][k] == 1) && (A[k][i] <= -1+((D[k][j]*S[k][j])*52)))
=> (DN[i][j][k] == 1);

C++ recursive code to iterative

Hi guys so I have this code that I need for a project in university.The code I have is recursive and it is taking a lot of time to load.I tried converting it to iterative but I can't seem to do it in order as the output is different.Could you please help me converting it?Thank you.
void getBrewCount(int n, int i, int j){
if( i ==n && j == n){ //reach the (n,n) point
count++;
}else if( i > n || j > n){//wrong way
return;
}else {
if(i==8 && j==5){
j++;
}
if(i==11 && j==5){
j+=2;
}
if(i==15 && j>=14){
i+=2;
}
if(i==21 && j>=22){
i++;
}
getBrewCount(n, i +1, j );
getBrewCount(n, i , j +1);
}
In recursion you have an ending condition. When this condition is met, recursion ends and you "bubble up" from recursion. When you want to do the same iteratively, ending condition of recursion should be condition of your loop. Your recursion ends after it goes out of range. You can write while() loop which will loop until it reaches out of range, therefore while (i < n && j < n) { }. My while loop increments count and breaks itself right when range is achieved. However I do not know what is the real functionality of your function, so you might want to change it a little.
void getBrewCount(int n, int i, int j) {
while (i < n && j < n) {
// my ending condition
if (i == n && j == n) {
++count;
break;
}
/* code that updates i, j e.g.
if (i == 10)
++j;
*/
}
return;
}

Longest Common Increasing Subsequence Dynamic Programming

I'm working on finding a solution to the longest common increasing sub sequence problem. Here's a link if you're not familiar with it. LCIS
The problem can basically be reduced to two different problems. 'Longest common subsequence' and 'Longest increasing subsequence'. Here's the recursive solution to Longest common subsequence:
LCS(S,n,T,m)
{
if (n==0 || m==0) return 0;
if (S[n] == T[m]) result = 1 + LCS(S,n-1,T,m-1); // no harm in matching up
else result = max( LCS(S,n-1,T,m), LCS(S,n,T,m-1) );
return result;
}
Based on that and the general recursive formula found here I've been trying to implement the algorithm so I can use dynamic programming.
int lcis(int S[4], int n, int T[4], int m, int prev)
{
int result;
if (n == 0 || m == 0)
return 1;
if (S[n] == T[m] && S[n] > S[prev]){
result = myMax(1 + lcis(S, n-1, T, m-1, n), lcis(S, n-1, T, m, prev),
lcis(S, n, T, m-1, prev)) ;
}
else
result = max(lcis(S,n-1,T,m, prev), lcis(S,n,T,m-1, prev));
return result;
}
Obviously this does not give the correct solution. Any help would be appreciated.
For example, if I give it the two sequences {1, 2, 4, 5} and {12, 1, 2, 4} I get an output of 2. The correct output here would be 3, for the sub sequence {1,2,4}
EDIT:
Here is some revised code, after suggestions from below. Still not 100% correct. But closer. Note I am now using vectors, but this shouldn't change anything.
int lcis(vector<int> S, int n, vector<int> T, int m, int size)
{
int result;
if (n < 0 || m < 0)
return 0;
if (S[n] == T[m] && (n == size - 1 || S[n] < S[n + 1] )){
result = myMax(1 + lcis(S, n - 1, T, m - 1, size), lcis(S, n - 1, T, m, size),
lcis(S, n, T, m - 1, size));
}
else
result = max(lcis(S, n-1, T, m, size), lcis(S, n, T, m-1, size));
return result;
}
Remember that you are going backward through the array. So this test
S[n] > S[prev]
Should be the other way around:
S[n] < S[prev]
I am not sure why you need prev at all, as it should always be n+1, so maybe use
if (S[n] == T[m] && n < 3 && S[n] < S[n+1])
If you need to make it work for any size, then either pass the size in, or just a flag to say don't check n+1
Edit:
My mistake - you want to be going through the first if case when n == 3 (or size), as you are at the start of a (potentially) increasing subsequence.
The if test should be
if (S[n] == T[m] && (n == 3 || S[n] < S[n+1]))
Note that this if test:
if (n == 0 || m == 0)
return 1;
is ignoring the first element of either sequence (and assuming it is at the end of an increasing subsequence). What is needed is to stop the recursion when you have gone before the start of either sequence. You also know that when you have gone before the start of the sequence, you can't possibly be in a subsequence, so you can return 0 for the length of the subsequence. So the test should be
if (n < 0 || m < 0)
return 0;

Substring comparision with one mismatch

The following is code for comparing a String A and String B with atmost one mismatch
i.e.,
ABC is same as ABX or AXC or XBC but, not same as AXZ
I did check several cases but, the website says it provides wrong answer. Could someone help to figure out where does this code fail?
Also, I'd be glad if someone could provide a better algorithm for the same problem.
TY
int compare(string a, int pos, string b) {
int count = 0;
int length = b.length()-1;
int mid = b.length() /2;
if(pos+length >= a.length())
return 0;
for(int i=0,j=pos;i<=mid;i++,j++) {
if(i == mid) {
if(a[j] != b[i])
count ++;
}
else {
if(a[j] != b[i])
count ++;
if(a[pos+length - i] != b[length -i])
count ++;
}
if(count >= 2) return 0;
}
return 1;
}
One problem is that if b.length() is even, then you compare a[pos + b.length() / 2] to b[b.length() / 2] twice: once when i == mid - 1, and once when i == mid. So something like compare("abcd", 0, "abbd") returns 0, because it counts the 'c'-vs.-'b' discrepancy as two separate mismatches.
I recommend that you simply strip out all logic related to mid. It serves no purpose other than massively complicating your code. If you iterate straight from 0 to b.length() - 1, you'll be much better off.