INDAPlus21 / INDAPlus21/eliased-structures-task-15
Pass
Open
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 0
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
Very well done Elias!
Nice tree! Little messy code thou. God start for using Rust!
Here are some notes:
Data Structure
- Why do you count the lines inside of command for-loop to print at
if line_iter == length_of_commands && line_iter > 0, when you can print outside of the for-loop? - Why do you fit the logic of command type-2 in the same scope, and not seperate them?
- An optimisation would be to print early on all
!is_stack,!is_queue,!is_priority. - No need to trim and such.
- Note how I've refactored the code using match, iterators, and pattern matching.
Your code:
let mut commands = vec![];
for _line in input.lock().lines().map(|_line| _line.unwrap()) {
let command: Vec<u16> = _line
.split(' ')
.map(|s| s.trim())
.filter(|s| !s.is_empty())
.map(|s| s.parse().unwrap())
.collect();
commands.push(command);
if commands.len() == (commands[0][0] + 1).into() {
let mut line_iter = 0;
let mut length_of_commands = 0;
let mut queue_order: VecDeque<u32> = VecDeque::with_capacity(commands[0][0].into());
let mut stack_order = vec![];
let mut priority_order = vec![];
//...
for command_i in commands {
if command_i.len() == 1 {
length_of_commands = command_i[0];
} else if command_i[0] == 1 {
// queue_order.push(command_i[1]);
queue_order.push_back(command_i[1].into());
stack_order.push(command_i[1]);
priority_order.push(command_i[1]);
line_iter += 1;
} else if command_i[0] == 2 {
if is_stack || is_queue || is_priority {
if !priority_order.contains(&command_i[1])
&& !queue_order.contains(&command_i[1].into())
&& !stack_order.contains(&command_i[1])
{
is_stack = false;
is_queue = false;
is_priority = false;
} else {
let max_value = *priority_order.iter().max().unwrap();
if u32::from(command_i[1]) != queue_order[0] {
is_queue = false;
}
if command_i[1] != stack_order[stack_order.len() - 1] {
is_stack = false;
}
if command_i[1] != max_value {
is_priority = false;Guess the Data Structure!
}
queue_order.pop_front();
stack_order.remove(stack_order.len() - 1);
let max_index =
priority_order.iter().position(|&r| r == max_value).unwrap();
priority_order.remove(max_index);
}
}
line_iter += 1;
}
if line_iter == length_of_commands && line_iter > 0 {
if is_stack && !is_queue && !is_priority {
println!("stack");
} else if is_queue && !is_stack && !is_priority {
println!("queue");
} else if is_priority && !is_stack && !is_queue {
println!("priority queue");
} else if !is_priority && !is_stack && !is_queue {
println!("impossible");
} else {
println!("not sure");
}
}
}
commands = vec![];
}
}
Refactored (untested) code:
let lines: Vec<String> = input
.lock()
.lines()
.map(|_line| _line.unwrap()
.collect();
while lines.has_next() {
let number_of_commands = lines.next().unwrap().parse().unwrap();
let mut queue_order: VecDeque<u32> = VecDeque::with_capacity(number_of_commands);
let mut stack_order = Vec::with_capacity(number_of_commands);
let mut priority_order = Vec::with_capacity(number_of_commands);
//...
for _ in 0..number_of_commands {
let (command_type, command_value): (u16, u16) = {
let line = lines
.next().unwrap()
.split_whitespace()
.map(|_num| _num.parse().unwrap());
(line.next().unwrap(), line.next().unwrap())
};
match command_type {
1 => {
queue_order.push_back(command_value.into());
stack_order.push(command_value);
priority_order.push(command_value);
},
_ /*2*/ => {
// Queue
if is_queue && (queue_order.is_empty() || queue_order[0] != command_value as u32) {
is_queue = false;
}
queue_order.pop_front();
// Stack
if is_stack && (stack_order.is_empty() || command_value != stack_order[stack_order.len() - 1]) {
is_stack = false;
}
stack_order.remove(stack_order.len() - 1);
// Priority queue
if is_priority {
match *priority_order.iter().max() {
Some(_max) => {
if command_value != _max {
is_priority = false;
}
priority_order.remove(
priority_order.iter().position(|&r| r == _max).unwrap()
);
},
None => is_priority = false;
}
}
}
}
if !is_queue && !is_stack && !is_priority {
break;
}
}
println!("{}",
match (is_queue, is_stack, is_priority) {
(false, true, false) => "stack",
(true, false, false) => "queue",
(false, false, true) => "priority queue",
(false, false, false) => "impossible",
_ => "not sure"
}
);
}
Keep it up!
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Rust implementation that reads from input.lock().lines() and compare it with the inline refactored snippet. Resolve the listed data-structure and control-flow feedback, then verify that the program still emits the classifications shown: stack, queue, priority queue, impossible, or not sure.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- backend
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100