INDAPlus21 / INDAPlus21/antonul-task-2

Pass

Open
#1 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

**Very well done Anton!**

Tips och tricks följer:

_Your code_:
```rust
// ...

let mut sum = 0;

match numbers.len() {
// edge cases, logic and print
0 => {
sum = 0;
}
_ => {
numbers.sort();
let half_of_numbers = numbers.len()/2;
for number in half_of_numbers..numbers.len() {
sum += numbers[number];
}
}
}
println!("{}", sum);
```

_Condensed vesion:_
```rust
// ...

println!("{}",
match numbers.len() {
0 => 0,
_len => {
numbers.sort();
numbers[_len/2.._len].iter().sum()
}
}
);
```

_Your code_:
```rust
// ...

for row in 1..=rows {
let mut row_output: String = String::default();
for col in 1..=columns {
let top_dist = row;
let bot_dist = rows - row + 1;
let left_dist = col;
let right_dist = columns - col + 1;
//gets the smallest value of ..._dist variables
let min_distance = top_dist.min(bot_dist).min(left_dist).min(right_dist);
//adds the distance if it's under 10 to output and . if it's not
if min_distance < 10 {
row_output.push_str(&min_distance.to_string());
}
else {
row_output.push('.');
}
}
println!("{}", row_output);
}
```

_Optimised version:_
```rust
//...

for _row in 1..=rows {
// Pre-allocate memory.
let mut row = String::with_capacity(5);

for _col in 1..=columns {
let top_dist = _row;
let bottom_dist = rows - _row + 1;
let left_dist = _col;
let right_dist = columns - _col + 1;

row.push(
match top_dist.min(bottom_dist).min(left_dist).min(right_dist) {
_min_dist if _min_dist < 10 =>
// Quicker to convert directly to static sized type, than to dynamic string to static sized string.
std::char::from_digit(_min_dist, 10).unwrap(),
_ => '.'
}
);
}

println!("{}", row);
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

No file, test, or entry point is named. The issue is feedback on Rust code rather than a requested change, so confirm whether any documentation or code update is intended before starting; no completion criterion is provided.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
10/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.