Initial commit

This commit is contained in:
2022-08-04 23:58:25 +02:00
commit ff1c36fc24
447 changed files with 9869 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
{
"blurb": "Convert a number, represented as a sequence of digits in one base, to any other base.",
"authors": [
"jonasbb"
],
"contributors": [
"CGMossa",
"coriolinus",
"cwhakes",
"efx",
"ErikSchierboom",
"IanWhitney",
"lutostag",
"mkantor",
"navossoc",
"nfiles",
"pedantic79",
"petertseng",
"rofrol",
"stringparser",
"xakon",
"ZapAnton"
],
"files": {
"solution": [
"src/lib.rs",
"Cargo.toml"
],
"test": [
"tests/all-your-base.rs"
],
"example": [
".meta/example.rs"
]
}
}

View File

@@ -0,0 +1 @@
{"track":"rust","exercise":"all-your-base","id":"22568dbd27034998a766c1e5833e585d","url":"https://exercism.org/tracks/rust/exercises/all-your-base","handle":"ArthurDanjou","is_requester":true,"auto_approve":false}

8
all-your-base/.gitignore vendored Normal file
View File

@@ -0,0 +1,8 @@
# Generated by Cargo
# will have compiled files and executables
/target/
**/*.rs.bk
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock

4
all-your-base/Cargo.toml Normal file
View File

@@ -0,0 +1,4 @@
[package]
edition = "2021"
name = "allyourbase"
version = "1.0.0"

85
all-your-base/HELP.md Normal file
View File

@@ -0,0 +1,85 @@
# Help
## Running the tests
Execute the tests with:
```bash
$ cargo test
```
All but the first test have been ignored. After you get the first test to
pass, open the tests source file which is located in the `tests` directory
and remove the `#[ignore]` flag from the next test and get the tests to pass
again. Each separate test is a function with `#[test]` flag above it.
Continue, until you pass every test.
If you wish to run _only ignored_ tests without editing the tests source file, use:
```bash
$ cargo test -- --ignored
```
If you are using Rust 1.51 or later, you can run _all_ tests with
```bash
$ cargo test -- --include-ignored
```
To run a specific test, for example `some_test`, you can use:
```bash
$ cargo test some_test
```
If the specific test is ignored, use:
```bash
$ cargo test some_test -- --ignored
```
To learn more about Rust tests refer to the online [test documentation][rust-tests].
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
## Submitting your solution
You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
## Rust Installation
Refer to the [exercism help page][help-page] for Rust installation and learning
resources.
## Submitting the solution
Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
## Feedback, Issues, Pull Requests
The GitHub [track repository][github] is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
If you want to know more about Exercism, take a look at the [contribution guide].
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
[help-page]: https://exercism.org/tracks/rust/learning
[github]: https://github.com/exercism/rust
[contribution guide]: https://exercism.org/docs/community/contributors

62
all-your-base/README.md Normal file
View File

@@ -0,0 +1,62 @@
# All Your Base
Welcome to All Your Base on Exercism's Rust Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Convert a number, represented as a sequence of digits in one base, to any other base.
Implement general base conversion. Given a number in base **a**,
represented as a sequence of digits, convert it to base **b**.
## Note
- Try to implement the conversion yourself.
Do not use something else to perform the conversion for you.
## About [Positional Notation](https://en.wikipedia.org/wiki/Positional_notation)
In positional notation, a number in base **b** can be understood as a linear
combination of powers of **b**.
The number 42, *in base 10*, means:
(4 * 10^1) + (2 * 10^0)
The number 101010, *in base 2*, means:
(1 * 2^5) + (0 * 2^4) + (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (0 * 2^0)
The number 1120, *in base 3*, means:
(1 * 3^3) + (1 * 3^2) + (2 * 3^1) + (0 * 3^0)
I think you got the idea!
*Yes. Those three numbers above are exactly the same. Congratulations!*
## Source
### Created by
- @jonasbb
### Contributed to by
- @CGMossa
- @coriolinus
- @cwhakes
- @efx
- @ErikSchierboom
- @IanWhitney
- @lutostag
- @mkantor
- @navossoc
- @nfiles
- @pedantic79
- @petertseng
- @rofrol
- @stringparser
- @xakon
- @ZapAnton

68
all-your-base/src/lib.rs Normal file
View File

@@ -0,0 +1,68 @@
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
InvalidInputBase,
InvalidOutputBase,
InvalidDigit(u32),
}
///
/// Convert a number between two bases.
///
/// A number is any slice of digits.
/// A digit is any unsigned integer (e.g. u8, u16, u32, u64, or usize).
/// Bases are specified as unsigned integers.
///
/// Return an `Err(.)` if the conversion is impossible.
/// The tests do not test for specific values inside the `Err(.)`.
///
///
/// You are allowed to change the function signature as long as all test still pass.
///
///
/// Example:
/// Input
/// number: &[4, 2]
/// from_base: 10
/// to_base: 2
/// Result
/// Ok(vec![1, 0, 1, 0, 1, 0])
///
/// The example corresponds to converting the number 42 from decimal
/// which is equivalent to 101010 in binary.
///
///
/// Notes:
/// * The empty slice ( "[]" ) is equal to the number 0.
/// * Never output leading 0 digits, unless the input number is 0, in which the output must be `[0]`.
/// However, your function must be able to process input with leading 0 digits.
///
pub fn convert(number: &[u32], from_base: u32, to_base: u32) -> Result<Vec<u32>, Error> {
if from_base < 2 {
return Err(Error::InvalidInputBase);
}
if to_base < 2 {
return Err(Error::InvalidOutputBase);
}
let mut value: u32 = 0;
for &n in number.iter() {
if n >= from_base {
return Err(Error::InvalidDigit(n))
} else {
value = value * from_base + n;
}
}
let mut result = Vec::new();
while value != 0 {
result.push(value % to_base);
value /= to_base;
}
if result.is_empty() {
result.push(0);
}
result.reverse();
Ok(result)
}

View File

@@ -0,0 +1,216 @@
use allyourbase as ayb;
#[test]
fn single_bit_one_to_decimal() {
let input_base = 2;
let input_digits = &[1];
let output_base = 10;
let output_digits = vec![1];
assert_eq!(
ayb::convert(input_digits, input_base, output_base),
Ok(output_digits)
);
}
#[test]
#[ignore]
fn binary_to_single_decimal() {
let input_base = 2;
let input_digits = &[1, 0, 1];
let output_base = 10;
let output_digits = vec![5];
assert_eq!(
ayb::convert(input_digits, input_base, output_base),
Ok(output_digits)
);
}
#[test]
#[ignore]
fn single_decimal_to_binary() {
let input_base = 10;
let input_digits = &[5];
let output_base = 2;
let output_digits = vec![1, 0, 1];
assert_eq!(
ayb::convert(input_digits, input_base, output_base),
Ok(output_digits)
);
}
#[test]
#[ignore]
fn binary_to_multiple_decimal() {
let input_base = 2;
let input_digits = &[1, 0, 1, 0, 1, 0];
let output_base = 10;
let output_digits = vec![4, 2];
assert_eq!(
ayb::convert(input_digits, input_base, output_base),
Ok(output_digits)
);
}
#[test]
#[ignore]
fn decimal_to_binary() {
let input_base = 10;
let input_digits = &[4, 2];
let output_base = 2;
let output_digits = vec![1, 0, 1, 0, 1, 0];
assert_eq!(
ayb::convert(input_digits, input_base, output_base),
Ok(output_digits)
);
}
#[test]
#[ignore]
fn trinary_to_hexadecimal() {
let input_base = 3;
let input_digits = &[1, 1, 2, 0];
let output_base = 16;
let output_digits = vec![2, 10];
assert_eq!(
ayb::convert(input_digits, input_base, output_base),
Ok(output_digits)
);
}
#[test]
#[ignore]
fn hexadecimal_to_trinary() {
let input_base = 16;
let input_digits = &[2, 10];
let output_base = 3;
let output_digits = vec![1, 1, 2, 0];
assert_eq!(
ayb::convert(input_digits, input_base, output_base),
Ok(output_digits)
);
}
#[test]
#[ignore]
fn fifteen_bit_integer() {
let input_base = 97;
let input_digits = &[3, 46, 60];
let output_base = 73;
let output_digits = vec![6, 10, 45];
assert_eq!(
ayb::convert(input_digits, input_base, output_base),
Ok(output_digits)
);
}
#[test]
#[ignore]
fn empty_list() {
let input_base = 2;
let input_digits = &[];
let output_base = 10;
let output_digits = vec![0];
assert_eq!(
ayb::convert(input_digits, input_base, output_base),
Ok(output_digits)
);
}
#[test]
#[ignore]
fn single_zero() {
let input_base = 10;
let input_digits = &[0];
let output_base = 2;
let output_digits = vec![0];
assert_eq!(
ayb::convert(input_digits, input_base, output_base),
Ok(output_digits)
);
}
#[test]
#[ignore]
fn multiple_zeros() {
let input_base = 10;
let input_digits = &[0, 0, 0];
let output_base = 2;
let output_digits = vec![0];
assert_eq!(
ayb::convert(input_digits, input_base, output_base),
Ok(output_digits)
);
}
#[test]
#[ignore]
fn leading_zeros() {
let input_base = 7;
let input_digits = &[0, 6, 0];
let output_base = 10;
let output_digits = vec![4, 2];
assert_eq!(
ayb::convert(input_digits, input_base, output_base),
Ok(output_digits)
);
}
#[test]
#[ignore]
fn invalid_positive_digit() {
let input_base = 2;
let input_digits = &[1, 2, 1, 0, 1, 0];
let output_base = 10;
assert_eq!(
ayb::convert(input_digits, input_base, output_base),
Err(ayb::Error::InvalidDigit(2))
);
}
#[test]
#[ignore]
fn input_base_is_one() {
let input_base = 1;
let input_digits = &[];
let output_base = 10;
assert_eq!(
ayb::convert(input_digits, input_base, output_base),
Err(ayb::Error::InvalidInputBase)
);
}
#[test]
#[ignore]
fn output_base_is_one() {
let input_base = 2;
let input_digits = &[1, 0, 1, 0, 1, 0];
let output_base = 1;
assert_eq!(
ayb::convert(input_digits, input_base, output_base),
Err(ayb::Error::InvalidOutputBase)
);
}
#[test]
#[ignore]
fn input_base_is_zero() {
let input_base = 0;
let input_digits = &[];
let output_base = 10;
assert_eq!(
ayb::convert(input_digits, input_base, output_base),
Err(ayb::Error::InvalidInputBase)
);
}
#[test]
#[ignore]
fn output_base_is_zero() {
let input_base = 10;
let input_digits = &[7];
let output_base = 0;
assert_eq!(
ayb::convert(input_digits, input_base, output_base),
Err(ayb::Error::InvalidOutputBase)
);
}