Knapsack01 problem, written in c#

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












Is this code written in a decent style? Appreciate some feedback : )



private static int Max(int a, int b)

return a > b ? a : b;


/// <summary>
/// Returns the solution to Knapsack01 problem. Uses bottom up dynamic programming.
/// </summary>
private static int Knapsack01(int capacity, int values, int weights)

int[,] table = new int[values.Length + 1, capacity + 1];


for (int row = 0; row <= values.Length; row++)

for (int column = 0; column <= capacity; column++)

if (row == 0


return table[values.Length, capacity];







share|improve this question



























    up vote
    3
    down vote

    favorite












    Is this code written in a decent style? Appreciate some feedback : )



    private static int Max(int a, int b)

    return a > b ? a : b;


    /// <summary>
    /// Returns the solution to Knapsack01 problem. Uses bottom up dynamic programming.
    /// </summary>
    private static int Knapsack01(int capacity, int values, int weights)

    int[,] table = new int[values.Length + 1, capacity + 1];


    for (int row = 0; row <= values.Length; row++)

    for (int column = 0; column <= capacity; column++)

    if (row == 0


    return table[values.Length, capacity];







    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      Is this code written in a decent style? Appreciate some feedback : )



      private static int Max(int a, int b)

      return a > b ? a : b;


      /// <summary>
      /// Returns the solution to Knapsack01 problem. Uses bottom up dynamic programming.
      /// </summary>
      private static int Knapsack01(int capacity, int values, int weights)

      int[,] table = new int[values.Length + 1, capacity + 1];


      for (int row = 0; row <= values.Length; row++)

      for (int column = 0; column <= capacity; column++)

      if (row == 0


      return table[values.Length, capacity];







      share|improve this question













      Is this code written in a decent style? Appreciate some feedback : )



      private static int Max(int a, int b)

      return a > b ? a : b;


      /// <summary>
      /// Returns the solution to Knapsack01 problem. Uses bottom up dynamic programming.
      /// </summary>
      private static int Knapsack01(int capacity, int values, int weights)

      int[,] table = new int[values.Length + 1, capacity + 1];


      for (int row = 0; row <= values.Length; row++)

      for (int column = 0; column <= capacity; column++)

      if (row == 0


      return table[values.Length, capacity];









      share|improve this question












      share|improve this question




      share|improve this question








      edited Feb 6 at 17:24









      200_success

      123k14143401




      123k14143401









      asked Feb 6 at 6:31









      reviewee

      161




      161




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          I might redefine Max() as



          private static T Max<T>(T a, T b) where T : IComparable<T>

          return a?.CompareTo(b) > 0 ? a : b;



          to be able to be reusable for any type that implements the IComparable<T> interface (int implements IComparable<int>, so it still works as expected). It'll be a tad slower, but it will quickly give you the max of just about anything, ints, doubles, strings...






          share|improve this answer





















          • What's the point? OP's entire code is built on the use of int types, not generic types. YAGNI, there's no reason to over-engineer this.
            – Flater
            Feb 9 at 13:14











          • I hardly call 5-10 seconds of thought over-engineering, but to each their own.
            – Jesse C. Slicer
            Feb 9 at 13:23










          • You're adding complexity for no discernible benefit. Quantifying the amount of complexity is irrelevant. There is no current (nor expected) use case for using other types. If anything, the entire method should be discarded as it's simply repeating the Math.Max() behavior (which already works for more than just ints).
            – Flater
            Feb 9 at 14:49

















          up vote
          1
          down vote













          It's not exactly well-documented. You can sort of determine what the "Knapsack problem" is by looking at a few comments and some variable names, but if I were the developer, I'd probably add a comment block to describe how it works in case someone else came upon this code later and needed to understand it quickly.



          I'm not quite sure why they didn't just use Math.Max instead of creating their own function (maybe they ported it from some other language that didn't have that function handy?).



          Aside from those things, if it's functional and returns accurate results, then I don't see a huge problem with it.






          share|improve this answer























            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186885%2fknapsack01-problem-written-in-c%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            I might redefine Max() as



            private static T Max<T>(T a, T b) where T : IComparable<T>

            return a?.CompareTo(b) > 0 ? a : b;



            to be able to be reusable for any type that implements the IComparable<T> interface (int implements IComparable<int>, so it still works as expected). It'll be a tad slower, but it will quickly give you the max of just about anything, ints, doubles, strings...






            share|improve this answer





















            • What's the point? OP's entire code is built on the use of int types, not generic types. YAGNI, there's no reason to over-engineer this.
              – Flater
              Feb 9 at 13:14











            • I hardly call 5-10 seconds of thought over-engineering, but to each their own.
              – Jesse C. Slicer
              Feb 9 at 13:23










            • You're adding complexity for no discernible benefit. Quantifying the amount of complexity is irrelevant. There is no current (nor expected) use case for using other types. If anything, the entire method should be discarded as it's simply repeating the Math.Max() behavior (which already works for more than just ints).
              – Flater
              Feb 9 at 14:49














            up vote
            1
            down vote













            I might redefine Max() as



            private static T Max<T>(T a, T b) where T : IComparable<T>

            return a?.CompareTo(b) > 0 ? a : b;



            to be able to be reusable for any type that implements the IComparable<T> interface (int implements IComparable<int>, so it still works as expected). It'll be a tad slower, but it will quickly give you the max of just about anything, ints, doubles, strings...






            share|improve this answer





















            • What's the point? OP's entire code is built on the use of int types, not generic types. YAGNI, there's no reason to over-engineer this.
              – Flater
              Feb 9 at 13:14











            • I hardly call 5-10 seconds of thought over-engineering, but to each their own.
              – Jesse C. Slicer
              Feb 9 at 13:23










            • You're adding complexity for no discernible benefit. Quantifying the amount of complexity is irrelevant. There is no current (nor expected) use case for using other types. If anything, the entire method should be discarded as it's simply repeating the Math.Max() behavior (which already works for more than just ints).
              – Flater
              Feb 9 at 14:49












            up vote
            1
            down vote










            up vote
            1
            down vote









            I might redefine Max() as



            private static T Max<T>(T a, T b) where T : IComparable<T>

            return a?.CompareTo(b) > 0 ? a : b;



            to be able to be reusable for any type that implements the IComparable<T> interface (int implements IComparable<int>, so it still works as expected). It'll be a tad slower, but it will quickly give you the max of just about anything, ints, doubles, strings...






            share|improve this answer













            I might redefine Max() as



            private static T Max<T>(T a, T b) where T : IComparable<T>

            return a?.CompareTo(b) > 0 ? a : b;



            to be able to be reusable for any type that implements the IComparable<T> interface (int implements IComparable<int>, so it still works as expected). It'll be a tad slower, but it will quickly give you the max of just about anything, ints, doubles, strings...







            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Feb 6 at 17:14









            Jesse C. Slicer

            10.9k2738




            10.9k2738











            • What's the point? OP's entire code is built on the use of int types, not generic types. YAGNI, there's no reason to over-engineer this.
              – Flater
              Feb 9 at 13:14











            • I hardly call 5-10 seconds of thought over-engineering, but to each their own.
              – Jesse C. Slicer
              Feb 9 at 13:23










            • You're adding complexity for no discernible benefit. Quantifying the amount of complexity is irrelevant. There is no current (nor expected) use case for using other types. If anything, the entire method should be discarded as it's simply repeating the Math.Max() behavior (which already works for more than just ints).
              – Flater
              Feb 9 at 14:49
















            • What's the point? OP's entire code is built on the use of int types, not generic types. YAGNI, there's no reason to over-engineer this.
              – Flater
              Feb 9 at 13:14











            • I hardly call 5-10 seconds of thought over-engineering, but to each their own.
              – Jesse C. Slicer
              Feb 9 at 13:23










            • You're adding complexity for no discernible benefit. Quantifying the amount of complexity is irrelevant. There is no current (nor expected) use case for using other types. If anything, the entire method should be discarded as it's simply repeating the Math.Max() behavior (which already works for more than just ints).
              – Flater
              Feb 9 at 14:49















            What's the point? OP's entire code is built on the use of int types, not generic types. YAGNI, there's no reason to over-engineer this.
            – Flater
            Feb 9 at 13:14





            What's the point? OP's entire code is built on the use of int types, not generic types. YAGNI, there's no reason to over-engineer this.
            – Flater
            Feb 9 at 13:14













            I hardly call 5-10 seconds of thought over-engineering, but to each their own.
            – Jesse C. Slicer
            Feb 9 at 13:23




            I hardly call 5-10 seconds of thought over-engineering, but to each their own.
            – Jesse C. Slicer
            Feb 9 at 13:23












            You're adding complexity for no discernible benefit. Quantifying the amount of complexity is irrelevant. There is no current (nor expected) use case for using other types. If anything, the entire method should be discarded as it's simply repeating the Math.Max() behavior (which already works for more than just ints).
            – Flater
            Feb 9 at 14:49




            You're adding complexity for no discernible benefit. Quantifying the amount of complexity is irrelevant. There is no current (nor expected) use case for using other types. If anything, the entire method should be discarded as it's simply repeating the Math.Max() behavior (which already works for more than just ints).
            – Flater
            Feb 9 at 14:49












            up vote
            1
            down vote













            It's not exactly well-documented. You can sort of determine what the "Knapsack problem" is by looking at a few comments and some variable names, but if I were the developer, I'd probably add a comment block to describe how it works in case someone else came upon this code later and needed to understand it quickly.



            I'm not quite sure why they didn't just use Math.Max instead of creating their own function (maybe they ported it from some other language that didn't have that function handy?).



            Aside from those things, if it's functional and returns accurate results, then I don't see a huge problem with it.






            share|improve this answer



























              up vote
              1
              down vote













              It's not exactly well-documented. You can sort of determine what the "Knapsack problem" is by looking at a few comments and some variable names, but if I were the developer, I'd probably add a comment block to describe how it works in case someone else came upon this code later and needed to understand it quickly.



              I'm not quite sure why they didn't just use Math.Max instead of creating their own function (maybe they ported it from some other language that didn't have that function handy?).



              Aside from those things, if it's functional and returns accurate results, then I don't see a huge problem with it.






              share|improve this answer

























                up vote
                1
                down vote










                up vote
                1
                down vote









                It's not exactly well-documented. You can sort of determine what the "Knapsack problem" is by looking at a few comments and some variable names, but if I were the developer, I'd probably add a comment block to describe how it works in case someone else came upon this code later and needed to understand it quickly.



                I'm not quite sure why they didn't just use Math.Max instead of creating their own function (maybe they ported it from some other language that didn't have that function handy?).



                Aside from those things, if it's functional and returns accurate results, then I don't see a huge problem with it.






                share|improve this answer















                It's not exactly well-documented. You can sort of determine what the "Knapsack problem" is by looking at a few comments and some variable names, but if I were the developer, I'd probably add a comment block to describe how it works in case someone else came upon this code later and needed to understand it quickly.



                I'm not quite sure why they didn't just use Math.Max instead of creating their own function (maybe they ported it from some other language that didn't have that function handy?).



                Aside from those things, if it's functional and returns accurate results, then I don't see a huge problem with it.







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited Feb 6 at 17:25









                200_success

                123k14143401




                123k14143401











                answered Feb 6 at 6:47









                jhilgeman

                1614




                1614






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186885%2fknapsack01-problem-written-in-c%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Chat program with C++ and SFML

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    Will my employers contract hold up in court?