Knapsack01 problem, written in c#
Clash 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];
c# dynamic-programming knapsack-problem
add a comment |Â
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];
c# dynamic-programming knapsack-problem
add a comment |Â
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];
c# dynamic-programming knapsack-problem
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];
c# dynamic-programming knapsack-problem
edited Feb 6 at 17:24
200_success
123k14143401
123k14143401
asked Feb 6 at 6:31
reviewee
161
161
add a comment |Â
add a comment |Â
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, int
s, double
s, string
s...
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 theMath.Max()
behavior (which already works for more than just ints).
â Flater
Feb 9 at 14:49
add a comment |Â
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.
add a comment |Â
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, int
s, double
s, string
s...
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 theMath.Max()
behavior (which already works for more than just ints).
â Flater
Feb 9 at 14:49
add a comment |Â
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, int
s, double
s, string
s...
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 theMath.Max()
behavior (which already works for more than just ints).
â Flater
Feb 9 at 14:49
add a comment |Â
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, int
s, double
s, string
s...
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, int
s, double
s, string
s...
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 theMath.Max()
behavior (which already works for more than just ints).
â Flater
Feb 9 at 14:49
add a comment |Â
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 theMath.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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited Feb 6 at 17:25
200_success
123k14143401
123k14143401
answered Feb 6 at 6:47
jhilgeman
1614
1614
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password