Skip to content
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

Add exercise all-your-base. #280

Merged
merged 1 commit into from
Jun 29, 2016
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
148 changes: 148 additions & 0 deletions all-your-base.json
Original file line number Diff line number Diff line change
@@ -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
} ]
}
28 changes: 28 additions & 0 deletions all-your-base.md
Original file line number Diff line number Diff line change
@@ -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!*
2 changes: 2 additions & 0 deletions all-your-base.yml
Original file line number Diff line number Diff line change
@@ -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."