Решение на Форматиране на импорти от Ясен Ефремов
Резултати
- 12 точки от тестове
- 0 бонус точки
- 12 точки общо
- 12 успешни тест(а)
- 8 неуспешни тест(а)
Код
#[derive(Debug, Clone, Copy)]
pub struct Import<'a>(pub &'a [&'a str]);
#[derive(Clone, Copy)]
pub enum Order {
Original,
Sorted,
}
#[derive(Default, PartialEq, Eq, PartialOrd, Ord)]
struct Node { value: String, children: Vec<Node>, import_self: bool }
#[derive(Default)]
struct Tree { root: Node }
impl Tree {
fn to_string(&self, tree_root: &Node, depth: usize) -> String {
let mut res = String::new();
if tree_root.children.is_empty() {
res = format!("{}{},\n", " ".repeat(depth), tree_root.value);
} else {
res.push_str(format!("{}{}::{{\n", " ".repeat(depth), tree_root.value).as_str());
if tree_root.import_self {
res.push_str(format!("{}self,\n", " ".repeat(depth + 4)).as_str());
}
for child in &tree_root.children {
res.push_str(self.to_string(child, depth + 4).as_str());
}
if depth == 0 {
res.push_str(format!("{}}}\n", " ".repeat(depth)).as_str());
} else {
res.push_str(format!("{}}},\n", " ".repeat(depth)).as_str());
}
}
res
}
}
pub fn format_flat(imports: &[Import], order: Order) -> Vec<String> {
let mut res: Vec<String> = Vec::new();
for i in imports {
let new_import = i.0.join("::");
if !res.contains(&new_import) { res.push(new_import); }
}
match order {
Order::Original => res,
Order::Sorted => { res.sort(); res },
}
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut res: Vec<String> = Vec::new();
let mut res_trees: Vec<Tree> = Vec::new();
let mut curr_tree = Tree::default();
let mut curr_node: &mut Node;
let mut curr_top = "";
for import in imports {
if import.0.len() == 0 { continue; }
if import.0[0] != curr_top {
if !curr_top.is_empty() {
res_trees.push(curr_tree);
}
curr_top = import.0[0];
curr_tree = Tree { root: Node { value: curr_top.into(), children: Vec::new(), import_self: false } };
}
curr_node = &mut curr_tree.root;
if import.0.len() == 1 { curr_node.import_self = true; }
for &t in &import.0[1..] {
match order {
Order::Original => (),
Order::Sorted => curr_node.children.sort(),
}
curr_node = if curr_node.children.iter().filter(|c| c.value == t).collect::<Vec<&Node>>().len() != 0 {
curr_node.children.iter_mut().filter(|c| c.value == t).next().unwrap()
} else {
curr_node.children.push(Node { value: t.into(), children: Vec::new(), import_self: false });
curr_node.children.last_mut().unwrap()
};
if t == *import.0.last().unwrap() { curr_node.import_self = true; }
}
}
res_trees.push(curr_tree);
for t in res_trees {
res.push(t.to_string(&t.root, 0));
}
res
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20241203-1739405-9st1lm/solution) Finished test [unoptimized + debuginfo] target(s) in 1.28s 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 ... ok test solution_test::test_flat_sorted_duplicates ... ok test solution_test::test_flat_sorted ... ok test solution_test::test_nested_basic ... ok test solution_test::test_nested_deep ... ok test solution_test::test_nested_empty ... FAILED test solution_test::test_nested_only_crate ... FAILED test solution_test::test_nested_original ... ok test solution_test::test_nested_original_2 ... ok test solution_test::test_nested_original_duplicates ... ok test solution_test::test_nested_original_multi_crate ... FAILED test solution_test::test_nested_sorted ... FAILED test solution_test::test_nested_original_self ... ok test solution_test::test_nested_sorted_2 ... FAILED test solution_test::test_nested_sorted_duplicates ... FAILED test solution_test::test_nested_sorted_multi_crate ... FAILED test solution_test::test_nested_sorted_self ... FAILED failures: ---- solution_test::test_nested_empty stdout ---- thread 'solution_test::test_nested_empty' panicked at 'assertion failed: `(left == right)` left: `[",\n"]`, right: `[]`', tests/solution_test.rs:122:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ---- solution_test::test_nested_only_crate stdout ---- thread 'solution_test::test_nested_only_crate' panicked at 'assertion failed: `(left == right)` left: `["my_crate,\n"]`, right: `["my_crate\n"]`', tests/solution_test.rs:132: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,\n}\n", "std::{\n string::{\n String,\n },\n}\n", "crate::{\n a,\n}\n"]`, 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,\n b::{\n B2,\n B1,\n },\n c,\n}\n"]`, 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_sorted_2 stdout ---- thread 'solution_test::test_nested_sorted_2' panicked at 'assertion failed: `(left == right)` left: `["my_crate::{\n a::{\n inner::{\n I1,\n },\n A1,\n },\n b::{\n B2,\n B1,\n },\n c,\n}\n"]`, 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_duplicates stdout ---- thread 'solution_test::test_nested_sorted_duplicates' panicked at 'assertion failed: `(left == right)` left: `["my_crate::{\n a,\n b::{\n B2,\n B1,\n },\n c,\n}\n"]`, 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_multi_crate stdout ---- thread 'solution_test::test_nested_sorted_multi_crate' panicked at 'assertion failed: `(left == right)` left: `["crate::{\n b,\n}\n", "std::{\n string::{\n String,\n },\n}\n", "crate::{\n a,\n}\n"]`, 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,\n b::{\n self,\n B2,\n B1,\n },\n c,\n}\n"]`, 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_nested_empty solution_test::test_nested_only_crate solution_test::test_nested_original_multi_crate 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. 12 passed; 8 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass `--test solution_test`
История (2 версии и 0 коментара)
Ясен качи решение на 03.12.2024 16:53 (преди 9 месеца)
#[derive(Debug, Clone, Copy)]
pub struct Import<'a>(pub &'a [&'a str]);
#[derive(Clone, Copy)]
pub enum Order {
Original,
Sorted,
}
#[derive(Default, PartialEq, Eq, PartialOrd, Ord)]
-struct Node { value: String, children: Vec<Node> }
+struct Node { value: String, children: Vec<Node>, import_self: bool }
#[derive(Default)]
struct Tree { root: Node }
impl Tree {
fn to_string(&self, tree_root: &Node, depth: usize) -> String {
let mut res = String::new();
if tree_root.children.is_empty() {
res = format!("{}{},\n", " ".repeat(depth), tree_root.value);
} else {
res.push_str(format!("{}{}::{{\n", " ".repeat(depth), tree_root.value).as_str());
+ if tree_root.import_self {
+ res.push_str(format!("{}self,\n", " ".repeat(depth + 4)).as_str());
+ }
+
for child in &tree_root.children {
res.push_str(self.to_string(child, depth + 4).as_str());
}
+
if depth == 0 {
res.push_str(format!("{}}}\n", " ".repeat(depth)).as_str());
} else {
res.push_str(format!("{}}},\n", " ".repeat(depth)).as_str());
}
}
res
}
}
pub fn format_flat(imports: &[Import], order: Order) -> Vec<String> {
let mut res: Vec<String> = Vec::new();
for i in imports {
let new_import = i.0.join("::");
if !res.contains(&new_import) { res.push(new_import); }
}
match order {
Order::Original => res,
Order::Sorted => { res.sort(); res },
}
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut res: Vec<String> = Vec::new();
let mut res_trees: Vec<Tree> = Vec::new();
let mut curr_tree = Tree::default();
let mut curr_node: &mut Node;
let mut curr_top = "";
for import in imports {
+ if import.0.len() == 0 { continue; }
if import.0[0] != curr_top {
if !curr_top.is_empty() {
res_trees.push(curr_tree);
}
curr_top = import.0[0];
- curr_tree = Tree { root: Node { value: curr_top.into(), children: Vec::new() } };
+ curr_tree = Tree { root: Node { value: curr_top.into(), children: Vec::new(), import_self: false } };
}
curr_node = &mut curr_tree.root;
+ if import.0.len() == 1 { curr_node.import_self = true; }
for &t in &import.0[1..] {
match order {
Order::Original => (),
Order::Sorted => curr_node.children.sort(),
}
curr_node = if curr_node.children.iter().filter(|c| c.value == t).collect::<Vec<&Node>>().len() != 0 {
curr_node.children.iter_mut().filter(|c| c.value == t).next().unwrap()
} else {
- curr_node.children.push(Node { value: t.into(), children: Vec::new() });
+ curr_node.children.push(Node { value: t.into(), children: Vec::new(), import_self: false });
curr_node.children.last_mut().unwrap()
};
-
- curr_node.value = t.into();
+
+ if t == *import.0.last().unwrap() { curr_node.import_self = true; }
}
}
res_trees.push(curr_tree);
-
for t in res_trees {
res.push(t.to_string(&t.root, 0));
}
-
res
}