Решение на Форматиране на импорти от Стоян Дарджиков

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

Към профила на Стоян Дарджиков

Резултати

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

Код

use std::collections::HashSet;
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 already_used_imports = HashSet::new();
let mut full_imports = Vec::new();
for import in imports.iter() {
let mut full_import = String::new();
for str in import.0 {
if !full_import.is_empty() {
full_import.push_str("::");
}
full_import.push_str(str);
}
if already_used_imports.insert(full_import.clone()) {
full_imports.push(full_import);
}
}
match order {
Order::Original => full_imports,
Order::Sorted => {
full_imports.sort();
full_imports
}
}
}
struct Tree {
root: Node
}
struct Node {
value: String,
children: Vec<Node>,
is_self: bool
}
impl Tree {
fn new() -> Tree {
Tree {root: Node {
value: String::new(),
children: Vec::new(),
is_self: false
}}
}
fn flatten_into_vec(&mut self, order: Order, mut vec: Vec<String>) -> Vec<String> {
match order {
Order::Original => (),
Order::Sorted => self.root.children.sort_by(|a, b| a.value.cmp(&b.value)),
}
for import_base in &mut self.root.children {
let mut str = import_base.flatten(0, &order);
str.pop();
str.pop();
str.push('\n');
vec.push(str);
}
vec
}
}
impl Node {
pub fn add_child(&mut self, child_value: String, is_self: bool) -> usize {
let new_child = Node {
value: child_value,
children: Vec::new(),
is_self
};
self.children.push(new_child);
let len = self.children.len();
return len - 1;
}
pub fn find_child(&self, child_value: String) -> Option<usize> {
for (index, child) in self.children.iter().enumerate() {
if (child.value == child_value) {
return Some(index);
}
}
None
}
pub fn flatten(&mut self, depth: i32, order: &Order) -> String {
let mut str = String::new();
for i in 0..depth {
str.push_str(" ");
}
if (self.children.len() == 0) {
str.push_str(&self.value);
str.push_str(",\n");
str
} else {
str.push_str(&self.value);
str.push_str("::{\n");
match order {
Order::Original => (),
Order::Sorted => self.children.sort_by(|a, b| a.value.cmp(&b.value)),
};
if (self.is_self) {
self.children.insert(0, Node {value: "self".to_string(), children: Vec::new(), is_self: false});
}
for child in &mut self.children {
str.push_str(&child.flatten(depth + 1, order));
}
for i in 0..depth {
str.push_str(" ");
}
str.push_str("},\n");
str
}
}
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut result_tree = Tree::new();
for import in imports {
let mut currNode: &mut Node = &mut result_tree.root;
for (index, import_token) in import.0.iter().enumerate() {
let existing_child_index_option = currNode.find_child(import_token.to_string());
let next_node_index = match existing_child_index_option {
Some(index) => index,
None => currNode.add_child(import_token.to_string(), index == import.0.len() - 1)
};
currNode = &mut currNode.children[next_node_index];
if (index == import.0.len() - 1) {
currNode.is_self = true;
}
}
}
let mut full_imports = Vec::new();
result_tree.flatten_into_vec(order, full_imports)
}

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

Compiling solution v0.1.0 (/tmp/d20241203-1739405-d8x4at/solution)
warning: unnecessary parentheses around `if` condition
  --> src/lib.rs:86:16
   |
86 |             if (child.value == child_value) {
   |                ^                          ^
   |
   = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
   |
86 -             if (child.value == child_value) {
86 +             if child.value == child_value {
   |

warning: unnecessary parentheses around `if` condition
  --> src/lib.rs:99:12
   |
99 |         if (self.children.len() == 0) {
   |            ^                        ^
   |
help: remove these parentheses
   |
99 -         if (self.children.len() == 0) {
99 +         if self.children.len() == 0 {
   |

warning: unnecessary parentheses around `if` condition
   --> src/lib.rs:110:16
    |
110 |             if (self.is_self) {
    |                ^            ^
    |
help: remove these parentheses
    |
110 -             if (self.is_self) {
110 +             if self.is_self {
    |

warning: unnecessary parentheses around `if` condition
   --> src/lib.rs:136:16
    |
136 |             if (index == import.0.len() - 1) {
    |                ^                           ^
    |
help: remove these parentheses
    |
136 -             if (index == import.0.len() - 1) {
136 +             if index == import.0.len() - 1 {
    |

warning: unused variable: `i`
  --> src/lib.rs:96:13
   |
96 |         for i in 0..depth {
   |             ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `i`
   --> src/lib.rs:116:17
    |
116 |             for i in 0..depth {
    |                 ^ help: if this is intentional, prefix it with an underscore: `_i`

warning: variable does not need to be mutable
   --> src/lib.rs:142:9
    |
142 |     let mut full_imports = Vec::new();
    |         ----^^^^^^^^^^^^
    |         |
    |         help: remove this `mut`
    |
    = note: `#[warn(unused_mut)]` on by default

warning: variable `currNode` should have a snake case name
   --> src/lib.rs:128:17
    |
128 |         let mut currNode: &mut Node = &mut result_tree.root;
    |                 ^^^^^^^^ help: convert the identifier to snake case: `curr_node`
    |
    = note: `#[warn(non_snake_case)]` on by default

warning: `solution` (lib) generated 8 warnings
    Finished test [unoptimized + debuginfo] target(s) in 1.31s
     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_deep ... ok
test solution_test::test_nested_empty ... ok
test solution_test::test_nested_only_crate ... ok
test solution_test::test_nested_original_2 ... ok
test solution_test::test_nested_original ... ok
test solution_test::test_nested_original_duplicates ... ok
test solution_test::test_nested_original_multi_crate ... ok
test solution_test::test_nested_original_self ... ok
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 ... ok

test result: ok. 20 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

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

Стоян качи първо решение на 02.12.2024 22:44 (преди 9 месеца)

Стоян качи решение на 02.12.2024 23:06 (преди 9 месеца)

use std::collections::HashSet;
-
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 already_used_imports = HashSet::new();
let mut full_imports = Vec::new();
for import in imports.iter() {
let mut full_import = String::new();
for str in import.0 {
if !full_import.is_empty() {
full_import.push_str("::");
}
full_import.push_str(str);
}
if already_used_imports.insert(full_import.clone()) {
full_imports.push(full_import);
}
}
match order {
Order::Original => full_imports,
Order::Sorted => {
full_imports.sort();
full_imports
}
}
}
struct Tree {
root: Node
}
struct Node {
value: String,
- children: Vec<Node>
+ children: Vec<Node>,
+ is_self: bool
}
impl Tree {
fn new() -> Tree {
Tree {root: Node {
value: String::new(),
children: Vec::new(),
+ is_self: false
}}
}
fn flatten_into_vec(&mut self, order: Order, mut vec: Vec<String>) -> Vec<String> {
match order {
Order::Original => (),
Order::Sorted => self.root.children.sort_by(|a, b| a.value.cmp(&b.value)),
}
for import_base in &mut self.root.children {
vec.push(import_base.flatten(0, &order));
}
vec
}
}
impl Node {
- pub fn add_child(&mut self, child_value: String) -> usize {
+ pub fn add_child(&mut self, child_value: String, is_self: bool) -> usize {
+
+ if is_self {
+ print!("Adding self {}\n", child_value);
+ }
let new_child = Node {
value: child_value,
- children: Vec::new()
+ children: Vec::new(),
+ is_self
};
self.children.push(new_child);
let len = self.children.len();
return len - 1;
}
pub fn find_child(&self, child_value: String) -> Option<usize> {
for (index, child) in self.children.iter().enumerate() {
if (child.value == child_value) {
return Some(index);
}
}
None
}
pub fn flatten(&mut self, depth: i32, order: &Order) -> String {
let mut str = String::new();
for i in 0..depth {
str.push_str(" ");
}
if (self.children.len() == 0) {
str.push_str(&self.value);
str.push_str(",\n");
str
} else {
str.push_str(&self.value);
str.push_str("::{\n");
match order {
Order::Original => (),
Order::Sorted => self.children.sort_by(|a, b| a.value.cmp(&b.value)),
};
+ if (self.is_self) {
+ self.children.insert(0, Node {value: "self".to_string(), children: Vec::new(), is_self: false});
+ }
for child in &mut self.children {
str.push_str(&child.flatten(depth + 1, order));
}
for i in 0..depth {
str.push_str(" ");
}
str.push_str("},\n");
str
}
}
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut result_tree = Tree::new();
for import in imports {
let mut currNode: &mut Node = &mut result_tree.root;
- for import_token in import.0 {
+ for (index, import_token) in import.0.iter().enumerate() {
let existing_child_index_option = currNode.find_child(import_token.to_string());
let next_node_index = match existing_child_index_option {
Some(index) => index,
- None => currNode.add_child(import_token.to_string())
+ None => currNode.add_child(import_token.to_string(), index == import.0.len() - 1)
};
currNode = &mut currNode.children[next_node_index];
+ if (index == import.0.len() - 1) {
+ currNode.is_self = true;
+ }
}
}
let mut full_imports = Vec::new();
result_tree.flatten_into_vec(order, full_imports)
}

Стоян качи решение на 02.12.2024 23:07 (преди 9 месеца)

use std::collections::HashSet;
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 already_used_imports = HashSet::new();
let mut full_imports = Vec::new();
for import in imports.iter() {
let mut full_import = String::new();
for str in import.0 {
if !full_import.is_empty() {
full_import.push_str("::");
}
full_import.push_str(str);
}
if already_used_imports.insert(full_import.clone()) {
full_imports.push(full_import);
}
}
match order {
Order::Original => full_imports,
Order::Sorted => {
full_imports.sort();
full_imports
}
}
}
struct Tree {
root: Node
}
struct Node {
value: String,
children: Vec<Node>,
is_self: bool
}
impl Tree {
fn new() -> Tree {
Tree {root: Node {
value: String::new(),
children: Vec::new(),
is_self: false
}}
}
fn flatten_into_vec(&mut self, order: Order, mut vec: Vec<String>) -> Vec<String> {
match order {
Order::Original => (),
Order::Sorted => self.root.children.sort_by(|a, b| a.value.cmp(&b.value)),
}
for import_base in &mut self.root.children {
vec.push(import_base.flatten(0, &order));
}
vec
}
}
impl Node {
pub fn add_child(&mut self, child_value: String, is_self: bool) -> usize {
-
- if is_self {
- print!("Adding self {}\n", child_value);
- }
let new_child = Node {
value: child_value,
children: Vec::new(),
is_self
};
self.children.push(new_child);
let len = self.children.len();
return len - 1;
}
pub fn find_child(&self, child_value: String) -> Option<usize> {
for (index, child) in self.children.iter().enumerate() {
if (child.value == child_value) {
return Some(index);
}
}
None
}
pub fn flatten(&mut self, depth: i32, order: &Order) -> String {
let mut str = String::new();
for i in 0..depth {
str.push_str(" ");
}
if (self.children.len() == 0) {
str.push_str(&self.value);
str.push_str(",\n");
str
} else {
str.push_str(&self.value);
str.push_str("::{\n");
match order {
Order::Original => (),
Order::Sorted => self.children.sort_by(|a, b| a.value.cmp(&b.value)),
};
if (self.is_self) {
self.children.insert(0, Node {value: "self".to_string(), children: Vec::new(), is_self: false});
}
for child in &mut self.children {
str.push_str(&child.flatten(depth + 1, order));
}
for i in 0..depth {
str.push_str(" ");
}
str.push_str("},\n");
str
}
}
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut result_tree = Tree::new();
for import in imports {
let mut currNode: &mut Node = &mut result_tree.root;
for (index, import_token) in import.0.iter().enumerate() {
let existing_child_index_option = currNode.find_child(import_token.to_string());
let next_node_index = match existing_child_index_option {
Some(index) => index,
None => currNode.add_child(import_token.to_string(), index == import.0.len() - 1)
};
currNode = &mut currNode.children[next_node_index];
if (index == import.0.len() - 1) {
currNode.is_self = true;
}
}
}
let mut full_imports = Vec::new();
result_tree.flatten_into_vec(order, full_imports)
}

Стоян качи решение на 02.12.2024 23:15 (преди 9 месеца)

use std::collections::HashSet;
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 already_used_imports = HashSet::new();
let mut full_imports = Vec::new();
for import in imports.iter() {
let mut full_import = String::new();
for str in import.0 {
if !full_import.is_empty() {
full_import.push_str("::");
}
full_import.push_str(str);
}
if already_used_imports.insert(full_import.clone()) {
full_imports.push(full_import);
}
}
match order {
Order::Original => full_imports,
Order::Sorted => {
full_imports.sort();
full_imports
}
}
}
struct Tree {
root: Node
}
struct Node {
value: String,
children: Vec<Node>,
is_self: bool
}
impl Tree {
fn new() -> Tree {
Tree {root: Node {
value: String::new(),
children: Vec::new(),
is_self: false
}}
}
fn flatten_into_vec(&mut self, order: Order, mut vec: Vec<String>) -> Vec<String> {
match order {
Order::Original => (),
Order::Sorted => self.root.children.sort_by(|a, b| a.value.cmp(&b.value)),
}
for import_base in &mut self.root.children {
- vec.push(import_base.flatten(0, &order));
+ let mut str = import_base.flatten(0, &order);
+ str.pop();
+ str.pop();
+ str.push('\n');
+ vec.push(str);
}
vec
}
}
impl Node {
pub fn add_child(&mut self, child_value: String, is_self: bool) -> usize {
+
+ if is_self {
+ print!("Adding self {}\n", child_value);
+ }
let new_child = Node {
value: child_value,
children: Vec::new(),
is_self
};
self.children.push(new_child);
let len = self.children.len();
return len - 1;
}
pub fn find_child(&self, child_value: String) -> Option<usize> {
for (index, child) in self.children.iter().enumerate() {
if (child.value == child_value) {
return Some(index);
}
}
None
}
pub fn flatten(&mut self, depth: i32, order: &Order) -> String {
let mut str = String::new();
for i in 0..depth {
str.push_str(" ");
}
if (self.children.len() == 0) {
str.push_str(&self.value);
str.push_str(",\n");
str
} else {
str.push_str(&self.value);
str.push_str("::{\n");
match order {
Order::Original => (),
Order::Sorted => self.children.sort_by(|a, b| a.value.cmp(&b.value)),
};
if (self.is_self) {
self.children.insert(0, Node {value: "self".to_string(), children: Vec::new(), is_self: false});
}
for child in &mut self.children {
str.push_str(&child.flatten(depth + 1, order));
}
for i in 0..depth {
str.push_str(" ");
}
str.push_str("},\n");
str
}
}
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut result_tree = Tree::new();
for import in imports {
let mut currNode: &mut Node = &mut result_tree.root;
for (index, import_token) in import.0.iter().enumerate() {
let existing_child_index_option = currNode.find_child(import_token.to_string());
let next_node_index = match existing_child_index_option {
Some(index) => index,
None => currNode.add_child(import_token.to_string(), index == import.0.len() - 1)
};
currNode = &mut currNode.children[next_node_index];
if (index == import.0.len() - 1) {
currNode.is_self = true;
}
}
}
let mut full_imports = Vec::new();
result_tree.flatten_into_vec(order, full_imports)
}

Стоян качи решение на 02.12.2024 23:15 (преди 9 месеца)

use std::collections::HashSet;
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 already_used_imports = HashSet::new();
let mut full_imports = Vec::new();
for import in imports.iter() {
let mut full_import = String::new();
for str in import.0 {
if !full_import.is_empty() {
full_import.push_str("::");
}
full_import.push_str(str);
}
if already_used_imports.insert(full_import.clone()) {
full_imports.push(full_import);
}
}
match order {
Order::Original => full_imports,
Order::Sorted => {
full_imports.sort();
full_imports
}
}
}
struct Tree {
root: Node
}
struct Node {
value: String,
children: Vec<Node>,
is_self: bool
}
impl Tree {
fn new() -> Tree {
Tree {root: Node {
value: String::new(),
children: Vec::new(),
is_self: false
}}
}
fn flatten_into_vec(&mut self, order: Order, mut vec: Vec<String>) -> Vec<String> {
match order {
Order::Original => (),
Order::Sorted => self.root.children.sort_by(|a, b| a.value.cmp(&b.value)),
}
for import_base in &mut self.root.children {
let mut str = import_base.flatten(0, &order);
str.pop();
str.pop();
str.push('\n');
vec.push(str);
}
vec
}
}
impl Node {
pub fn add_child(&mut self, child_value: String, is_self: bool) -> usize {
-
- if is_self {
- print!("Adding self {}\n", child_value);
- }
let new_child = Node {
value: child_value,
children: Vec::new(),
is_self
};
self.children.push(new_child);
let len = self.children.len();
return len - 1;
}
pub fn find_child(&self, child_value: String) -> Option<usize> {
for (index, child) in self.children.iter().enumerate() {
if (child.value == child_value) {
return Some(index);
}
}
None
}
pub fn flatten(&mut self, depth: i32, order: &Order) -> String {
let mut str = String::new();
for i in 0..depth {
str.push_str(" ");
}
if (self.children.len() == 0) {
str.push_str(&self.value);
str.push_str(",\n");
str
} else {
str.push_str(&self.value);
str.push_str("::{\n");
match order {
Order::Original => (),
Order::Sorted => self.children.sort_by(|a, b| a.value.cmp(&b.value)),
};
if (self.is_self) {
self.children.insert(0, Node {value: "self".to_string(), children: Vec::new(), is_self: false});
}
for child in &mut self.children {
str.push_str(&child.flatten(depth + 1, order));
}
for i in 0..depth {
str.push_str(" ");
}
str.push_str("},\n");
str
}
}
}
pub fn format_nested(imports: &[Import], order: Order) -> Vec<String> {
let mut result_tree = Tree::new();
for import in imports {
let mut currNode: &mut Node = &mut result_tree.root;
for (index, import_token) in import.0.iter().enumerate() {
let existing_child_index_option = currNode.find_child(import_token.to_string());
let next_node_index = match existing_child_index_option {
Some(index) => index,
None => currNode.add_child(import_token.to_string(), index == import.0.len() - 1)
};
currNode = &mut currNode.children[next_node_index];
if (index == import.0.len() - 1) {
currNode.is_self = true;
}
}
}
let mut full_imports = Vec::new();
result_tree.flatten_into_vec(order, full_imports)
}