Skip to content

Commit 6bb1391

Browse files
authored
Merge branch 'TheAlgorithms:master' into master
2 parents 8f06c40 + 41c76e4 commit 6bb1391

37 files changed

+2804
-1042
lines changed

.github/workflows/upload_coverage_report.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ jobs:
3131
if: >-
3232
github.event_name == 'pull_request' &&
3333
github.event.pull_request.head.repo.full_name != github.repository
34-
uses: codecov/codecov-action@v4
34+
uses: codecov/codecov-action@v5
3535
with:
3636
files: "${{ env.REPORT_NAME }}"
3737
fail_ci_if_error: true
3838
- name: Upload coverage to codecov (with token)
3939
if: "! github.event.pull_request.head.repo.fork "
40-
uses: codecov/codecov-action@v4
40+
uses: codecov/codecov-action@v5
4141
with:
4242
token: ${{ secrets.CODECOV_TOKEN }}
4343
files: "${{ env.REPORT_NAME }}"

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ suspicious_operation_groupings = { level = "allow", priority = 1 }
164164
use_self = { level = "allow", priority = 1 }
165165
while_float = { level = "allow", priority = 1 }
166166
needless_pass_by_ref_mut = { level = "allow", priority = 1 }
167+
too_long_first_doc_paragraph = { level = "allow", priority = 1 }
167168
# cargo-lints:
168169
cargo_common_metadata = { level = "allow", priority = 1 }
169170
# style-lints:

DIRECTORY.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@
5353
* [Decimal To Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/decimal_to_hexadecimal.rs)
5454
* [Hexadecimal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_binary.rs)
5555
* [Hexadecimal To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_decimal.rs)
56+
* [Length Conversion](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/length_conversion.rs)
5657
* [Octal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_binary.rs)
5758
* [Octal To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_decimal.rs)
59+
* [Rgb Cmyk Conversion](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/rgb_cmyk_conversion.rs)
5860
* Data Structures
5961
* [Avl Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/avl_tree.rs)
6062
* [B Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/b_tree.rs)
@@ -100,6 +102,8 @@
100102
* [Subset Generation](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/subset_generation.rs)
101103
* [Trapped Rainwater](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/trapped_rainwater.rs)
102104
* [Word Break](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/word_break.rs)
105+
* Financial
106+
* [Present Value](https://github.com/TheAlgorithms/Rust/blob/master/src/financial/present_value.rs)
103107
* General
104108
* [Convex Hull](https://github.com/TheAlgorithms/Rust/blob/master/src/general/convex_hull.rs)
105109
* [Fisher Yates Shuffle](https://github.com/TheAlgorithms/Rust/blob/master/src/general/fisher_yates_shuffle.rs)
@@ -150,11 +154,14 @@
150154
* [Tarjans Ssc](https://github.com/TheAlgorithms/Rust/blob/master/src/graph/tarjans_ssc.rs)
151155
* [Topological Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/graph/topological_sort.rs)
152156
* [Two Satisfiability](https://github.com/TheAlgorithms/Rust/blob/master/src/graph/two_satisfiability.rs)
157+
* Greedy
158+
* [Stable Matching](https://github.com/TheAlgorithms/Rust/blob/master/src/greedy/stable_matching.rs)
153159
* [Lib](https://github.com/TheAlgorithms/Rust/blob/master/src/lib.rs)
154160
* Machine Learning
155161
* [Cholesky](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/cholesky.rs)
156162
* [K Means](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/k_means.rs)
157163
* [Linear Regression](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/linear_regression.rs)
164+
* [Logistic Regression](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/logistic_regression.rs)
158165
* Loss Function
159166
* [Average Margin Ranking Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/loss_function/average_margin_ranking_loss.rs)
160167
* [Hinge Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/loss_function/hinge_loss.rs)
@@ -316,6 +323,7 @@
316323
* [Burrows Wheeler Transform](https://github.com/TheAlgorithms/Rust/blob/master/src/string/burrows_wheeler_transform.rs)
317324
* [Duval Algorithm](https://github.com/TheAlgorithms/Rust/blob/master/src/string/duval_algorithm.rs)
318325
* [Hamming Distance](https://github.com/TheAlgorithms/Rust/blob/master/src/string/hamming_distance.rs)
326+
* [Isogram](https://github.com/TheAlgorithms/Rust/blob/master/src/string/isogram.rs)
319327
* [Isomorphism](https://github.com/TheAlgorithms/Rust/blob/master/src/string/isomorphism.rs)
320328
* [Jaro Winkler Distance](https://github.com/TheAlgorithms/Rust/blob/master/src/string/jaro_winkler_distance.rs)
321329
* [Knuth Morris Pratt](https://github.com/TheAlgorithms/Rust/blob/master/src/string/knuth_morris_pratt.rs)
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/// Author : https://github.com/ali77gh
2+
/// Conversion of length units.
3+
///
4+
/// Available Units:
5+
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Millimeter
6+
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Centimeter
7+
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Meter
8+
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Kilometer
9+
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Inch
10+
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Foot
11+
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Yard
12+
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Mile
13+
14+
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
15+
pub enum LengthUnit {
16+
Millimeter,
17+
Centimeter,
18+
Meter,
19+
Kilometer,
20+
Inch,
21+
Foot,
22+
Yard,
23+
Mile,
24+
}
25+
26+
fn unit_to_meter_multiplier(from: LengthUnit) -> f64 {
27+
match from {
28+
LengthUnit::Millimeter => 0.001,
29+
LengthUnit::Centimeter => 0.01,
30+
LengthUnit::Meter => 1.0,
31+
LengthUnit::Kilometer => 1000.0,
32+
LengthUnit::Inch => 0.0254,
33+
LengthUnit::Foot => 0.3048,
34+
LengthUnit::Yard => 0.9144,
35+
LengthUnit::Mile => 1609.34,
36+
}
37+
}
38+
39+
fn unit_to_meter(input: f64, from: LengthUnit) -> f64 {
40+
input * unit_to_meter_multiplier(from)
41+
}
42+
43+
fn meter_to_unit(input: f64, to: LengthUnit) -> f64 {
44+
input / unit_to_meter_multiplier(to)
45+
}
46+
47+
/// This function will convert a value in unit of [from] to value in unit of [to]
48+
/// by first converting it to meter and than convert it to destination unit
49+
pub fn length_conversion(input: f64, from: LengthUnit, to: LengthUnit) -> f64 {
50+
meter_to_unit(unit_to_meter(input, from), to)
51+
}
52+
53+
#[cfg(test)]
54+
mod length_conversion_tests {
55+
use std::collections::HashMap;
56+
57+
use super::LengthUnit::*;
58+
use super::*;
59+
60+
#[test]
61+
fn zero_to_zero() {
62+
let units = vec![
63+
Millimeter, Centimeter, Meter, Kilometer, Inch, Foot, Yard, Mile,
64+
];
65+
66+
for u1 in units.clone() {
67+
for u2 in units.clone() {
68+
assert_eq!(length_conversion(0f64, u1, u2), 0f64);
69+
}
70+
}
71+
}
72+
73+
#[test]
74+
fn length_of_one_meter() {
75+
let meter_in_different_units = HashMap::from([
76+
(Millimeter, 1000f64),
77+
(Centimeter, 100f64),
78+
(Kilometer, 0.001f64),
79+
(Inch, 39.37007874015748f64),
80+
(Foot, 3.280839895013123f64),
81+
(Yard, 1.0936132983377078f64),
82+
(Mile, 0.0006213727366498068f64),
83+
]);
84+
for (input_unit, input_value) in &meter_in_different_units {
85+
for (target_unit, target_value) in &meter_in_different_units {
86+
assert!(
87+
num_traits::abs(
88+
length_conversion(*input_value, *input_unit, *target_unit) - *target_value
89+
) < 0.0000001
90+
);
91+
}
92+
}
93+
}
94+
}

src/conversions/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ mod decimal_to_binary;
44
mod decimal_to_hexadecimal;
55
mod hexadecimal_to_binary;
66
mod hexadecimal_to_decimal;
7+
mod length_conversion;
78
mod octal_to_binary;
89
mod octal_to_decimal;
10+
mod rgb_cmyk_conversion;
911
pub use self::binary_to_decimal::binary_to_decimal;
1012
pub use self::binary_to_hexadecimal::binary_to_hexadecimal;
1113
pub use self::decimal_to_binary::decimal_to_binary;
1214
pub use self::decimal_to_hexadecimal::decimal_to_hexadecimal;
1315
pub use self::hexadecimal_to_binary::hexadecimal_to_binary;
1416
pub use self::hexadecimal_to_decimal::hexadecimal_to_decimal;
17+
pub use self::length_conversion::length_conversion;
1518
pub use self::octal_to_binary::octal_to_binary;
1619
pub use self::octal_to_decimal::octal_to_decimal;
20+
pub use self::rgb_cmyk_conversion::rgb_to_cmyk;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/// Author : https://github.com/ali77gh\
2+
/// References:\
3+
/// RGB: https://en.wikipedia.org/wiki/RGB_color_model\
4+
/// CMYK: https://en.wikipedia.org/wiki/CMYK_color_model\
5+
6+
/// This function Converts RGB to CMYK format
7+
///
8+
/// ### Params
9+
/// * `r` - red
10+
/// * `g` - green
11+
/// * `b` - blue
12+
///
13+
/// ### Returns
14+
/// (C, M, Y, K)
15+
pub fn rgb_to_cmyk(rgb: (u8, u8, u8)) -> (u8, u8, u8, u8) {
16+
// Safety: no need to check if input is positive and less than 255 because it's u8
17+
18+
// change scale from [0,255] to [0,1]
19+
let (r, g, b) = (
20+
rgb.0 as f64 / 255f64,
21+
rgb.1 as f64 / 255f64,
22+
rgb.2 as f64 / 255f64,
23+
);
24+
25+
match 1f64 - r.max(g).max(b) {
26+
1f64 => (0, 0, 0, 100), // pure black
27+
k => (
28+
(100f64 * (1f64 - r - k) / (1f64 - k)) as u8, // c
29+
(100f64 * (1f64 - g - k) / (1f64 - k)) as u8, // m
30+
(100f64 * (1f64 - b - k) / (1f64 - k)) as u8, // y
31+
(100f64 * k) as u8, // k
32+
),
33+
}
34+
}
35+
36+
#[cfg(test)]
37+
mod tests {
38+
use super::*;
39+
40+
macro_rules! test_rgb_to_cmyk {
41+
($($name:ident: $tc:expr,)*) => {
42+
$(
43+
#[test]
44+
fn $name() {
45+
let (rgb, cmyk) = $tc;
46+
assert_eq!(rgb_to_cmyk(rgb), cmyk);
47+
}
48+
)*
49+
}
50+
}
51+
52+
test_rgb_to_cmyk! {
53+
white: ((255, 255, 255), (0, 0, 0, 0)),
54+
gray: ((128, 128, 128), (0, 0, 0, 49)),
55+
black: ((0, 0, 0), (0, 0, 0, 100)),
56+
red: ((255, 0, 0), (0, 100, 100, 0)),
57+
green: ((0, 255, 0), (100, 0, 100, 0)),
58+
blue: ((0, 0, 255), (100, 100, 0, 0)),
59+
}
60+
}

0 commit comments

Comments
 (0)