Skip to content

[pull] master from TheAlgorithms:master #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions src/conversions/binary_to_hexadecimal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Author : cyrixninja
// Binary to Hex Converter : Converts Binary to Hexadecimal
// Wikipedia References : 1. https://en.wikipedia.org/wiki/Hexadecimal
// 2. https://en.wikipedia.org/wiki/Binary_number

static BITS_TO_HEX: &[(u8, &str)] = &[
(0b0000, "0"),
(0b0001, "1"),
(0b0010, "2"),
(0b0011, "3"),
(0b0100, "4"),
(0b0101, "5"),
(0b0110, "6"),
(0b0111, "7"),
(0b1000, "8"),
(0b1001, "9"),
(0b1010, "a"),
(0b1011, "b"),
(0b1100, "c"),
(0b1101, "d"),
(0b1110, "e"),
(0b1111, "f"),
];

fn bin_to_hexadecimal(binary_str: &str) -> String {
let binary_str = binary_str.trim();

if binary_str.is_empty() {
return String::from("Invalid Input");
}

let is_negative = binary_str.starts_with('-');
let binary_str = if is_negative {
&binary_str[1..]
} else {
binary_str
};

if !binary_str.chars().all(|c| c == '0' || c == '1') {
return String::from("Invalid Input");
}

let padded_len = (4 - (binary_str.len() % 4)) % 4;
let binary_str = format!("{:0width$}", binary_str, width = binary_str.len() + padded_len);

// Convert binary to hexadecimal
let mut hexadecimal = String::with_capacity(binary_str.len() / 4 + 2);
hexadecimal.push_str("0x");

for chunk in binary_str.as_bytes().chunks(4) {
let mut nibble = 0;
for (i, &byte) in chunk.iter().enumerate() {
nibble |= ((byte - b'0') as u8) << (3 - i);
}

let hex_char = BITS_TO_HEX
.iter()
.find(|&&(bits, _)| bits == nibble)
.map(|&(_, hex)| hex)
.unwrap();
hexadecimal.push_str(hex_char);
}

if is_negative {
format!("-{}", hexadecimal)
} else {
hexadecimal
}
}

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

#[test]
fn test_empty_string() {
let input = "";
let expected = "Invalid Input";
assert_eq!(bin_to_hexadecimal(input), expected);
}

#[test]
fn test_invalid_binary() {
let input = "a";
let expected = "Invalid Input";
assert_eq!(bin_to_hexadecimal(input), expected);
}

#[test]
fn test_binary() {
let input = "00110110";
let expected = "0x36";
assert_eq!(bin_to_hexadecimal(input), expected);
}

#[test]
fn test_padded_binary() {
let input = " 1010 ";
let expected = "0xa";
assert_eq!(bin_to_hexadecimal(input), expected);
}
}
36 changes: 36 additions & 0 deletions src/machine_learning/loss_function/mae_loss.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! # Mean Absolute Error Loss Function
//!
//! The `mae_loss` function calculates the Mean Absolute Error loss, which is a
//! robust loss function used in machine learning.
//!
//! ## Formula
//!
//! For a pair of actual and predicted values, represented as vectors `actual`
//! and `predicted`, the Mean Absolute loss is calculated as:
//!
//! - loss = `(actual - predicted) / n_elements`.
//!
//! It returns the average loss by dividing the `total_loss` by total no. of
//! elements.
//!
pub fn mae_loss(predicted: &Vec<f64>, actual: &[f64]) -> f64 {
let mut total_loss: f64 = 0.0;
for (p, a) in predicted.iter().zip(actual.iter()) {
let diff: f64 = p - a;
let absolute_diff: f64 = diff.abs();
total_loss += absolute_diff;
}
total_loss / (predicted.len() as f64)
}

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

#[test]
fn test_mae_loss() {
let predicted_values: Vec<f64> = vec![1.0, 2.0, 3.0, 4.0];
let actual_values: Vec<f64> = vec![1.0, 3.0, 3.5, 4.5];
assert_eq!(mae_loss(&predicted_values, &actual_values), 0.5);
}
}
2 changes: 2 additions & 0 deletions src/machine_learning/loss_function/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod mae_loss;
mod mse_loss;

pub use self::mae_loss::mae_loss;
pub use self::mse_loss::mse_loss;
3 changes: 3 additions & 0 deletions src/machine_learning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ mod loss_function;
mod optimization;

pub use self::linear_regression::linear_regression;

pub use self::loss_function::mae_loss;
pub use self::loss_function::mse_loss;

pub use self::optimization::gradient_descent;