Решение на Форматиране на импорти от Памела Славчева
Към профила на Памела Славчева
Резултати
- 18 точки от тестове
- 0 бонус точки
- 18 точки общо
- 18 успешни тест(а)
- 2 неуспешни тест(а)
Код
use std::collections::HashSet;
struct Tree { root: Node }
#[derive(PartialEq, PartialOrd, Eq, Ord, Clone)]
struct Node { value: String, children: Vec<Node> }
#[derive(PartialEq, PartialOrd, Eq, Ord, Clone)]
pub struct Import<'a>(pub &'a [&'a str]);
pub enum Order {
Original,
Sorted,
}
impl Node {
fn new(value: String) -> Node
{
Node { value, children: Vec::new() }
}
}
impl Tree {
fn new(root: Node) -> Tree {
Tree { root }
}
fn insert_import(&mut self, import: &Import) -> ()
{
let mut current_node = &mut self.root;
for i in 1..import.0.len()
{
let mut child_index = 0;
let mut found: bool = false;
for j in 0..current_node.children.len()
{
if current_node.children[j].value == import.0[i]
{
child_index = j;
found = true;
break;
}
}
if !found
{
current_node.children.push(Node::new(import.0[i].to_string()));
child_index = current_node.children.len() - 1;
}
current_node = &mut current_node.children[child_index];
}
if import.0.len() == 2
{
let mut has_self = false;
for i in 0..current_node.children.len()
{
if current_node.children[i].value == "self".to_string()
{
has_self = true;
break;
}
}
if !has_self
{
current_node.children.push(Node::new(String::from("self")));
}
}
}
}
fn recursive_read(node: &Node, depth: u64) -> String
{
let mut result: String = String::new();
for _i in 0..depth
{
result.push_str(" ");
}
if node.children.len() > 1 || (node.children.len() == 1 && node.children[0].value != "self")
{
result.push_str(&node.value);
result.push_str("::");
result.push_str("{\n");
for child in &node.children
{
result.push_str(&recursive_read(&child, depth + 1));
}
for _i in 0..depth
{
result.push_str(" ");
}
if depth > 0
{
result.push_str("},\n");
}
else
{
result.push_str("}\n");
}
}
else
{
result.push_str(&node.value);
result.push_str(",\n");
}
result
}
pub fn format_flat(imports: &[Import], order: Order) -> Vec<String> {
let mut set:HashSet<String> = HashSet::new();
let mut result: Vec<String> = Vec::new();
for import in imports {
let mut temp:String = String::new();
for word in import.0 {
temp.push_str(word);
temp.push_str("::");
}
let temp = temp.trim_end_matches("::").to_string();
if !set.contains(&temp)
{
result.push(temp.clone());
set.insert(temp);
}
}
match order {
Order::Original => result,
Order::Sorted => {result.sort(); result}
}
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut imports_copy: Vec<Import> = imports.to_vec();
match order
{
Order::Sorted => imports_copy.sort(),
Order::Original => {}
}
let mut trees: Vec<Tree> = Vec::new();
for import in imports_copy {
let mut found: bool = false;
for tree in &mut trees
{
if import.0[0] == tree.root.value
{
tree.insert_import(&import);
found = true;
break;
}
}
if !found
{
trees.push(Tree::new(Node::new(import.0[0].to_string())));
let temp = trees.len()-1;
trees[temp].insert_import(&import);
}
}
let mut result:Vec<String> = vec![];
for tree in trees
{
result.push(recursive_read(&tree.root, 0));
}
result
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20241203-1739405-1rki3mc/solution) Finished test [unoptimized + debuginfo] target(s) in 1.38s 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 ... ok test solution_test::test_nested_basic ... ok test solution_test::test_flat_sorted_duplicates ... ok test solution_test::test_nested_empty ... ok test solution_test::test_nested_deep ... ok test solution_test::test_nested_original ... ok test solution_test::test_nested_only_crate ... FAILED test solution_test::test_nested_original_duplicates ... ok test solution_test::test_nested_original_2 ... ok test solution_test::test_nested_original_multi_crate ... ok test solution_test::test_nested_original_self ... FAILED test solution_test::test_nested_sorted ... ok test solution_test::test_nested_sorted_duplicates ... ok test solution_test::test_nested_sorted_2 ... ok test solution_test::test_nested_sorted_multi_crate ... ok test solution_test::test_nested_sorted_self ... ok failures: ---- 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 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ---- 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,\n b::{\n B2,\n B1,\n self,\n },\n a,\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 failures: solution_test::test_nested_only_crate solution_test::test_nested_original_self test result: FAILED. 18 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass `--test solution_test`