Knapsack 0-1 in Rust

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
2
down vote

favorite












Here is an implementation of Knapsack in Rust. How could it be improved?
Some things I have already spotted:



What would be the best way to extract result? Is using an option preferred unwrapping the last element of vector? I also want to show last value of vector regardless of its contents, so would unwrapping be preferable?



How to save casting all the time for array indices, vector definitions?



use std::cmp;

fn solve_knapsack_dynamically(
weights: &Vec<i64>,
profits: &Vec<i64>,
knap_sack_max_weight: i64,
) -> Option<i64>
let mut prev_row = vec![0; knap_sack_max_weight as usize];
let mut curr_row = vec![0; knap_sack_max_weight as usize];

for i in 1..weights.len()
for j in 0..knap_sack_max_weight
if weights[i as usize] > j
curr_row[j as usize] = prev_row[j as usize];
else
curr_row[j as usize] = cmp::max(
prev_row[j as usize],
prev_row[(j - weights[i as usize]) as usize] + profits[i as usize],
);



prev_row = curr_row.to_vec();


curr_row.last().cloned()


fn main()
let weights = vec![10, 50, 20, 100];
let profits = vec![56, 23, 11, 4];
let knap_sack_max_weight = 120;

let result = solve_knapsack_dynamically(&weights, &profits, knap_sack_max_weight);

match result
Some(x) => println!("Result: ", x),
None => println!("Failed"),








share|improve this question





















  • Your indentation seems off. Is that the same indentation you've used in your program?
    – Zeta
    Mar 3 at 16:59










  • Should be better now
    – A. Romeu
    Mar 3 at 17:02
















up vote
2
down vote

favorite












Here is an implementation of Knapsack in Rust. How could it be improved?
Some things I have already spotted:



What would be the best way to extract result? Is using an option preferred unwrapping the last element of vector? I also want to show last value of vector regardless of its contents, so would unwrapping be preferable?



How to save casting all the time for array indices, vector definitions?



use std::cmp;

fn solve_knapsack_dynamically(
weights: &Vec<i64>,
profits: &Vec<i64>,
knap_sack_max_weight: i64,
) -> Option<i64>
let mut prev_row = vec![0; knap_sack_max_weight as usize];
let mut curr_row = vec![0; knap_sack_max_weight as usize];

for i in 1..weights.len()
for j in 0..knap_sack_max_weight
if weights[i as usize] > j
curr_row[j as usize] = prev_row[j as usize];
else
curr_row[j as usize] = cmp::max(
prev_row[j as usize],
prev_row[(j - weights[i as usize]) as usize] + profits[i as usize],
);



prev_row = curr_row.to_vec();


curr_row.last().cloned()


fn main()
let weights = vec![10, 50, 20, 100];
let profits = vec![56, 23, 11, 4];
let knap_sack_max_weight = 120;

let result = solve_knapsack_dynamically(&weights, &profits, knap_sack_max_weight);

match result
Some(x) => println!("Result: ", x),
None => println!("Failed"),








share|improve this question





















  • Your indentation seems off. Is that the same indentation you've used in your program?
    – Zeta
    Mar 3 at 16:59










  • Should be better now
    – A. Romeu
    Mar 3 at 17:02












up vote
2
down vote

favorite









up vote
2
down vote

favorite











Here is an implementation of Knapsack in Rust. How could it be improved?
Some things I have already spotted:



What would be the best way to extract result? Is using an option preferred unwrapping the last element of vector? I also want to show last value of vector regardless of its contents, so would unwrapping be preferable?



How to save casting all the time for array indices, vector definitions?



use std::cmp;

fn solve_knapsack_dynamically(
weights: &Vec<i64>,
profits: &Vec<i64>,
knap_sack_max_weight: i64,
) -> Option<i64>
let mut prev_row = vec![0; knap_sack_max_weight as usize];
let mut curr_row = vec![0; knap_sack_max_weight as usize];

for i in 1..weights.len()
for j in 0..knap_sack_max_weight
if weights[i as usize] > j
curr_row[j as usize] = prev_row[j as usize];
else
curr_row[j as usize] = cmp::max(
prev_row[j as usize],
prev_row[(j - weights[i as usize]) as usize] + profits[i as usize],
);



prev_row = curr_row.to_vec();


curr_row.last().cloned()


fn main()
let weights = vec![10, 50, 20, 100];
let profits = vec![56, 23, 11, 4];
let knap_sack_max_weight = 120;

let result = solve_knapsack_dynamically(&weights, &profits, knap_sack_max_weight);

match result
Some(x) => println!("Result: ", x),
None => println!("Failed"),








share|improve this question













Here is an implementation of Knapsack in Rust. How could it be improved?
Some things I have already spotted:



What would be the best way to extract result? Is using an option preferred unwrapping the last element of vector? I also want to show last value of vector regardless of its contents, so would unwrapping be preferable?



How to save casting all the time for array indices, vector definitions?



use std::cmp;

fn solve_knapsack_dynamically(
weights: &Vec<i64>,
profits: &Vec<i64>,
knap_sack_max_weight: i64,
) -> Option<i64>
let mut prev_row = vec![0; knap_sack_max_weight as usize];
let mut curr_row = vec![0; knap_sack_max_weight as usize];

for i in 1..weights.len()
for j in 0..knap_sack_max_weight
if weights[i as usize] > j
curr_row[j as usize] = prev_row[j as usize];
else
curr_row[j as usize] = cmp::max(
prev_row[j as usize],
prev_row[(j - weights[i as usize]) as usize] + profits[i as usize],
);



prev_row = curr_row.to_vec();


curr_row.last().cloned()


fn main()
let weights = vec![10, 50, 20, 100];
let profits = vec![56, 23, 11, 4];
let knap_sack_max_weight = 120;

let result = solve_knapsack_dynamically(&weights, &profits, knap_sack_max_weight);

match result
Some(x) => println!("Result: ", x),
None => println!("Failed"),










share|improve this question












share|improve this question




share|improve this question








edited Mar 3 at 21:56









Phrancis

14.6k645137




14.6k645137









asked Mar 3 at 16:12









zcqwevb

1112




1112











  • Your indentation seems off. Is that the same indentation you've used in your program?
    – Zeta
    Mar 3 at 16:59










  • Should be better now
    – A. Romeu
    Mar 3 at 17:02
















  • Your indentation seems off. Is that the same indentation you've used in your program?
    – Zeta
    Mar 3 at 16:59










  • Should be better now
    – A. Romeu
    Mar 3 at 17:02















Your indentation seems off. Is that the same indentation you've used in your program?
– Zeta
Mar 3 at 16:59




Your indentation seems off. Is that the same indentation you've used in your program?
– Zeta
Mar 3 at 16:59












Should be better now
– A. Romeu
Mar 3 at 17:02




Should be better now
– A. Romeu
Mar 3 at 17:02















active

oldest

votes











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%2f188733%2fknapsack-0-1-in-rust%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188733%2fknapsack-0-1-in-rust%23new-answer', 'question_page');

);

Post as a guest