diff --git a/all-your-base.json b/all-your-base.json new file mode 100644 index 0000000000..f84ee3bb59 --- /dev/null +++ b/all-your-base.json @@ -0,0 +1,148 @@ +{ + "#": [ "It's up to each track do decide:" + , "" + , "1. What's the canonical representation of zero?" + , " - []?" + , " - [0]?" + , "" + , "2. What representations of zero are allowed?" + , " - []?" + , " - [0]?" + , " - [0,0]?" + , "" + , "3. Are leading zeroes allowed?" + , "" + , "4. How should invalid input be handled?" + , "" + , "All the undefined cases are marked as null." + , "" + , "All your numeric-base are belong to [2..]. :)" + ], + "cases": [ + { "description" : "single bit one to decimal" + , "input_base" : 2 + , "input_digits" : [1] + , "output_base" : 10 + , "output_digits": [1] + }, + { "description" : "binary to single decimal" + , "input_base" : 2 + , "input_digits" : [1, 0, 1] + , "output_base" : 10 + , "output_digits": [5] + }, + { "description" : "single decimal to binary" + , "input_base" : 10 + , "input_digits" : [5] + , "output_base" : 2 + , "output_digits": [1, 0, 1] + }, + { "description" : "binary to multiple decimal" + , "input_base" : 2 + , "input_digits" : [1, 0, 1, 0, 1, 0] + , "output_base" : 10 + , "output_digits": [4, 2] + }, + { "description" : "decimal to binary" + , "input_base" : 10 + , "input_digits" : [4, 2] + , "output_base" : 2 + , "output_digits": [1, 0, 1, 0, 1, 0] + }, + { "description" : "trinary to hexadecimal" + , "input_base" : 3 + , "input_digits" : [1, 1, 2, 0] + , "output_base" : 16 + , "output_digits": [2, 10] + }, + { "description" : "hexadecimal to trinary" + , "input_base" : 16 + , "input_digits" : [2, 10] + , "output_base" : 3 + , "output_digits": [1, 1, 2, 0] + }, + { "description" : "15-bit integer" + , "input_base" : 97 + , "input_digits" : [3,46,60] + , "output_base" : 73 + , "output_digits": [6,10,45] + }, + { "description" : "empty list" + , "input_base" : 2 + , "input_digits" : [] + , "output_base" : 10 + , "output_digits": null + }, + { "description" : "single zero" + , "input_base" : 10 + , "input_digits" : [0] + , "output_base" : 2 + , "output_digits": null + }, + { "description" : "multiple zeros" + , "input_base" : 10 + , "input_digits" : [0, 0, 0] + , "output_base" : 2 + , "output_digits": null + }, + { "description" : "leading zeros" + , "input_base" : 7 + , "input_digits" : [0, 6, 0] + , "output_base" : 10 + , "output_digits": null + }, + { "description" : "negative digit" + , "input_base" : 2 + , "input_digits" : [1, -1, 1, 0, 1, 0] + , "output_base" : 10 + , "output_digits": null + }, + { "description" : "invalid positive digit" + , "input_base" : 2 + , "input_digits" : [1, 2, 1, 0, 1, 0] + , "output_base" : 10 + , "output_digits": null + }, + { "description" : "first base is one" + , "input_base" : 1 + , "input_digits" : [] + , "output_base" : 10 + , "output_digits": null + }, + { "description" : "second base is one" + , "input_base" : 2 + , "input_digits" : [1, 0, 1, 0, 1, 0] + , "output_base" : 1 + , "output_digits": null + }, + { "description" : "first base is zero" + , "input_base" : 0 + , "input_digits" : [] + , "output_base" : 10 + , "output_digits": null + }, + { "description" : "second base is zero" + , "input_base" : 10 + , "input_digits" : [7] + , "output_base" : 0 + , "output_digits": null + }, + { "description" : "first base is negative" + , "input_base" : -2 + , "input_digits" : [1] + , "output_base" : 10 + , "output_digits": null + }, + { "description" : "second base is negative" + , "input_base" : 2 + , "input_digits" : [1] + , "output_base" : -7 + , "output_digits": null + }, + { "description" : "both bases are negative" + , "input_base" : -2 + , "input_digits" : [1] + , "output_base" : -7 + , "output_digits": null + } ] +} diff --git a/all-your-base.md b/all-your-base.md new file mode 100644 index 0000000000..6847424d9e --- /dev/null +++ b/all-your-base.md @@ -0,0 +1,28 @@ +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 112, *in base 3*, means: + +(1 * 3^2) + (1 * 3^1) + (2 * 3^0) + +I think you got the idea! + + +*Yes. Those three numbers above are exactly the same. Congratulations!* diff --git a/all-your-base.yml b/all-your-base.yml new file mode 100644 index 0000000000..8f210e8a09 --- /dev/null +++ b/all-your-base.yml @@ -0,0 +1,2 @@ +--- +blurb: "Write a program that will convert a number, represented as a sequence of digits in one base, to any other base."