Решение на Форматиране на импорти от Ана Илиева

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

Към профила на Ана Илиева

Резултати

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

Код

use std::collections::{HashSet, BTreeMap};
pub struct Import<'a>(pub &'a [&'a str]);
#[derive(Clone)]
pub enum Order {
Original,
Sorted,
}
struct Module {
modules_so_far: Vec<String>,
children: BTreeMap<String, Module>,
}
impl Module {
fn new() -> Self {
Self {
modules_so_far: Vec::new(),
children: BTreeMap::new(),
}
}
fn insert_path(&mut self, path: &[&str]) {
if path.is_empty() {
return;
}
if path.len() == 1 {
let item = path[0].to_string();
if !self.modules_so_far.contains(&item) {
self.modules_so_far.push(item);
}
} else {
self.children.entry(path[0].to_string()).or_insert_with(Module::new).insert_path(&path[1..]);
}
}
fn sort(&mut self) {
self.modules_so_far.sort();
for submodule in self.children.values_mut() {
submodule.sort();
}
}
fn to_string(&self, indent: usize, order: Order) -> String {
let mut result: String = String::new();
let indent_str: String = " ".repeat(indent);
let mut entries: Vec<String> = self.modules_so_far.iter().cloned().collect();
entries.extend(self.children.keys().map(|name: &String| format!("{}::{{\n{}{}}}", name, self.children[name]
.to_string(indent + 4, order.clone()), indent_str))
);
if let Order::Sorted = order {
entries.sort();
}
for entry in entries {
result.push_str(&indent_str);
result.push_str(&entry);
result.push_str(",\n");
}
result
}
}
pub fn format_flat(imports: &[Import], order: Order) -> Vec<String> {
let mut unique_imports: HashSet<String> = HashSet::new();
let mut result_vec: Vec<String> = Vec::new();
for import in imports {
let formatted: String = import.0.join("::");
if unique_imports.insert(formatted.clone()) {
result_vec.push(formatted);
}
}
if let Order::Sorted = order {
result_vec.sort();
}
result_vec
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut tree: BTreeMap<&str, Module> = BTreeMap::new();
for import in imports {
let crate_name: &str = import.0[0];
let path: &[&str] = &import.0[1..];
tree.entry(crate_name).or_insert_with(Module::new).insert_path(path);
}
if let Order::Sorted = order {
for module in tree.values_mut() {
module.sort();
}
}
tree.into_iter().map(|(crate_name, module)| {
let mut result: String = String::new();
result.push_str(crate_name);
result.push_str("::{\n");
result.push_str(&module.to_string(4, order.clone()));
result.push('}');
result.push('\n');
result
})
.collect()
}

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

Compiling solution v0.1.0 (/tmp/d20241203-1739405-1gp8wpx/solution)
    Finished test [unoptimized + debuginfo] target(s) in 2.09s
     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 ... ok
test solution_test::test_nested_only_crate ... FAILED
test solution_test::test_nested_original ... FAILED
test solution_test::test_nested_original_duplicates ... FAILED
test solution_test::test_nested_original_2 ... FAILED
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_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    c,\n    a,\n    b::{\n        B2,\n        B1,\n    },\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_duplicates stdout ----
thread 'solution_test::test_nested_original_duplicates' panicked at 'assertion failed: `(left == right)`
  left: `["my_crate::{\n    c,\n    a,\n    b::{\n        B2,\n        B1,\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_2 stdout ----
thread 'solution_test::test_nested_original_2' panicked at 'assertion failed: `(left == right)`
  left: `["my_crate::{\n    c,\n    a::{\n        A1,\n        inner::{\n            I1,\n        },\n    },\n    b::{\n        B2,\n        B1,\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_self stdout ----
thread 'solution_test::test_nested_original_self' panicked at 'assertion failed: `(left == right)`
  left: `["my_crate::{\n    c,\n    a,\n    b,\n    b::{\n        B2,\n        B1,\n    },\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    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_self
    solution_test::test_nested_sorted_self

test result: FAILED. 14 passed; 6 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:40 (преди 9 месеца)