Решение на Форматиране на импорти от Петър Христов

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

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

Резултати

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

Код

pub struct Import<'a>(pub &'a [&'a str]);
pub enum Order {
Original,
Sorted,
}
impl<'a> Clone for Import<'a> {
fn clone(&self) -> Self {
Import(self.0)
}
}
pub fn input_format(imports: &mut Vec<Import<'_>>, order: &Order) {
if let Order::Sorted = order {
for i in (0..(imports.len() - 1)).rev() {
imports.sort_by_key(|import| import.0.get(i));
}
for i in 0..(imports.len() - 1) {
if i != imports.len() - 1 {
if imports[i].0 == imports[i + 1].0 {
imports.remove(i + 1);
}
}
}
} else {
for i in 0..(imports.len() - 1) {
for j in i + 1..(imports.len()) {
if imports[i].0 == imports[j].0 {
imports.remove(j);
}
}
}
}
}
pub fn format_flat(imports: &[Import], order: Order) -> Vec<String> {
let mut result: Vec<String> = Vec::new();
let mut imports = imports.to_vec();
input_format(&mut imports, &order);
for import in imports {
let mut line: String = String::new();
for value in import.0 {
line.push_str(value);
line.push_str("::");
}
line.truncate(line.len() - 2);
result.push(line);
}
result
}
struct Tree {
root: Node,
}
struct Node {
value: String,
children: Vec<Node>,
}
impl Tree {
fn new() -> Self {
Tree {
root: Node::new("root"),
}
}
fn add_child(&mut self, path: &[&str]) {
self.root.add_child(path);
}
fn print_rec(&self, node: &Node, result: &mut String, depth: usize) {
let mut str = format!("{}{}", " ".repeat(depth), node.value);
if !node.children.is_empty() {
str.push_str("::{\n");
result.push_str(str.as_str());
for child in &node.children {
self.print_rec(child, result, depth + 1);
}
if depth != 0 {
result.push_str(format!("{}{}", " ".repeat(depth), "},\n").as_str());
} else {
result.push_str("},\n");
}
} else {
str.push_str(",\n");
result.push_str(str.as_str());
for child in &node.children {
self.print_rec(child, result, depth + 1);
}
}
}
fn add_to_str(&self, result: &mut Vec<String>) {
for child in &self.root.children {
let mut str = String::new();
self.print_rec(&child, &mut str, 0);
str.pop();
str.pop();
str.push('\n');
result.push(str);
}
}
}
impl Node {
fn new(value: &str) -> Self {
Node {
value: value.to_string(),
children: Vec::new(),
}
}
fn add_child(&mut self, path: &[&str]) {
if let Some((first, rest)) = path.split_first() {
let mut found = false;
for child in &mut self.children {
if child.value == *first {
if !rest.is_empty() {
child.add_child(rest);
}
found = true;
break;
}
}
if !found {
let mut new_child = Node::new(first);
if !rest.is_empty() {
new_child.add_child(rest);
}
self.children.push(new_child);
}
}
}
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut result: Vec<String> = Vec::new();
let mut root = Tree::new();
let mut imports = imports.to_vec();
input_format(&mut imports, &order);
for import in imports {
root.add_child(import.0);
}
root.add_to_str(&mut result);
result
}

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

Compiling solution v0.1.0 (/tmp/d20241203-1739405-1emuwqg/solution)
    Finished test [unoptimized + debuginfo] target(s) in 1.10s
     Running tests/solution_test.rs (target/debug/deps/solution_test-1428e1090729d165)

running 20 tests
test solution_test::test_flat_multi_crate ... ok
test solution_test::test_flat_empty ... FAILED
test solution_test::test_flat_original ... ok
test solution_test::test_flat_original_duplicates ... FAILED
test solution_test::test_flat_sorted ... ok
test solution_test::test_nested_basic ... ok
test solution_test::test_flat_sorted_duplicates ... FAILED
test solution_test::test_nested_empty ... FAILED
test solution_test::test_nested_deep ... ok
test solution_test::test_nested_only_crate ... ok
test solution_test::test_nested_original ... ok
test solution_test::test_nested_original_2 ... ok
test solution_test::test_nested_original_duplicates ... FAILED
test solution_test::test_nested_original_multi_crate ... ok
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 ... FAILED
test solution_test::test_nested_sorted_self ... FAILED
test solution_test::test_nested_sorted_multi_crate ... ok

failures:

---- solution_test::test_flat_empty stdout ----
thread 'solution_test::test_flat_empty' panicked at 'attempt to subtract with overflow', src/lib.rs:28:21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- solution_test::test_flat_original_duplicates stdout ----
thread 'solution_test::test_flat_original_duplicates' panicked at 'index out of bounds: the len is 4 but the index is 4', src/lib.rs:30:36

---- solution_test::test_flat_sorted_duplicates stdout ----
thread 'solution_test::test_flat_sorted_duplicates' panicked at 'index out of bounds: the len is 4 but the index is 4', src/lib.rs:22:20

---- solution_test::test_nested_empty stdout ----
thread 'solution_test::test_nested_empty' panicked at 'attempt to subtract with overflow', src/lib.rs:28:21

---- solution_test::test_nested_original_duplicates stdout ----
thread 'solution_test::test_nested_original_duplicates' panicked at 'index out of bounds: the len is 4 but the index is 4', src/lib.rs:30:36

---- 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    },\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

---- solution_test::test_nested_sorted_duplicates stdout ----
thread 'solution_test::test_nested_sorted_duplicates' panicked at 'index out of bounds: the len is 4 but the index is 4', src/lib.rs:22:20

---- 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_empty
    solution_test::test_flat_original_duplicates
    solution_test::test_flat_sorted_duplicates
    solution_test::test_nested_empty
    solution_test::test_nested_original_duplicates
    solution_test::test_nested_original_self
    solution_test::test_nested_sorted_duplicates
    solution_test::test_nested_sorted_self

test result: FAILED. 12 passed; 8 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 17:25 (преди 10 месеца)