Skip to content

Commit

Permalink
feat: ordered dynamic programming
Browse files Browse the repository at this point in the history
  • Loading branch information
ghimiresdp committed Sep 6, 2024
1 parent d91ab75 commit c80ce8c
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 79 deletions.
4 changes: 0 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ members = [
"complexity-analysis",
"data-structures",
"design-patterns",
"dynamic-programming",
"macros",
"problem-solving",
"projects/pandas",
Expand Down
34 changes: 14 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ you can run `cargo test` command, or to run specific test, you can run
`cargo test --bin <binary_name>`

```bash
# Example: running binary for hoffman encoding
cargo run --bin hoffman
cargo test --bin hoffman
# Example: running binary for huffman encoding
cargo run --bin huffman
cargo test --bin huffman
```

## [1. Data Structures](./data-structures/)
Expand Down Expand Up @@ -45,7 +45,7 @@ cargo test --bin hoffman
4. Trie
5. Disjoint Set

## [2. Algorithms](./algorithms/)
## [2. Algorithms](./algorithms/README.md)

### [2.1. Searching](algorithms/searching/)

Expand Down Expand Up @@ -80,7 +80,7 @@ cargo test --bin hoffman
4. [Topological Sort]
5. [A* Search Algorithm]

## [3. Design Patterns](./design-patterns/)
## [3. Design Patterns](./design-patterns/README.md)

1. [Singleton Pattern](design-patterns/src/singleton.rs) `cargo run --bin singleton`
2. [Factory Pattern](design-patterns/src/factory.rs) `cargo run --bin factory`
Expand All @@ -91,7 +91,7 @@ cargo test --bin hoffman
7. [Command Pattern](design-patterns/src/command.rs) `cargo run --bin command`
8. [Adapter Pattern](design-patterns/src/adapter.rs) `cargo run --bin adapter`

## [4. Problem Solving](problem-solving/)
## [4. Problem Solving](problem-solving/README.md)

### [4.1. Basic Problems](problem-solving/basic/)

Expand All @@ -103,12 +103,16 @@ cargo test --bin hoffman
6. [Linear Regression Model](problem-solving/basic/linear_regression.rs) `cargo run --bin linear_regression`
7. [Matrix Multiplication Model](problem-solving/basic/matrix_multiplication.rs) `cargo run --bin matrix_multiplication`

### [4.2. Mid Level Problems](problem-solving/mid/)
### [4.2. Dynamic PRogramming](problem-solving/dp/)

1. [List group by consecutive numbers](problem-solving/mid/consecutive_groups.rs) `cargo run --bin consecutive_groups`
2. [Find the length of the longest substring with maximum 2 repetition](problem-solving/mid/repeat.rs)`cargo run --bin repeat`
3. [Find the index of 2 numbers in an array whose sum equals to the provided target](problem-solving/mid/two_sum.rs) `cargo run --bin two_sum`
4. [Minimize the Sum from an array](problem-solving/mid/minimize_sum.rs) `cargo run --bin minimize_sum`
5. [Fibonacci Series](./problem-solving/dp/fibonacci.rs) `cargo run --bin fibonacci`
6. [Longest Commmon Subsequence](./problem-solving/dp/longest_common_subsequence.rs) `cargo run --bin lcs`
7. [Coin Change Problem]
8. [Palindrome Partition]

### [4.3. Pro Level Problems](problem-solving/pro/)

Expand All @@ -118,21 +122,11 @@ cargo test --bin hoffman

## [5. Complexity Analysis](./complexity-analysis/)

1. Time Complexity analysis
2. Space Complexity analysis
### [5.1. Time Complexity analysis]

## [6. Dynamic Programming](./dynamic-programming/)
### [5.2. Space Complexity analysis]

1. [Fibonacci Series](./dynamic-programming/src/dp001_fibonacci.rs)
- run: `cargo run --bin fib`
- test: `cargo test --bin fib`
2. [Longest Commmon Subsequence](./dynamic-programming/src/dp002_lcs.rs)
- run: `cargo run --bin lcs`
- test: `cargo test --bin lcs`
3. [Knapsack Problem]
4. [Matrix Multiplication]

## [7. Projects](./projects/)
## [6. Projects](./projects/)

> **Note**: Topics that do not contain hyperlinks are work in progress and will
> be updated as soon as the solution gets completed.
Expand Down
14 changes: 0 additions & 14 deletions dynamic-programming/Cargo.toml

This file was deleted.

18 changes: 12 additions & 6 deletions problem-solving/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,28 @@ path = "basic/comprehension.rs"
name = "matrix_multiplication"
path = "basic/matrix_multiplication.rs"

# =============================[ MEDIUM CHALLENGES]=============================
# ============================[ Dynamic Programmign]============================

[[bin]]
name = "consecutive"
path = "mid/consecutive_groups.rs"
path = "dp/consecutive_groups.rs"

[[bin]]
name = "repeat"
path = "mid/repeat.rs"
path = "dp/repeat.rs"

[[bin]]
name = "minimize_sum"
path = "mid/minimize_sum.rs"
path = "dp/minimize_sum.rs"

[[bin]]
name = 'two_sum'
path = "mid/two_sum.rs"
path = "dp/two_sum.rs"

#===============================[ PRO CHALLENGES]===============================
[[bin]]
name = "fibonacci"
path = "dp/fibonacci.rs"

[[bin]]
name = 'lcs'
path = "dp/longest_common_subsequence.rs"
6 changes: 5 additions & 1 deletion problem-solving/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
6. [Linear Regression Model](basic/linear_regression.rs) `cargo run --bin linear_regression`
7. [Matrix Multiplication Model](basic/matrix_multiplication.rs) `cargo run --bin matmul`

### [4.2. Mid Level Problems](mid/)
### [4.2. Dynamic Programming](dp/)

1. [List group by consecutive numbers](mid/consecutive_groups.rs) `cargo run --bin consecutive_groups`
2. [Find the length of the longest substring with maximum 2 repetition](mid/repeat.rs)`cargo run --bin repeat`
3. [Find the index of 2 numbers in an array whose sum equals to the provided target](mid/two_sum.rs) `cargo run --bin two_sum`
4. [Minimize the Sum from an array](mid/minimize_sum.rs) `cargo run --bin minimize_sum`
5. [Fibonacci Series](src/fibonacci.rs)
6. [Longest Common Subsequence](src/longest_common_subsequence.rs)
7. [Coin Change Problem]
8. [Palindrome Partition]

### [4.3. Pro Level Problems](pro/)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! To run/test the solution please enter the commands below
//! `cargo run --bin fibonacci`
//! `cargo test --bin fibonacci`
use std::io;

fn fib(n: usize) -> usize {
Expand Down Expand Up @@ -32,6 +35,16 @@ fn fibonacci(mut n: usize) -> usize {
return result;
}

/**
* The main function of the program. It prompts the user to enter a number,
* calculates the nth number in the Fibonacci series using both a recursive and
* iterative approach, and prints the results.
*
* This solution works until 93rd number in the fibonacci series since the
* solution uses `usize` which is 64 bit unsigned integer.
*
* None. The function prints the results to the console.
*/
fn main() {
let mut input = String::new();
println!("Enter the value of n: ");
Expand All @@ -43,11 +56,15 @@ fn main() {
input,
fibonacci(input)
);
println!(
"The {}th number of the fibonacci series using recursion is: {}",
input,
fib(input)
);
if input < 40 {
println!(
"The {}th number of the fibonacci series using recursion is: {}",
input,
fib(input)
);
} else {
println!("The recursive calculation is skipped since it is inefficient at higher numbers")
}
}

#[cfg(test)]
Expand All @@ -62,4 +79,13 @@ mod tests {
fn fib_with_recursion() {
assert_eq!(fib(10), 55);
}

/// this panics since it is 197,402,742,198,682,231,67 and can not fit into
/// the `usize` hence it panics with the message:
/// "attempt to add with overflow"
#[test]
#[should_panic]
fn fib_with_recursion_above_usize() {
fibonacci(94);
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
/**
* -----------------------------------------------------------------------------
* Longest Common Subsequence problem
*
* To execute, please run: cargo run --bin adapter
* To run tests, please run: cargo test --bin adapter
* -----------------------------------------------------------------------------
*
* Given two strings, find the longest common subsequence (LCS).
* A subsequence is a sequence that appears in the same relative order, but not
* necessarily consecutive within another sequence.
*
* example:
* - X = [AGGTAB]
* - Y = [GXTXAYB]
*
* The LCS of X and Y is [GTAB] with length 4
*
*/
fn main() {
let a = "AGGTAB";
let b = "GXTXAYB";
println!("LCS is : {}", lcs(a, b));
println!("LCS is : {}", lcs(b, a));
}
//! ----------------------------------------------------------------------------
//! ? To execute/test please run:
//! ```
//! cargo run --bin lcs
//! cargo test --bin lcs
//! ```
//! ---------------------------------------------------------------------------

/// Longest Common Subsequence problem
/// Given two strings, find the longest common subsequence (LCS).
/// A subsequence is a sequence that appears in the same relative order, but not
/// necessarily consecutive within another sequence.
///
/// example:
/// - X = [AGGTAB]
/// - Y = [GXTXAYB]
///
/// The LCS of X and Y is [GTAB] with length 4
///
/// Computes the length of the Longest Common Subsequence (LCS) between two
/// strings.
/// The LCS of two strings is the longest sequence of characters that appears
Expand Down Expand Up @@ -66,6 +59,20 @@ fn lcs(a: &str, b: &str) -> usize {
matrix[m][n]
}

fn main() {
let a = "AGGTAB";
let b = "GXTXAYB";
println!(
r"
Finding out the length of the longest common subsequence between
- AGGTAB
- GXTXAYB
"
);
println!("LCS is : {}", lcs(a, b));
println!("LCS is : {}", lcs(b, a));
}

#[cfg(test)]
mod tests {
use crate::lcs;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 0 additions & 3 deletions problem-solving/pro/main.rs

This file was deleted.

0 comments on commit c80ce8c

Please sign in to comment.