IndaPlus22 / dnorma-task-2

0 stars 0 forks source link

Komplettering #1

Open violaso opened 2 years ago

violaso commented 2 years ago

Well done Daniel!

I'm only missing one additional solution to a Kattis problem. Did you miss a git push?

Assignment instructions:

Solve at least two of the following problems:

Tips

Some tips on your code.

Avstånd till kanten

Your solution has som unnecessary branching.

Your solution:

fn check_num(row_nr: u32, rows: u32, column_nr: u32, columns: u32) -> String{
    let row_distance: u32;
    let column_distance: u32;
    if (rows - row_nr) < row_nr {
        row_distance = rows - row_nr;
    }
    else {
        row_distance = row_nr;
    }
    if (columns - column_nr) > column_nr {
        column_distance = column_nr;
    }
    else {
        column_distance = columns - column_nr;
    }

    if cmp::min(column_distance, row_distance) >= 9 {
        return ".".to_string();
    }
    else {
        return (cmp::min(column_distance, row_distance)+1).to_string();
    }

}

Optimised solution:

fn check_num(row_nr: u32, rows: u32, column_nr: u32, columns: u32) -> String{
    let row_distance = cmp::min(rows - row_nr, row_nr);
    let column_distance = cmp::min(columns - column_nr, column_nr);

    match cmp::min(column_distance, row_distance) {
        _x if _x >= 9 => ".".to_string(),
        _x => (_x + 1).to_string()
    }
}

Keep up the good work!

DanielN77 commented 2 years ago

Klar förresten(glömde skriva, sorry)