Skip to content

Commit 03d77a8

Browse files
authored
Add decimal to hexadecimal conversion (#631)
1 parent 7d2aa9e commit 03d77a8

File tree

15 files changed

+76
-17
lines changed

15 files changed

+76
-17
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
* [Binary To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_decimal.rs)
4444
* [Binary To Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_hexadecimal.rs)
4545
* [Decimal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/decimal_to_binary.rs)
46+
* [Decimal To Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/decimal_to_hexadecimal.rs)
4647
* [Hexadecimal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_binary.rs)
4748
* [Octal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_binary.rs)
4849
* [Octal To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_decimal.rs)

src/backtracking/n_queens.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn n_queens_solver(n: usize) -> Vec<Vec<String>> {
3636
solutions
3737
}
3838

39-
fn is_safe(board: &Vec<Vec<char>>, row: usize, col: usize) -> bool {
39+
fn is_safe(board: &[Vec<char>], row: usize, col: usize) -> bool {
4040
// Check if there is a queen in the same column
4141
for (i, _) in board.iter().take(row).enumerate() {
4242
if board[i][col] == 'Q' {

src/ciphers/base64.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn collect_six_bits(from: (u8, u8), offset: u8) -> u8 {
3131
((combined & (0b1111110000000000u16 >> offset)) >> (10 - offset)) as u8
3232
}
3333

34-
pub fn base64_encode(data: &Vec<u8>) -> String {
34+
pub fn base64_encode(data: &[u8]) -> String {
3535
let mut bits_encoded = 0usize;
3636
let mut encoded_string = String::new();
3737
// Using modulo twice to prevent an underflow, Wolfram|Alpha says this is optimal
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
pub fn decimal_to_hexadecimal(base_num: u64) -> String {
2+
let mut num = base_num;
3+
let mut hexadecimal_num = String::new();
4+
5+
loop {
6+
let remainder = num % 16;
7+
let hex_char = if remainder < 10 {
8+
(remainder as u8 + b'0') as char
9+
} else {
10+
(remainder as u8 - 10 + b'A') as char
11+
};
12+
13+
hexadecimal_num.insert(0, hex_char);
14+
num /= 16;
15+
if num == 0 {
16+
break;
17+
}
18+
}
19+
20+
hexadecimal_num
21+
}
22+
23+
#[cfg(test)]
24+
mod tests {
25+
use super::*;
26+
27+
#[test]
28+
fn test_zero() {
29+
assert_eq!(decimal_to_hexadecimal(0), "0");
30+
}
31+
32+
#[test]
33+
fn test_single_digit_decimal() {
34+
assert_eq!(decimal_to_hexadecimal(9), "9");
35+
}
36+
37+
#[test]
38+
fn test_single_digit_hexadecimal() {
39+
assert_eq!(decimal_to_hexadecimal(12), "C");
40+
}
41+
42+
#[test]
43+
fn test_multiple_digit_hexadecimal() {
44+
assert_eq!(decimal_to_hexadecimal(255), "FF");
45+
}
46+
47+
#[test]
48+
fn test_big() {
49+
assert_eq!(decimal_to_hexadecimal(u64::MAX), "FFFFFFFFFFFFFFFF");
50+
}
51+
52+
#[test]
53+
fn test_random() {
54+
assert_eq!(decimal_to_hexadecimal(123456), "1E240");
55+
}
56+
}

src/conversions/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
mod binary_to_decimal;
22
mod binary_to_hexadecimal;
33
mod decimal_to_binary;
4+
mod decimal_to_hexadecimal;
45
mod hexadecimal_to_binary;
56
mod octal_to_binary;
67
mod octal_to_decimal;
78
pub use self::binary_to_decimal::binary_to_decimal;
89
pub use self::binary_to_hexadecimal::binary_to_hexadecimal;
910
pub use self::decimal_to_binary::decimal_to_binary;
11+
pub use self::decimal_to_hexadecimal::decimal_to_hexadecimal;
1012
pub use self::hexadecimal_to_binary::hexadecimal_to_binary;
1113
pub use self::octal_to_binary::octal_to_binary;
1214
pub use self::octal_to_decimal::octal_to_decimal;

src/data_structures/probabilistic/bloom_filter.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl<Item: Hash> BloomFilter<Item> for MultiBinaryBloomFilter {
136136
for builder in &self.hash_builders {
137137
let mut hasher = builder.build_hasher();
138138
item.hash(&mut hasher);
139-
let hash = hasher.finish();
139+
let hash = builder.hash_one(&item);
140140
let index = hash % self.filter_size as u64;
141141
let byte_index = index as usize / 8; // this is this byte that we need to modify
142142
let bit_index = (index % 8) as u8; // we cannot only OR with value 1 this time, since we have 8 bits
@@ -148,7 +148,7 @@ impl<Item: Hash> BloomFilter<Item> for MultiBinaryBloomFilter {
148148
for builder in &self.hash_builders {
149149
let mut hasher = builder.build_hasher();
150150
item.hash(&mut hasher);
151-
let hash = hasher.finish();
151+
let hash = builder.hash_one(item);
152152
let index = hash % self.filter_size as u64;
153153
let byte_index = index as usize / 8; // this is this byte that we need to modify
154154
let bit_index = (index % 8) as u8; // we cannot only OR with value 1 this time, since we have 8 bits

src/data_structures/probabilistic/count_min_sketch.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::hash_map::RandomState;
22
use std::fmt::{Debug, Formatter};
3-
use std::hash::{BuildHasher, Hash, Hasher};
3+
use std::hash::{BuildHasher, Hash};
44

55
/// A probabilistic data structure holding an approximate count for diverse items efficiently (using constant space)
66
///
@@ -129,7 +129,7 @@ impl<Item: Hash, const WIDTH: usize, const DEPTH: usize> CountMinSketch
129129
for (row, r) in self.hashers.iter_mut().enumerate() {
130130
let mut h = r.build_hasher();
131131
item.hash(&mut h);
132-
let hashed = h.finish();
132+
let hashed = r.hash_one(&item);
133133
let col = (hashed % WIDTH as u64) as usize;
134134
self.counts[row][col] += count;
135135
}
@@ -142,7 +142,7 @@ impl<Item: Hash, const WIDTH: usize, const DEPTH: usize> CountMinSketch
142142
.map(|(row, r)| {
143143
let mut h = r.build_hasher();
144144
item.hash(&mut h);
145-
let hashed = h.finish();
145+
let hashed = r.hash_one(&item);
146146
let col = (hashed % WIDTH as u64) as usize;
147147
self.counts[row][col]
148148
})

src/geometry/polygon_points.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn cross(x1: Ll, y1: Ll, x2: Ll, y2: Ll) -> Ll {
55
x1 * y2 - x2 * y1
66
}
77

8-
pub fn polygon_area(pts: &Vec<Pll>) -> Ll {
8+
pub fn polygon_area(pts: &[Pll]) -> Ll {
99
let mut ats = 0;
1010
for i in 2..pts.len() {
1111
ats += cross(
@@ -27,7 +27,7 @@ fn gcd(mut a: Ll, mut b: Ll) -> Ll {
2727
a
2828
}
2929

30-
fn boundary(pts: &Vec<Pll>) -> Ll {
30+
fn boundary(pts: &[Pll]) -> Ll {
3131
let mut ats = pts.len() as Ll;
3232
for i in 0..pts.len() {
3333
let deltax = pts[i].0 - pts[(i + 1) % pts.len()].0;
@@ -37,7 +37,7 @@ fn boundary(pts: &Vec<Pll>) -> Ll {
3737
ats
3838
}
3939

40-
pub fn lattice_points(pts: &Vec<Pll>) -> Ll {
40+
pub fn lattice_points(pts: &[Pll]) -> Ll {
4141
let bounds = boundary(pts);
4242
let area = polygon_area(pts);
4343
area + 1 - bounds / 2

src/machine_learning/loss_function/mae_loss.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! It returns the average loss by dividing the `total_loss` by total no. of
1414
//! elements.
1515
//!
16-
pub fn mae_loss(predicted: &Vec<f64>, actual: &[f64]) -> f64 {
16+
pub fn mae_loss(predicted: &[f64], actual: &[f64]) -> f64 {
1717
let mut total_loss: f64 = 0.0;
1818
for (p, a) in predicted.iter().zip(actual.iter()) {
1919
let diff: f64 = p - a;

src/machine_learning/loss_function/mse_loss.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! It returns the average loss by dividing the `total_loss` by total no. of
1414
//! elements.
1515
//!
16-
pub fn mse_loss(predicted: &Vec<f64>, actual: &[f64]) -> f64 {
16+
pub fn mse_loss(predicted: &[f64], actual: &[f64]) -> f64 {
1717
let mut total_loss: f64 = 0.0;
1818
for (p, a) in predicted.iter().zip(actual.iter()) {
1919
let diff: f64 = p - a;

src/machine_learning/optimization/adam.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl Adam {
6464
}
6565
}
6666

67-
pub fn step(&mut self, gradients: &Vec<f64>) -> Vec<f64> {
67+
pub fn step(&mut self, gradients: &[f64]) -> Vec<f64> {
6868
let mut model_params = vec![0.0; gradients.len()];
6969
self.t += 1;
7070

src/math/area_of_polygon.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct Point {
2525
* @return The area of the polygon.
2626
*/
2727

28-
pub fn area_of_polygon(fig: &Vec<Point>) -> f64 {
28+
pub fn area_of_polygon(fig: &[Point]) -> f64 {
2929
let mut res = 0.0;
3030

3131
for i in 0..fig.len() {

src/searching/moore_voting.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
4242
*/
4343

44-
pub fn moore_voting(arr: &Vec<i32>) -> i32 {
44+
pub fn moore_voting(arr: &[i32]) -> i32 {
4545
let n = arr.len();
4646
let mut cnt = 0; // initializing cnt
4747
let mut ele = 0; // initializing ele

src/searching/saddleback_search.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// element with the target element.
55
use std::cmp::Ordering;
66

7-
pub fn saddleback_search(matrix: &Vec<Vec<i32>>, element: i32) -> (usize, usize) {
7+
pub fn saddleback_search(matrix: &[Vec<i32>], element: i32) -> (usize, usize) {
88
// Initialize left and right indices
99
let mut left_index = 0;
1010
let mut right_index = matrix[0].len() - 1;

src/string/aho_corasick.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl AhoCorasick {
6565
let mut ans = vec![];
6666
let mut cur = Rc::clone(&self.root);
6767
let mut position: usize = 0;
68-
for (_, c) in s.chars().enumerate() {
68+
for c in s.chars() {
6969
loop {
7070
if let Some(child) = Rc::clone(&cur).borrow().trans.get(&c) {
7171
cur = Rc::clone(child);

0 commit comments

Comments
 (0)