check at least two out of ten booleans are true - language-agnostic

In a case where at least two out of three booleans are true, this is the easiest way to find out:
BOOL a, b, c;
-(BOOL)checkAtLeastTwo
{
return a && (b || c) || (b && c);
}
What will be the optimal solution if there is ten booleans and at least two of them needs to be true? Thanks in advance.

Your original implementation is sub-optimal - you can just sum true values:
return (int)a + (int)b + (int)c >= 2;
Obviously you can extend this to 10 variables:
return (int)a + (int)b + (int)c + (int)d + (int)e +
(int)f + (int)g + (int)h + (int)i + (int)j >= 2;

In C you can just check sum of your variables
return a + b + .... + n >= 2;
If implicit conversion from boolean to integer doesn't in your language, you can simply convert your variables to integer and check sum of converted values.

Related

Why the final result varies?

When returning from a function, The following coding style does NOT seem to work -
return (int) minim(mid-l,r-mid) + (int) (mid+mid==n)?1:0;
But the following code is working fine -
int x = minim(mid-l,r-mid);
int y = (mid+mid==n)?1:0;
return x+y ;
mid, l, r, n are all integers.
Can someone please help me understand why?
You need to add parentheses as '+' takes precedence over the ternary operator '?:'
return (int) minim(mid-l,r-mid) + ((int) (mid+mid==n)?1:0);
Due to operator precedence
return (int) minim(mid-l,r-mid) + (int) (mid+mid==n)?1:0;
means
return ((int) minim(mid-l,r-mid) + (int) (mid+mid==n))?1:0;
not
return (int) minim(mid-l,r-mid) + ((int) (mid+mid==n))?1:0);

Why does the fibonacci recursive sequence work?

I am wondering why this Fibonacci recursive function works:
int fibRec(int n)
{
if ((n == 1) || (n == 0))
{
return n;
}
int i = fibRec(n - 1) + fibRec(n - 2);
return i;
}
I understand what the Fibonacci sequence is and I understand what a recursive function does and how this function is working. I'm just having troubles understanding why it works. I know that when you break it down, you are essentially adding a bunch of 0s and 1s, as this image depicts.
fibonacci recursive
But why is it that when I pass a 5 to the function and all the 0 and 1s are added that it will equal the 5th sequence number in the Fibonacci sequence? I've seen this question asked before but never really explained. The responses are all just "because recursion". Yes, I know what a recursive function is and how this one is working. But WHY does this recursive function give you the correct Fibonacci sequence number?
In the Fibonacci sequence the first two numbers are zero and one. Every number after these is the sum of the previous 2 numbers. So the first few numbers are
F(0) ≡ 0
F(1) ≡ 1
F(2) = F(1) + F(0) = 1 + 0 = 1
F(3) = F(2) + F(1) = 1 + 1 = 2
F(4) = F(3) + F(2) = 2 + 1 = 3
F(5) = F(4) + F(3) = 3 + 2 = 5
F(6) = F(5) + F(4) = 5 + 3 = 8
...
F(n) = F(n - 1) + F(n - 2) ∀ n > 1
Therefore when we calculate a Fibonacci number recursively we have to practice the following logical procedure (in pseudo-code out of respect to StackOverflow).
Integer NthFibonacci(Integer n) {
if (n < 0) {
return undefined;
} else if (n < 2) {
return n;
} else {
return NthFibonacci(n - 1) + NthFibonacci(n - 2);
}
}
I'm sure you know all this but I think it will help my explanation to have this part as a reference.
Where the Ones and Zeros Come In
The best way to explain this is probably with an example.
Imagine that, as above, we are trying to recursively calculate F(6). Try following the procedure given above. Remember that we will perform recursion only if n > 1.
First we start with F(6) = F(5) + F(4).
Then we find F(5) = F(4) + F(3).
Then we find F(4) = F(3) + F(2).
Then we find F(3) = F(2) + F(1).
Then we find F(2) = F(1) + F(0).
This is where things start to work out!
We have now gotten F(2) in terms of F(1) ≡ 1 and F(0) ≡ 0 (both of which are known), and so we are able to calculate an actual value instead of performing more recursion.
We can now find F(2) = F(1) + F(0) = 1 + 0 = 1.
NOTICE THE 1 AND 0 Those are what people are talking about when they say the whole thing comes down to ones and zeros. Every time we recurse down to find a base value we will end up finding F(2) = 1 + 0. This leads to more ones and zeros as we move back up our recursion tree being able to calculate higher and higher values, as follows.
F(3) = F(2) + F(1) = (1 + 0) + 1
F(4) = F(3) + F(2) = ((1 + 0) + 1) + (1 + 0)
F(5) = F(4) + F(3) = (((1 + 0) + 1) + (1 + 0)) + ((1 + 0) + 1)
F(6) = F(5) + F(4) = ((((1 + 0) + 1) + (1 + 0)) + ((1 + 0) + 1)) + (((1 + 0) + 1) + (1 + 0))
Now if you add up all the 1's you get a sum of 8, and so F(6) = 8, which is correct!
This is how it works, and this is how it breaks down to ones and zeros.
Remember, recursion works by breaking down the problem till we know what the answer is, and then building it up from there.
What do we know about the fibonacci sequence?
We know that when:
x = 1
and
x = 0
That that is the lowest it goes. That is an important key. Because when x = 0 we are really doing 0 + 0 and when x = 1 we are really doing 0 + 1. Now start at the top.
0,1,1,2,3,5,8,13...
If we are at 13. what is 13? Why simply 5 + 8 right? So That is where
int i = fibRec(n - 1) + fibRec(n - 2);
comes from. Because these are going to branch out lower and lower till we are at a base case for each one.
This is the recursive calling. Because now the method is going to go back to the stack and call fibRec again. You will notice that (n-1) and (n-2) are both added together and set to i. This is so that we don't lose the value. because of the + sign the stack then ends up returning more and more (n-1)s and (n-2)s until we are at the base case. I hope all of this makes sense. Thinking recursively can be very difficult. Here is a a visual representation from top to bottom of what it would look like.
In short. This just keeps adding the previous fibonacci sequences to the current one until it gets to the current loop.

Numeric function

As a sub-routine for a script I'm writing, I need a numerical function that behaves in a specific pattern. It takes a single input and provides a single output, such that between 0 and L inclusive, it is the identity function, but then between L+1 and L*2+1, it returns L to 0 respectively, and then from L*2+2 to L*3+2 it returns 0 to L respectively, and so on. I want to do this without any if statements, only using absolute value and modulus. Can anyone give me pseudocode for this function?
Given input I, limit L and result R, and using only basic arithmetic and absolute values, this gives the desired result.
R = ABS(L * ((I - (I % (L + 1))) / (L + 1) % 2) + (L + 1) * (I - (I % (L + 1))) / (L + 1) - I)
This can obviously be hugely simplified by declaring some intermediate variables, and using additional methods, e.g. floor to simulate integer division. Here's a Javascript example:
var factor = Math.floor(input / (limit + 1));
var flag = factor % 2;
var result = Math.abs(limit * flag + (limit + 1) * factor - input)

AS3 one-liner with conditionals

How can I craft a one-liner return line that also has conditionals? For example, if I want to make a 'return the median' one:
//assuming sorted input array
return ((inputArray.length % 2) && (inputArray[int(inputArray.length/2)] + inputArray[int(inputArray.length/2)+1]) / 2) || inputArray[int(inputArray.length/2)+1];
Is there anyway to make this work?
You should use the ternary operator:
return (inputArray.length % 2 != 0) ? (inputArray[int(inputArray.length/2)] + inputArray[int(inputArray.length/2)+1]) / 2 : inputArray[int(inputArray.length/2)+1];
Which is equivalent to:
if (inputArray.length % 2 != 0) {
return (inputArray[int(inputArray.length/2)] + inputArray[int(inputArray.length/2)+1]) / 2;
} else {
return inputArray[int(inputArray.length/2)+1];
}
If you want to use only && and ||, you can use the following (which is not really a good programming style):
((inputArray.length % 2 != 0) || return inputArray[int(inputArray.length/2)+1]) && return (inputArray[int(inputArray.length/2)] + inputArray[int(inputArray.length/2)+1]);
Which is equivalent to:
(condition || return value2) && return value1;
So, thanks to the short-circuit evaluation of boolean operators:
if condition is true, return value2 is not evaluated and return value1 will be executed.
if condition is false, return value2 will be evaluated.
I would go for a more readable version without repetitions, but with some variables.
var index:int = int(inputArray.length / 2);
var item1:Number = inputArray[index];
var item2:Number = inputArray[index + 1];
var median:Number = (item1 + item2) / 2;
return (inputArray.length % 2 != 0) ? median : item2;
Please try with modified conditions:
return ((inputArray.length % 2) && (((inputArray[int(inputArray.length/2)] + inputArray[int(inputArray.length/2)+1]) / 2) || inputArray[int(inputArray.length/2)+1]));

Increase when smaller than, decrease when lower than

Out of curiosity, is there a (language independent*) way to make these two generic statements into one statement?
if (a < b) a += x;
else if (a > b) a -= x;
May also be used as
if (abs(b - a) < x) a = b;
else if (a < b) a += x;
else if (a > b) a -= x;
Oh, now that I'm writing this I'm thinking of something like this:
if (a != b) a += x * (a < b ? 1 : -1);
But that doesn't really benefit the readability. Other ways perhaps?
= Normal languages, not Brainfuck and the likes or really ancient ones
a += x * sgn(b-a)
Where sgn is the sign function:
sgn x | x > 0 = 1
| x < 0 = -1
| otherwise = 0
You should really wonder why you want this, the compiler will do fine optimizing and the given statement is very readable.
Language independent is a bit tricky, but if you have cmp or similar, you can use that:
a += x * cmp(b, a)
cmp(b, a) returns:
0 if b==a
1 if b > a
-1 if a < b
Also, there is a bug in your suggested implementation:
a = 9;
b = 10;
x = 2;
if (a < b) a += x;
if (a > b) a -= x;
print a;
Output: 9 (expected 11)
You need an else to fix this. Alternatively, if you want to prevent a going past b, do this:
if (a < b)
{
a = min(b, a + x);
}
else if (a > b)
{
a = max(b, a - x);
}
If you want to do this in one expression you can:
a = (a < b) ? min(b, a + x) : max(b, a - x);
I think the first way is clearer though.
a += (b - a) / abs(b - a) * x
But this is not language independent and does not really help readiness if you ask me....