Knapsack 0-1 in Rust

Clash 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"),
rust knapsack-problem
add a comment |Â
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"),
rust knapsack-problem
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
add a comment |Â
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"),
rust knapsack-problem
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"),
rust knapsack-problem
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
add a comment |Â
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
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f188733%2fknapsack-0-1-in-rust%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
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