Решение на Форматиране на импорти от Богдан Миронов
Резултати
- 5 точки от тестове
- 0 бонус точки
- 5 точки общо
- 5 успешни тест(а)
- 15 неуспешни тест(а)
Код
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 res = vec![];
for import in imports {
let importEntry = import.0.join("::");
res.push(importEntry);
}
return match order {
Order::Original => res,
Order::Sorted => {
res.sort();
res
}
};
}
struct Node { value: String, children: Vec<Node> }
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut root = Node {
value: String::new(),
children: vec![],
};
for import in imports {
let mut current_node = &mut root;
//Check if contains in child and ...
}
let mut result = vec![];
format_node(&root, 0, &mut result);
result
// let res_str = result.join("");
// res_str.pop() //Removing the last ,
// res_str
}
fn format_node(node: &Node, depth: usize, result: &mut Vec<String>) {
let indent = " ".repeat(depth);
if !node.value.is_empty() {
if !node.children.is_empty() {
result.push(format!("{}{}::{{\n", indent, node.value));
} else {
result.push(format!("{}{},\n", indent, node.value));
}
}
for child in &node.children {
format_node(child, depth + 1, result);
}
if !node.value.is_empty() && !node.children.is_empty() {
result.push(format!("{}}},\n", indent));
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20241203-1739405-toq70d/solution)
warning: unused variable: `import`
--> src/lib.rs:34:9
|
34 | for import in imports {
| ^^^^^^ help: if this is intentional, prefix it with an underscore: `_import`
|
= note: `#[warn(unused_variables)]` on by default
warning: unused variable: `current_node`
--> src/lib.rs:35:17
|
35 | let mut current_node = &mut root;
| ^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_current_node`
warning: unused variable: `order`
--> src/lib.rs:28:42
|
28 | pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
| ^^^^^ help: if this is intentional, prefix it with an underscore: `_order`
warning: variable does not need to be mutable
--> src/lib.rs:35:13
|
35 | let mut current_node = &mut root;
| ----^^^^^^^^^^^^
| |
| help: remove this `mut`
|
= note: `#[warn(unused_mut)]` on by default
warning: variable `importEntry` should have a snake case name
--> src/lib.rs:12:13
|
12 | let importEntry = import.0.join("::");
| ^^^^^^^^^^^ help: convert the identifier to snake case: `import_entry`
|
= note: `#[warn(non_snake_case)]` on by default
warning: `solution` (lib) generated 5 warnings
Finished test [unoptimized + debuginfo] target(s) in 1.30s
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_sorted ... ok
test solution_test::test_flat_original_duplicates ... FAILED
test solution_test::test_nested_basic ... FAILED
test solution_test::test_flat_sorted_duplicates ... FAILED
test solution_test::test_nested_empty ... ok
test solution_test::test_nested_deep ... FAILED
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_original_self ... FAILED
test solution_test::test_nested_sorted ... FAILED
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_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_nested_basic stdout ----
thread 'solution_test::test_nested_basic' panicked at 'assertion failed: `(left == right)`
left: `[]`,
right: `["std::{\n a,\n}\n"]`', tests/solution_test.rs:140:5
---- 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_deep stdout ----
thread 'solution_test::test_nested_deep' panicked at 'assertion failed: `(left == right)`
left: `[]`,
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: `[]`,
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: `[]`,
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: `[]`,
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: `[]`,
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: `[]`,
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_original_self stdout ----
thread 'solution_test::test_nested_original_self' panicked at 'assertion failed: `(left == right)`
left: `[]`,
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 stdout ----
thread 'solution_test::test_nested_sorted' panicked at 'assertion failed: `(left == right)`
left: `[]`,
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: `[]`,
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: `[]`,
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: `[]`,
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: `[]`,
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`
История (2 версии и 0 коментара)
Богдан качи решение на 03.12.2024 17:59 (преди 12 месеца)
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 res = vec![];
for import in imports {
let importEntry = import.0.join("::");
res.push(importEntry);
}
- print!("{:?}", res);
-
return match order {
Order::Original => res,
Order::Sorted => {
res.sort();
res
}
};
}
+struct Node { value: String, children: Vec<Node> }
+
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
- todo!()
-}
+ let mut root = Node {
+ value: String::new(),
+ children: vec![],
+ };
+
+ for import in imports {
+ let mut current_node = &mut root;
+
+ //Check if contains in child and ...
+
+ }
+
+ let mut result = vec![];
+ format_node(&root, 0, &mut result);
+ result
+ // let res_str = result.join("");
+ // res_str.pop() //Removing the last ,
+ // res_str
+}
+
+fn format_node(node: &Node, depth: usize, result: &mut Vec<String>) {
+ let indent = " ".repeat(depth);
+
+ if !node.value.is_empty() {
+ if !node.children.is_empty() {
+ result.push(format!("{}{}::{{\n", indent, node.value));
+ } else {
+ result.push(format!("{}{},\n", indent, node.value));
+ }
+ }
+
+ for child in &node.children {
+ format_node(child, depth + 1, result);
+ }
+
+ if !node.value.is_empty() && !node.children.is_empty() {
+ result.push(format!("{}}},\n", indent));
+ }
+}
