Решение на Форматиране на импорти от Василен Петков

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

Към профила на Василен Петков

Резултати

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

Код

use std::collections::{HashSet, HashMap};
use std::fmt::Write;
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::new();
let mut seen = HashSet::new();
for imp in imports {
let import_str = imp.0.join("::");
if !seen.contains(&import_str) {
seen.insert(import_str.clone());
unique_imports.push(import_str);
}
}
if let Order::Sorted = order {
unique_imports.sort();
}
unique_imports
}
#[derive(Default)]
struct TreeNode {
children: HashMap<String, TreeNode>,
}
impl TreeNode {
fn add_path(&mut self, path: &[&str]) {
if path.is_empty() {
return;
}
let (head, tail) = path.split_first().unwrap();
self.children
.entry(head.to_string())
.or_insert_with(TreeNode::default)
.add_path(tail);
}
fn format(&self, sorted: bool, depth: usize) -> String {
let mut result = String::new();
let mut entries: Vec<_> = self.children.iter().collect();
if sorted {
entries.sort_by_key(|(key, _)| *key);
}
for (key, child) in entries {
if child.children.is_empty() {
writeln!(result, "{}{},", " ".repeat(depth * 4), key).unwrap();
} else {
writeln!(result, "{}{}::{{", " ".repeat(depth * 4), key).unwrap();
let child_str = child.format(sorted, depth + 1);
result.push_str(&child_str);
writeln!(result, "{}}},", " ".repeat(depth * 4)).unwrap();
}
}
result
}
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut roots: HashMap<String, TreeNode> = HashMap::new();
for imp in imports {
let (crate_name, path) = imp.0.split_first().unwrap();
roots
.entry(crate_name.to_string())
.or_insert_with(TreeNode::default)
.add_path(path);
}
let sorted = matches!(order, Order::Sorted);
let mut result = Vec::new();
for (crate_name, tree) in &roots {
let mut crate_output = format!("{}::{{\n", crate_name);
crate_output.push_str(&tree.format(sorted, 1));
crate_output.push_str("}\n");
result.push(crate_output);
}
if sorted {
result.sort();
}
result
}

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

Compiling solution v0.1.0 (/tmp/d20241203-1739405-eut9y/solution)
    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_original_duplicates ... ok
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_empty ... ok
test solution_test::test_nested_only_crate ... FAILED
test solution_test::test_nested_deep ... ok
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 ... 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 ... ok
test solution_test::test_nested_sorted_self ... FAILED

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

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

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

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

Василен качи първо решение на 02.12.2024 23:50 (преди 9 месеца)