-
Mar 26th, 2012, 04:35 AM
#1
Thread Starter
Lively Member
Puzzle
Assume you have a method "SAMPLE" which accepts 3 integer parameters namely VALUE1, VALUE2 & VALUE3 and return an integer value which will be the product of the top 2 bigger values (out of the 3 input parameters).
Note : Try out an one line logic for this method, which shouldn’t use if else / ternary / max functions??.
-
Apr 6th, 2012, 09:50 PM
#2
Addicted Member
Re: Puzzle
You should have posted this in the maths forum. I'm certain it would have been answered almost immediately.
-
Apr 7th, 2012, 02:26 PM
#3
Re: Puzzle
What is this? Is it some kind of homework, or a challenge? If it is homework, is it a problem teaching something about LINQ, or is LINQ another item that has to be excluded?
If using LINQ is the point of the answer, what is the point of the question?
My usual boring signature: Nothing
-
Apr 7th, 2012, 06:34 PM
#4
Re: Puzzle
Originally Posted by rnjnagu
Assume you have a method "SAMPLE" which accepts 3 integer parameters namely VALUE1, VALUE2 & VALUE3 and return an integer value which will be the product of the top 2 bigger values (out of the 3 input parameters).
Note : Try out an one line logic for this method, which shouldn’t use if else / ternary / max functions??.
I can't see how this could be accomplished without If...Then. The calculation done is based on a condition so there is need for conditional branching. Anyone who can do this without it, I'd buy them a drink.
-
Apr 7th, 2012, 08:38 PM
#5
Re: Puzzle
As long as the items are in an array, or can be put into an array, it can be done with a truly ugly LINQ statement, or a multi line Lamda. Neither one is particularly efficient, and it makes use of some form of collection sorting.
My usual boring signature: Nothing
-
Apr 9th, 2012, 06:04 AM
#6
Re: Puzzle
If your chosen language allows implicit conversion between booleans and integers (eg c,c++,vb classic)...
C
Code:
int TopTwoSum(int a, int b, int c)
{
return a * (a>=b|a>=c) + b * (b>a|b>=c) + c * (c>a|c>b);
}
VB classic warning: my vb is getting pretty rusty
Code:
Function TopTwoSum(ByVal a As Integer, ByVal b As Integer,ByVal c As Integer) As Integer
TopTwoSum = -a * (a>=b Or a>=c) - b * (b>a Or b>=c) - c * (c>a Or c>b);
End Function
Last edited by Milk; Apr 9th, 2012 at 06:28 AM.
Reason: added a VB classic example
W o t . S i g
-
Apr 9th, 2012, 07:21 AM
#7
Re: Puzzle
I'm gonna build a shrine to you Milk
-
Apr 10th, 2012, 06:39 AM
#8
Re: Puzzle
Arguably that technique falls under the category of an If Else construct. It's a form of branching.
The answer really depends on whether this sentence:-
which shouldn’t use if else / ternary / max functions
refers to categories of techniques or specific items of syntax.
If it's categories of techniques then I would argue that this is impossible because the problem logically requires either branching or a sort and select.
If it's distinct bits of syntax then the answers easy because you simply pick a different branching or sorting mechanism (e.g. case).
In truth, it just wasn't a very well phrased question.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Apr 10th, 2012, 10:02 AM
#9
Re: Puzzle
I agree that the question is rather vague, but I would argue that there is no branching in that code. If I'd used conditional logical operators then yes, there would be branching. If you like it is a branchless If Else construct. I'm terming a branch as a point where the set of instructions to be executed by the processor is dependent on the result of a comparison instruction.
btw hows the climbing?
-
Apr 11th, 2012, 08:05 AM
#10
Re: Puzzle
We're definitely getting into semantics but...
If I'd used conditional logical operators
You have:-
(a>=b|a>=c)
(b>a|b>=c)
(c>a|c>b)
I'd also argue (although pretty tenuously, I must admit) that you do have a different set of instructions being executed as a result of the comparison. They just happen to exist on the same line and the instruction is actually calculated from the reults of the comparison instead of being reached by means of a jump.
I still take my hat off to you though. I wouldn't have thought to use false = 0 like that.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Apr 14th, 2012, 03:20 AM
#11
Re: Puzzle
I meant OrElse as opposed to Or but you knew that.
-
Apr 18th, 2012, 09:07 AM
#12
Re: Puzzle
One line you say ?
vb.net Code:
Dim X As Integer = 10 Dim Y As Integer = 11 Dim Z As Integer = 5 Console.WriteLine((X - ((X - Y) And ((X - Y) >> 31))) + ((X + Y) - (X - ((X - Y) And ((X - Y) >> 31))) - (((X + Y) - (X - ((X - Y) And ((X - Y) >> 31))) - Z) And (((X + Y) - (X - ((X - Y) And ((X - Y) >> 31))) - Z) >> 31))))
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|