Решение на Форматиране на импорти от Йосиф Иванов

Обратно към всички решения

Към профила на Йосиф Иванов

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 10 успешни тест(а)
  • 10 неуспешни тест(а)

Код

use std::collections::{BTreeMap};
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 unique_imports: Vec<String> = imports
.iter()
.map(|import| import.0.join("::"))
.collect();
unique_imports.sort();
if let Order::Sorted = order {
unique_imports.dedup();
}
unique_imports
}
fn format_node(
name: &str,
node: &TreeNode,
indent: usize,
order: &Order,
is_last: bool,
) -> String {
let mut result = String::new();
let indent_str = " ".repeat(indent * 4);
if !node.children.is_empty() {
result.push_str(&format!("{}{}::{{\n", indent_str, name));
let mut children: Vec<(&String, &TreeNode)> = node.children.iter().collect();
if let Order::Sorted = order {
children.sort_by_key(|(key, _)| *key);
}
for (_i, (child_name, child_node)) in children.iter().enumerate() {
result.push_str(&format_node(
child_name,
child_node,
indent + 1,
order,
false,
));
}
result.push_str(&format!(
"{}}}{}\n",
indent_str,
if is_last { "" } else { "," }
));
} else {
result.push_str(&format!("{}{}{}\n", indent_str, name, if is_last { "" } else { "," }));
}
result
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut tree: BTreeMap<String, TreeNode> = BTreeMap::new();
for import in imports {
let mut current = &mut tree;
for segment in import.0.iter() {
current = &mut current
.entry((*segment).to_string())
.or_insert_with(TreeNode::new)
.children;
}
}
let mut result = vec![];
for (i, (key, node)) in tree.iter().enumerate() {
result.push(format_node(
key,
node,
0,
&order,
i == tree.len() - 1,
));
}
result
}
#[derive(Default)]
struct TreeNode {
children: BTreeMap<String, TreeNode>,
}
impl TreeNode {
fn new() -> Self {
Self {
children: BTreeMap::new(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_basic() {
let imports = &[
Import(&["my_crate", "a"]),
Import(&["my_crate", "d"]),
Import(&["my_crate", "b", "B1"]),
Import(&["my_crate", "b", "B2"]),
Import(&["my_crate", "c"]),
];
assert_eq!(
format_flat(imports, Order::Sorted),
&[
"my_crate::a",
"my_crate::b::B1",
"my_crate::b::B2",
"my_crate::c",
"my_crate::d",
]
);
assert_eq!(
format_nested(imports, Order::Sorted),
&[
concat!(
"my_crate::{\n",
" a,\n",
" b::{\n",
" B1,\n",
" B2,\n",
" },\n",
" c,\n",
" d,\n",
"}\n",
)
]
);
}
}

Лог от изпълнението

Compiling solution v0.1.0 (/tmp/d20241203-1739405-1hxbx0a/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 ... FAILED
test solution_test::test_flat_original ... FAILED
test solution_test::test_flat_original_duplicates ... FAILED
test solution_test::test_flat_sorted ... ok
test solution_test::test_flat_sorted_duplicates ... ok
test solution_test::test_nested_basic ... ok
test solution_test::test_nested_deep ... ok
test solution_test::test_nested_empty ... ok
test solution_test::test_nested_only_crate ... ok
test solution_test::test_nested_original ... FAILED
test solution_test::test_nested_original_2 ... FAILED
test solution_test::test_nested_original_multi_crate ... FAILED
test solution_test::test_nested_original_duplicates ... FAILED
test solution_test::test_nested_sorted ... ok
test solution_test::test_nested_original_self ... FAILED
test solution_test::test_nested_sorted_2 ... ok
test solution_test::test_nested_sorted_duplicates ... ok
test solution_test::test_nested_sorted_multi_crate ... FAILED
test solution_test::test_nested_sorted_self ... FAILED

failures:

---- solution_test::test_flat_multi_crate stdout ----
thread 'solution_test::test_flat_multi_crate' panicked at 'assertion failed: `(left == right)`
  left: `["bar::string::String", "foo::string::String", "std::string::String"]`,
 right: `["std::string::String", "foo::string::String", "bar::string::String"]`', tests/solution_test.rs:107:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- solution_test::test_flat_original stdout ----
thread 'solution_test::test_flat_original' panicked at 'assertion failed: `(left == right)`
  left: `["my_crate::a", "my_crate::b", "my_crate::c::C1", "my_crate::c::C2", "my_crate::x::y::z::W1"]`,
 right: `["my_crate::c::C1", "my_crate::b", "my_crate::x::y::z::W1", "my_crate::a", "my_crate::c::C2"]`', tests/solution_test.rs:21:5

---- solution_test::test_flat_original_duplicates stdout ----
thread 'solution_test::test_flat_original_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::string::String", "std::iter::once", "std::iter", "std::iter::repeat"]`', tests/solution_test.rs:44:5

---- solution_test::test_nested_original stdout ----
thread 'solution_test::test_nested_original' panicked at 'assertion failed: `(left == right)`
  left: `["my_crate::{\n    a,\n    b::{\n        B1,\n        B2,\n    },\n    c,\n}\n"]`,
 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    a::{\n        A1,\n        inner::{\n            I1,\n        },\n    },\n    b::{\n        B1,\n        B2,\n    },\n    c,\n}\n"]`,
 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_multi_crate stdout ----
thread 'solution_test::test_nested_original_multi_crate' panicked at 'assertion failed: `(left == right)`
  left: `["crate::{\n    a,\n    b,\n},\n", "std::{\n    string::{\n        String,\n    },\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_original_duplicates stdout ----
thread 'solution_test::test_nested_original_duplicates' panicked at 'assertion failed: `(left == right)`
  left: `["my_crate::{\n    a,\n    b::{\n        B1,\n        B2,\n    },\n    c,\n}\n"]`,
 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_self stdout ----
thread 'solution_test::test_nested_original_self' panicked at 'assertion failed: `(left == right)`
  left: `["my_crate::{\n    a,\n    b::{\n        B1,\n        B2,\n    },\n    c,\n}\n"]`,
 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_multi_crate stdout ----
thread 'solution_test::test_nested_sorted_multi_crate' panicked at 'assertion failed: `(left == right)`
  left: `["crate::{\n    a,\n    b,\n},\n", "std::{\n    string::{\n        String,\n    },\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        B1,\n        B2,\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_flat_multi_crate
    solution_test::test_flat_original
    solution_test::test_flat_original_duplicates
    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_multi_crate
    solution_test::test_nested_sorted_self

test result: FAILED. 10 passed; 10 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass `--test solution_test`

История (1 версия и 0 коментара)

Йосиф качи първо решение на 03.12.2024 16:04 (преди 9 месеца)