INDAPlus21 / INDAPlus21/osveijer-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 Oliver!**

Your solutions are correct. But don't forget the screenshots next time!

Tips:

_Your code_:
```rust
let mut rect: String = "".to_owned();
for i in 1..r as i32+1 {
for j in 1..k as i32+1 {
let rdist = (rmid - (i as f32 - rmid).abs()) as i32;
let kdist = (kmid - (j as f32 - kmid).abs()) as i32;
if rdist > 9 && kdist > 9 {
rect = rect + ".";
} else if rdist < kdist {
rect = rect + &rdist.to_string();
} else {
rect = rect + &kdist.to_string();
}
}
rect = rect + "\n"
}

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

_Optimised version_:
```rust
// Pre-allocate heap memory to prevent re-allocation upon push.
let mut rect: String = String::with_capacity(r*(k+1.0) as usize);

for i in 1..r as i32+1 {
for j in 1..k as i32+1 {
let rdist = (rmid - (i as f32 - rmid).abs()) as u32;
let kdist = (kmid - (j as f32 - kmid).abs()) as u32;

// Push characters instead of re-allocation upon re-assignment.
rect.push(
if rdist > 9 && kdist > 9 {
'.'
} else {
std::char::from_digit(
match rdist < kdist {
true => rdist,
false => kdist
}
).unwrap()
}
);
}
rect.push('\n');
}

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

_More optimised version_ (no additional heap memory allocation):
```rust
for i in 1..r as i32+1 {
for j in 1..k as i32+1 {
let rdist = (rmid - (i as f32 - rmid).abs()) as u32;
let kdist = (kmid - (j as f32 - kmid).abs()) as u32;

print!("{}",
if rdist > 9 && kdist > 9 {
'.'
} else {
std::char::from_digit(rdist.min(kdist)).unwrap()
}
);
}
println!();
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

No source file or test is named. Start by locating the Rust program that produces the shown rectangle output, then compare its current implementation with the snippets in the issue; completion is unclear because the issue provides feedback and optimization examples rather than a specific requested change.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.