Puzzle-VBForums
Results 1 to 12 of 12

Thread: Puzzle

Hybrid View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2010
    Posts
    66

    Smile 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??.

  2. #2
    Addicted Member
    Join Date
    Jun 2010
    Location
    The Keystone State
    Posts
    131

    Re: Puzzle

    You should have posted this in the maths forum. I'm certain it would have been answered almost immediately.

  3. #3
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,054

    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

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Puzzle

    Quote Originally Posted by rnjnagu View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  5. #5
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,054

    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

  6. #6
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,428

    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

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Puzzle

    I'm gonna build a shrine to you Milk
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  8. #8
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    3,326

    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.
    When one of my minions says, "Hey, he's just one guy, what can he do?" I say "This"... and shoot them.

    The problem with putting your lair in a volcano is keeping your robot army from melting.

    I know that the human being and the fish can coexist peacefully - George Bush

  9. #9
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,428

    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?
    W o t . S i g

  10. #10
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    3,326

    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.
    When one of my minions says, "Hey, he's just one guy, what can he do?" I say "This"... and shoot them.

    The problem with putting your lair in a volcano is keeping your robot army from melting.

    I know that the human being and the fish can coexist peacefully - George Bush

  11. #11
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,428

    Re: Puzzle

    I meant OrElse as opposed to Or but you knew that.
    W o t . S i g

  12. #12
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Puzzle

    One line you say ?
    vb.net Code:
    1. Dim X As Integer = 10
    2.         Dim Y As Integer = 11
    3.         Dim Z As Integer = 5
    4.  
    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))))

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.