Решение на Форматиране на импорти от Силвия Земеделска
Към профила на Силвия Земеделска
Резултати
- 14 точки от тестове
- 0 бонус точки
- 14 точки общо
- 14 успешни тест(а)
- 6 неуспешни тест(а)
Код
pub struct Import<'a>(pub &'a [&'a str]);
impl<'a> Import<'a> {
pub fn to_vec(&self) -> Vec<String> {
let mut result = Vec::new();
for &s in self.0 {
result.push(s.to_string());
}
result
}
}
pub enum Order {
Original,
Sorted,
}
struct Tree { root: Node }
struct Node {
value: String,
children: Vec<Node>
}
impl Tree{
pub fn new() -> Self {
Tree {
root: Node {
value: "root".to_string(),
children: Vec::new(),
},
}
}
pub fn build_tree(& mut self, elements: &Vec<String>) {
self.root.value = elements[0].to_string();
self.root.children = Vec::new();
self.root.add_child(elements, 1);
}
}
impl Node {
pub fn new(val: String) -> Node {
let children: Vec<Node> = Vec::new();
Node {value: val, children: children}
}
pub fn print(&self, indentation: usize) -> Vec<String> {
let mut result = Vec::new();
let indent = " ".repeat(indentation);
if self.children.is_empty() {
result.push(format!("{}{},", indent, self.value));
} else {
result.push(format!("{}{}::{{", indent, self.value));
for node in self.children.iter() {
result.extend(node.print(indentation + 4));
}
if indentation != 0 {
result.push(format!("{}}},", indent));
}
else {
result.push("}\n".to_string());
}
}
let mut to_return:Vec<String> = Vec::new();
to_return.push(result.join("\n"));
to_return
}
pub fn add_child(&mut self, elements: &Vec<String>, mut counter: usize) {
if counter >= elements.len() {
return;
}
if elements[counter] == self.value {
counter = counter + 1;
}
let current_el = elements[counter].to_string();
for i in 0..self.children.len() {
if self.children[i].value == current_el {
self.children[i].add_child(elements, counter+1);
return;
}
}
let mut child = Node::new(current_el);
let mut current = &mut child;
for i in (counter + 1)..elements.len() {
let new_child = Node::new(elements[i].to_string());
current.children.push(new_child);
current = current.children.last_mut().unwrap();
}
self.children.push(child);
}
pub fn sort_tree(&mut self) {
self.children.sort_by(|a, b| {
if a.value == "self" {
std::cmp::Ordering::Less
} else if b.value == "self" {
std::cmp::Ordering::Greater
} else {
a.value.cmp(&b.value)
}
});
for child in &mut self.children {
child.sort_tree();
}
}
}
pub fn format_flat(imports: &[Import], order: Order) -> Vec<String> {
let mut result = Vec::new();
for import in imports.iter() {
let path = import.to_vec().join("::");
if !result.contains(&path) {
result.push(path);
}
}
if matches!(order, Order::Sorted) {
result.sort();
}
result
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut elements: Vec<Vec<String>> = Vec::new();
for import in imports {
elements.push(import.to_vec());
}
let mut tree = Tree::new();
tree.build_tree(&elements[0]);
for i in 1..elements.len() {
tree.root.add_child(&elements[i], 0);
}
match order {
Order::Original => {
}
Order::Sorted => {
tree.root.sort_tree();
}
}
tree.root.print(0)
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20241203-1739405-1a8esy3/solution) Finished test [unoptimized + debuginfo] target(s) in 1.73s 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 ... FAILED test solution_test::test_nested_only_crate ... FAILED test solution_test::test_nested_original ... ok test solution_test::test_nested_original_2 ... ok test solution_test::test_nested_original_duplicates ... ok 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 ... FAILED test solution_test::test_nested_sorted_self ... FAILED failures: ---- solution_test::test_nested_empty stdout ---- thread 'solution_test::test_nested_empty' panicked at 'index out of bounds: the len is 0 but the index is 0', src/lib.rs:142:22 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ---- solution_test::test_nested_only_crate stdout ---- thread 'solution_test::test_nested_only_crate' panicked at 'assertion failed: `(left == right)` left: `["my_crate,"]`, right: `["my_crate\n"]`', tests/solution_test.rs:132: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 b,\n std::{\n string::{\n String,\n },\n },\n a,\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 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_multi_crate stdout ---- thread 'solution_test::test_nested_sorted_multi_crate' panicked at 'assertion failed: `(left == right)` left: `["crate::{\n a,\n b,\n std::{\n string::{\n String,\n },\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_nested_empty solution_test::test_nested_only_crate 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. 14 passed; 6 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass `--test solution_test`
История (2 версии и 0 коментара)
Силвия качи решение на 03.12.2024 15:29 (преди 9 месеца)
pub struct Import<'a>(pub &'a [&'a str]);
impl<'a> Import<'a> {
pub fn to_vec(&self) -> Vec<String> {
let mut result = Vec::new();
for &s in self.0 {
- result.push(s.to_string());
- }
+ result.push(s.to_string());
+ }
result
}
}
pub enum Order {
Original,
Sorted,
}
struct Tree { root: Node }
struct Node {
value: String,
children: Vec<Node>
}
impl Tree{
pub fn new() -> Self {
Tree {
root: Node {
value: "root".to_string(),
children: Vec::new(),
},
}
}
pub fn build_tree(& mut self, elements: &Vec<String>) {
self.root.value = elements[0].to_string();
self.root.children = Vec::new();
self.root.add_child(elements, 1);
}
}
impl Node {
pub fn new(val: String) -> Node {
let children: Vec<Node> = Vec::new();
Node {value: val, children: children}
}
pub fn print(&self, indentation: usize) -> Vec<String> {
let mut result = Vec::new();
let indent = " ".repeat(indentation);
if self.children.is_empty() {
result.push(format!("{}{},", indent, self.value));
} else {
result.push(format!("{}{}::{{", indent, self.value));
for node in self.children.iter() {
result.extend(node.print(indentation + 4));
}
if indentation != 0 {
result.push(format!("{}}},", indent));
}
else {
result.push("}\n".to_string());
}
}
let mut to_return:Vec<String> = Vec::new();
to_return.push(result.join("\n"));
to_return
}
pub fn add_child(&mut self, elements: &Vec<String>, mut counter: usize) {
if counter >= elements.len() {
return;
}
if elements[counter] == self.value {
counter = counter + 1;
}
let current_el = elements[counter].to_string();
for i in 0..self.children.len() {
if self.children[i].value == current_el {
self.children[i].add_child(elements, counter+1);
return;
}
}
let mut child = Node::new(current_el);
let mut current = &mut child;
for i in (counter + 1)..elements.len() {
let new_child = Node::new(elements[i].to_string());
current.children.push(new_child);
current = current.children.last_mut().unwrap();
}
self.children.push(child);
}
pub fn sort_tree(&mut self) {
self.children.sort_by(|a, b| {
if a.value == "self" {
std::cmp::Ordering::Less
} else if b.value == "self" {
std::cmp::Ordering::Greater
} else {
a.value.cmp(&b.value)
}
});
for child in &mut self.children {
child.sort_tree();
}
}
}
pub fn format_flat(imports: &[Import], order: Order) -> Vec<String> {
let mut result = Vec::new();
for import in imports.iter() {
let path = import.to_vec().join("::");
if !result.contains(&path) {
result.push(path);
}
}
if matches!(order, Order::Sorted) {
result.sort();
}
result
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut elements: Vec<Vec<String>> = Vec::new();
for import in imports {
elements.push(import.to_vec());
}
let mut tree = Tree::new();
tree.build_tree(&elements[0]);
for i in 1..elements.len() {
tree.root.add_child(&elements[i], 0);
}
match order {
Order::Original => {
}
Order::Sorted => {
tree.root.sort_tree();
}
}
tree.root.print(0)
}