Skip to content

Commit 02dada7

Browse files
committed
solve all your base
1 parent adc744d commit 02dada7

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

rust/all-your-base/src/lib.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,32 @@ pub fn convert(number: &[u32], from_base: u32, to_base: u32) -> Result<Vec<u32>,
5151
return Ok(vec!(0))
5252
}
5353

54-
todo!("Convert {number:?} from base {from_base} to base {to_base}")
54+
let decimal = convert_to_decimal(number, from_base);
55+
print!("decimal {}", decimal);
56+
57+
let result = convert_decimal_to(decimal, to_base);
58+
return Ok(result)
59+
}
60+
61+
fn convert_decimal_to(decimal: u32, to_base: u32) -> Vec<u32> {
62+
let mut result = vec!();
63+
let mut n = decimal;
64+
while n != 0 {
65+
let remainder = n % to_base;
66+
n = n / to_base;
67+
result.insert(0, remainder);
68+
}
69+
return result
70+
}
71+
72+
fn convert_to_decimal(number: &[u32], from_base: u32) -> u32 {
73+
let mut decimal = 0;
74+
let mut cloned = number.to_vec();
75+
cloned.reverse();
76+
for (i, digit) in cloned.iter().enumerate() {
77+
decimal += digit * from_base.pow(i.try_into().unwrap());
78+
}
79+
decimal
5580
}
5681

5782
fn is_valid_base(base :u32) -> bool {

rust/all-your-base/tests/all-your-base.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use allyourbase as ayb;
22

33
#[test]
4-
#[ignore]
54
fn single_bit_one_to_decimal() {
65
let input_base = 2;
76
let input_digits = &[1];
@@ -14,7 +13,6 @@ fn single_bit_one_to_decimal() {
1413
}
1514

1615
#[test]
17-
#[ignore]
1816
fn binary_to_single_decimal() {
1917
let input_base = 2;
2018
let input_digits = &[1, 0, 1];
@@ -27,7 +25,6 @@ fn binary_to_single_decimal() {
2725
}
2826

2927
#[test]
30-
#[ignore]
3128
fn single_decimal_to_binary() {
3229
let input_base = 10;
3330
let input_digits = &[5];
@@ -40,7 +37,6 @@ fn single_decimal_to_binary() {
4037
}
4138

4239
#[test]
43-
#[ignore]
4440
fn binary_to_multiple_decimal() {
4541
let input_base = 2;
4642
let input_digits = &[1, 0, 1, 0, 1, 0];
@@ -53,7 +49,6 @@ fn binary_to_multiple_decimal() {
5349
}
5450

5551
#[test]
56-
#[ignore]
5752
fn decimal_to_binary() {
5853
let input_base = 10;
5954
let input_digits = &[4, 2];
@@ -66,7 +61,6 @@ fn decimal_to_binary() {
6661
}
6762

6863
#[test]
69-
#[ignore]
7064
fn trinary_to_hexadecimal() {
7165
let input_base = 3;
7266
let input_digits = &[1, 1, 2, 0];
@@ -79,7 +73,6 @@ fn trinary_to_hexadecimal() {
7973
}
8074

8175
#[test]
82-
#[ignore]
8376
fn hexadecimal_to_trinary() {
8477
let input_base = 16;
8578
let input_digits = &[2, 10];
@@ -92,7 +85,6 @@ fn hexadecimal_to_trinary() {
9285
}
9386

9487
#[test]
95-
#[ignore]
9688
fn test_15_bit_integer() {
9789
let input_base = 97;
9890
let input_digits = &[3, 46, 60];
@@ -141,7 +133,6 @@ fn multiple_zeros() {
141133
}
142134

143135
#[test]
144-
#[ignore]
145136
fn leading_zeros() {
146137
let input_base = 7;
147138
let input_digits = &[0, 6, 0];

0 commit comments

Comments
 (0)