Skip to content

Commit

Permalink
Merge branch 'master' into ref/dp/rod_cutting
Browse files Browse the repository at this point in the history
  • Loading branch information
vil02 authored Aug 6, 2024
2 parents 001d40c + 3cbb841 commit d2299cd
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 75 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ unwrap_in_result = { level = "allow", priority = 1 }
unwrap_used = { level = "allow", priority = 1 }
use_debug = { level = "allow", priority = 1 }
wildcard_enum_match_arm = { level = "allow", priority = 1 }
renamed_function_params = { level = "allow", priority = 1 }
# nursery-lints:
branches_sharing_code = { level = "allow", priority = 1 }
cognitive_complexity = { level = "allow", priority = 1 }
Expand All @@ -160,5 +161,9 @@ redundant_clone = { level = "allow", priority = 1 }
suboptimal_flops = { level = "allow", priority = 1 }
suspicious_operation_groupings = { level = "allow", priority = 1 }
use_self = { level = "allow", priority = 1 }
while_float = { level = "allow", priority = 1 }
needless_pass_by_ref_mut = { level = "allow", priority = 1 }
# cargo-lints:
cargo_common_metadata = { level = "allow", priority = 1 }
# style-lints:
doc_lazy_continuation = { level = "allow", priority = 1 }
2 changes: 1 addition & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
* [Graph](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/graph.rs)
* [Hash Table](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/hash_table.rs)
* [Heap](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/heap.rs)
* [Infix To Postfix](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/infix_to_postfix.rs)
* [Lazy Segment Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/lazy_segment_tree.rs)
* [Linked List](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/linked_list.rs)
* [Postfix Evaluation](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/postfix_evaluation.rs)
Expand Down Expand Up @@ -203,6 +202,7 @@
* [Geometric Series](https://github.com/TheAlgorithms/Rust/blob/master/src/math/geometric_series.rs)
* [Greatest Common Divisor](https://github.com/TheAlgorithms/Rust/blob/master/src/math/greatest_common_divisor.rs)
* [Huber Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/math/huber_loss.rs)
* [Infix To Postfix](https://github.com/TheAlgorithms/Rust/blob/master/src/math/infix_to_postfix.rs)
* [Interest](https://github.com/TheAlgorithms/Rust/blob/master/src/math/interest.rs)
* [Interpolation](https://github.com/TheAlgorithms/Rust/blob/master/src/math/interpolation.rs)
* [Interquartile Range](https://github.com/TheAlgorithms/Rust/blob/master/src/math/interquartile_range.rs)
Expand Down
72 changes: 0 additions & 72 deletions src/data_structures/infix_to_postfix.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/data_structures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ mod floyds_algorithm;
pub mod graph;
mod hash_table;
mod heap;
mod infix_to_postfix;
mod lazy_segment_tree;
mod linked_list;
mod postfix_evaluation;
Expand All @@ -31,7 +30,6 @@ pub use self::graph::DirectedGraph;
pub use self::graph::UndirectedGraph;
pub use self::hash_table::HashTable;
pub use self::heap::Heap;
pub use self::infix_to_postfix::infix_to_postfix;
pub use self::lazy_segment_tree::LazySegmentTree;
pub use self::linked_list::LinkedList;
pub use self::postfix_evaluation::evaluate_postfix;
Expand Down
94 changes: 94 additions & 0 deletions src/math/infix_to_postfix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InfixToPostfixError {
UnknownCharacter(char),
UnmatchedParent,
}

/// Function to convert [infix expression](https://en.wikipedia.org/wiki/Infix_notation) to [postfix expression](https://en.wikipedia.org/wiki/Reverse_Polish_notation)
pub fn infix_to_postfix(infix: &str) -> Result<String, InfixToPostfixError> {
let mut postfix = String::new();
let mut stack: Vec<char> = Vec::new();

// Define the precedence of operators
let precedence = |op: char| -> u8 {
match op {
'+' | '-' => 1,
'*' | '/' => 2,
'^' => 3,
_ => 0,
}
};

for token in infix.chars() {
match token {
c if c.is_alphanumeric() => {
postfix.push(c);
}
'(' => {
stack.push('(');
}
')' => {
while let Some(top) = stack.pop() {
if top == '(' {
break;
}
postfix.push(top);
}
}
'+' | '-' | '*' | '/' | '^' => {
while let Some(top) = stack.last() {
if *top == '(' || precedence(*top) < precedence(token) {
break;
}
postfix.push(stack.pop().unwrap());
}
stack.push(token);
}
other => return Err(InfixToPostfixError::UnknownCharacter(other)),
}
}

while let Some(top) = stack.pop() {
if top == '(' {
return Err(InfixToPostfixError::UnmatchedParent);
}

postfix.push(top);
}

Ok(postfix)
}

#[cfg(test)]
mod tests {
use super::*;

macro_rules! test_infix_to_postfix {
($($name:ident: $inputs:expr,)*) => {
$(
#[test]
fn $name() {
let (infix, expected) = $inputs;
assert_eq!(infix_to_postfix(infix), expected)
}
)*
}
}

test_infix_to_postfix! {
single_symbol: ("x", Ok(String::from("x"))),
simple_sum: ("x+y", Ok(String::from("xy+"))),
multiply_sum_left: ("x*(y+z)", Ok(String::from("xyz+*"))),
multiply_sum_right: ("(x+y)*z", Ok(String::from("xy+z*"))),
multiply_two_sums: ("(a+b)*(c+d)", Ok(String::from("ab+cd+*"))),
product_and_power: ("a*b^c", Ok(String::from("abc^*"))),
power_and_product: ("a^b*c", Ok(String::from("ab^c*"))),
product_of_powers: ("(a*b)^c", Ok(String::from("ab*c^"))),
product_in_exponent: ("a^(b*c)", Ok(String::from("abc*^"))),
regular_0: ("a-b+c-d*e", Ok(String::from("ab-c+de*-"))),
regular_1: ("a*(b+c)+d/(e+f)", Ok(String::from("abc+*def+/+"))),
regular_2: ("(a-b+c)*(d+e*f)", Ok(String::from("ab-c+def*+*"))),
unknown_character: ("(a-b)*#", Err(InfixToPostfixError::UnknownCharacter('#'))),
unmatched_paren: ("((a-b)", Err(InfixToPostfixError::UnmatchedParent)),
}
}
2 changes: 2 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mod gcd_of_n_numbers;
mod geometric_series;
mod greatest_common_divisor;
mod huber_loss;
mod infix_to_postfix;
mod interest;
mod interpolation;
mod interquartile_range;
Expand Down Expand Up @@ -122,6 +123,7 @@ pub use self::greatest_common_divisor::{
greatest_common_divisor_stein,
};
pub use self::huber_loss::huber_loss;
pub use self::infix_to_postfix::infix_to_postfix;
pub use self::interest::{compound_interest, simple_interest};
pub use self::interpolation::{lagrange_polynomial_interpolation, linear_interpolation};
pub use self::interquartile_range::interquartile_range;
Expand Down

0 comments on commit d2299cd

Please sign in to comment.