Решение на Форматиране на импорти от Йоанна Ненкова
Резултати
- 5 точки от тестове
- 0 бонус точки
- 5 точки общо
- 5 успешни тест(а)
- 15 неуспешни тест(а)
Код
struct Node {
value: String,
children: Vec<Node>,
}
impl Node {
fn new(value: String) -> Self {
Node {
value,
children: Vec::new(),
}
}
fn add(&mut self, path: &[&str]) {
if let Some((head, tail)) = path.split_first() {
let mut child = self.children.iter_mut().find(|child| child.value == *head);
if child.is_none() {
self.children.push(Node::new((*head).to_string()));
child = self.children.iter_mut().find(|child| child.value == *head);
}
if let Some(child) = child {
child.add(tail);
}
}
}
fn render(&self, depth: usize) -> Vec<String> {
let mut lines = Vec::new();
let last_child_index = self.children.len().saturating_sub(1);
for (index, child) in self.children.iter().enumerate() {
lines.push(format!("{:indent$}{}", "", child.value, indent = depth * 4));
if !child.children.is_empty() {
if let Some(last_line) = lines.last_mut() {
last_line.push_str("::{\n");
}
let child_lines = child.render(depth + 1);
for line in child_lines {
lines.push(format!("{:indent$}", line, indent = (depth + 1) * 4));
}
lines.push(format!("{:indent$}}}", "", indent = depth * 4));
}
if index != last_child_index {
lines.push(format!("{:indent$},", "", indent = depth * 4));
}
}
lines
}
}
struct Tree {
root: Node,
}
impl Tree {
fn new(root_value: String) -> Self {
Tree {
root: Node::new(root_value),
}
}
fn add(&mut self, path: &[&str]) {
self.root.add(path);
}
}
#[derive(Clone)]
pub struct Import<'a>(pub &'a [&'a str]);
pub enum Order {
Original,
Sorted,
}
pub fn format_flat(imports: &[Import], order: Order) -> Vec<String> {
let mut result: Vec<String> = imports
.iter()
.map(|import| import.0.join("::"))
.collect();
if let Order::Sorted = order {
result.sort();
}
result
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut tree = Tree::new(String::new());
for import in imports {
tree.add(import.0);
}
if let Order::Sorted = order {
sort_tree(&mut tree.root);
}
tree.root.render(0)
}
fn sort_tree(node: &mut Node) {
node.children.sort_by(|a, b| a.value.cmp(&b.value));
for child in &mut node.children {
sort_tree(child);
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20241203-1739405-cqbich/solution)
Finished test [unoptimized + debuginfo] target(s) in 1.06s
Running tests/solution_test.rs (target/debug/deps/solution_test-1428e1090729d165)
running 20 tests
test solution_test::test_flat_empty ... ok
test solution_test::test_flat_multi_crate ... ok
test solution_test::test_flat_original ... ok
test solution_test::test_flat_original_duplicates ... FAILED
test solution_test::test_flat_sorted ... ok
test solution_test::test_flat_sorted_duplicates ... FAILED
test solution_test::test_nested_basic ... FAILED
test solution_test::test_nested_deep ... FAILED
test solution_test::test_nested_empty ... ok
test solution_test::test_nested_only_crate ... FAILED
test solution_test::test_nested_original ... FAILED
test solution_test::test_nested_original_2 ... FAILED
test solution_test::test_nested_original_duplicates ... FAILED
test solution_test::test_nested_original_multi_crate ... FAILED
test solution_test::test_nested_sorted ... FAILED
test solution_test::test_nested_original_self ... FAILED
test solution_test::test_nested_sorted_duplicates ... FAILED
test solution_test::test_nested_sorted_2 ... FAILED
test solution_test::test_nested_sorted_multi_crate ... FAILED
test solution_test::test_nested_sorted_self ... FAILED
failures:
---- solution_test::test_flat_original_duplicates stdout ----
thread 'solution_test::test_flat_original_duplicates' panicked at 'assertion failed: `(left == right)`
left: `["std::string::String", "std::iter::once", "std::iter", "std::iter", "std::iter::repeat", "std::string::String"]`,
right: `["std::string::String", "std::iter::once", "std::iter", "std::iter::repeat"]`', tests/solution_test.rs:44:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
---- solution_test::test_flat_sorted_duplicates stdout ----
thread 'solution_test::test_flat_sorted_duplicates' panicked at 'assertion failed: `(left == right)`
left: `["std::iter", "std::iter", "std::iter::once", "std::iter::repeat", "std::string::String", "std::string::String"]`,
right: `["std::iter", "std::iter::once", "std::iter::repeat", "std::string::String"]`', tests/solution_test.rs:88:5
---- solution_test::test_nested_basic stdout ----
thread 'solution_test::test_nested_basic' panicked at 'assertion failed: `(left == right)`
left: `["std::{\n", " a", "}"]`,
right: `["std::{\n a,\n}\n"]`', tests/solution_test.rs:140:5
---- solution_test::test_nested_deep stdout ----
thread 'solution_test::test_nested_deep' panicked at 'assertion failed: `(left == right)`
left: `["std::{\n", " a::{\n", " b::{\n", " c::{\n", " d", " }", " }", " }", "}"]`,
right: `["std::{\n a::{\n b::{\n c::{\n d,\n },\n },\n },\n}\n"]`', tests/solution_test.rs:160:5
---- solution_test::test_nested_only_crate stdout ----
thread 'solution_test::test_nested_only_crate' panicked at 'assertion failed: `(left == right)`
left: `["my_crate"]`,
right: `["my_crate\n"]`', tests/solution_test.rs:132:5
---- solution_test::test_nested_original stdout ----
thread 'solution_test::test_nested_original' panicked at 'assertion failed: `(left == right)`
left: `["my_crate::{\n", " c", " ,", " b::{\n", " B2", " ,", " B1", " }", " ,", " a", "}"]`,
right: `["my_crate::{\n c,\n b::{\n B2,\n B1,\n },\n a,\n}\n"]`', tests/solution_test.rs:173:5
---- solution_test::test_nested_original_2 stdout ----
thread 'solution_test::test_nested_original_2' panicked at 'assertion failed: `(left == right)`
left: `["my_crate::{\n", " c", " ,", " b::{\n", " B2", " ,", " B1", " }", " ,", " a::{\n", " inner::{\n", " I1", " }", " ,", " A1", " }", "}"]`,
right: `["my_crate::{\n c,\n b::{\n B2,\n B1,\n },\n a::{\n inner::{\n I1,\n },\n A1,\n },\n}\n"]`', tests/solution_test.rs:198:5
---- solution_test::test_nested_original_duplicates stdout ----
thread 'solution_test::test_nested_original_duplicates' panicked at 'assertion failed: `(left == right)`
left: `["my_crate::{\n", " c", " ,", " b::{\n", " B2", " ,", " B1", " }", " ,", " a", "}"]`,
right: `["my_crate::{\n c,\n b::{\n B2,\n B1,\n },\n a,\n}\n"]`', tests/solution_test.rs:283:5
---- solution_test::test_nested_original_multi_crate stdout ----
thread 'solution_test::test_nested_original_multi_crate' panicked at 'assertion failed: `(left == right)`
left: `["crate::{\n", " b", " ,", " a", "}", ",", "std::{\n", " string::{\n", " String", " }", "}"]`,
right: `["crate::{\n b,\n a,\n}\n", "std::{\n string::{\n String,\n },\n}\n"]`', tests/solution_test.rs:385:5
---- solution_test::test_nested_sorted stdout ----
thread 'solution_test::test_nested_sorted' panicked at 'assertion failed: `(left == right)`
left: `["my_crate::{\n", " a", " ,", " b::{\n", " B1", " ,", " B2", " }", " ,", " c", "}"]`,
right: `["my_crate::{\n a,\n b::{\n B1,\n B2,\n },\n c,\n}\n"]`', tests/solution_test.rs:227:5
---- solution_test::test_nested_original_self stdout ----
thread 'solution_test::test_nested_original_self' panicked at 'assertion failed: `(left == right)`
left: `["my_crate::{\n", " c", " ,", " b::{\n", " B2", " ,", " B1", " }", " ,", " a", "}"]`,
right: `["my_crate::{\n c,\n b::{\n self,\n B2,\n B1,\n },\n a,\n}\n"]`', tests/solution_test.rs:334:5
---- solution_test::test_nested_sorted_duplicates stdout ----
thread 'solution_test::test_nested_sorted_duplicates' panicked at 'assertion failed: `(left == right)`
left: `["my_crate::{\n", " a", " ,", " b::{\n", " B1", " ,", " B2", " }", " ,", " c", "}"]`,
right: `["my_crate::{\n a,\n b::{\n B1,\n B2,\n },\n c,\n}\n"]`', tests/solution_test.rs:309:5
---- solution_test::test_nested_sorted_2 stdout ----
thread 'solution_test::test_nested_sorted_2' panicked at 'assertion failed: `(left == right)`
left: `["my_crate::{\n", " a::{\n", " A1", " ,", " inner::{\n", " I1", " }", " }", " ,", " b::{\n", " B1", " ,", " B2", " }", " ,", " c", "}"]`,
right: `["my_crate::{\n a::{\n A1,\n inner::{\n I1,\n },\n },\n b::{\n B1,\n B2,\n },\n c,\n}\n"]`', tests/solution_test.rs:252:5
---- solution_test::test_nested_sorted_multi_crate stdout ----
thread 'solution_test::test_nested_sorted_multi_crate' panicked at 'assertion failed: `(left == right)`
left: `["crate::{\n", " a", " ,", " b", "}", ",", "std::{\n", " string::{\n", " String", " }", "}"]`,
right: `["crate::{\n a,\n b,\n}\n", "std::{\n string::{\n String,\n },\n}\n"]`', tests/solution_test.rs:414:5
---- solution_test::test_nested_sorted_self stdout ----
thread 'solution_test::test_nested_sorted_self' panicked at 'assertion failed: `(left == right)`
left: `["my_crate::{\n", " a", " ,", " b::{\n", " B1", " ,", " B2", " }", " ,", " c", "}"]`,
right: `["my_crate::{\n a,\n b::{\n self,\n B1,\n B2,\n },\n c,\n}\n"]`', tests/solution_test.rs:360:5
failures:
solution_test::test_flat_original_duplicates
solution_test::test_flat_sorted_duplicates
solution_test::test_nested_basic
solution_test::test_nested_deep
solution_test::test_nested_only_crate
solution_test::test_nested_original
solution_test::test_nested_original_2
solution_test::test_nested_original_duplicates
solution_test::test_nested_original_multi_crate
solution_test::test_nested_original_self
solution_test::test_nested_sorted
solution_test::test_nested_sorted_2
solution_test::test_nested_sorted_duplicates
solution_test::test_nested_sorted_multi_crate
solution_test::test_nested_sorted_self
test result: FAILED. 5 passed; 15 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
error: test failed, to rerun pass `--test solution_test`
