Skip to content

Commit a126967

Browse files
committed
Add exercise all-your-base.
1 parent b89aec5 commit a126967

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed

all-your-base.json

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
{
2+
"#": [ "It's up to each track do decide:"
3+
, ""
4+
, "1. What's the canonical representation of zero?"
5+
, " - []?"
6+
, " - [0]?"
7+
, ""
8+
, "2. What representations of zero are allowed?"
9+
, " - []?"
10+
, " - [0]?"
11+
, " - [0,0]?"
12+
, ""
13+
, "3. Are leading zeroes allowed?"
14+
, ""
15+
, "4. How should invalid input be handled?"
16+
, ""
17+
, "All the undefined cases are marked as null."
18+
, ""
19+
, "All your numeric-base are belong to [2..]. :)"
20+
],
21+
"cases": [
22+
{ "description" : "single bit one to decimal"
23+
, "input_base" : 2
24+
, "input_digits" : [1]
25+
, "output_base" : 10
26+
, "output_digits": [1]
27+
},
28+
{ "description" : "binary to single decimal"
29+
, "input_base" : 2
30+
, "input_digits" : [1, 0, 1]
31+
, "output_base" : 10
32+
, "output_digits": [5]
33+
},
34+
{ "description" : "single decimal to binary"
35+
, "input_base" : 10
36+
, "input_digits" : [5]
37+
, "output_base" : 2
38+
, "output_digits": [1, 0, 1]
39+
},
40+
{ "description" : "binary to multiple decimal"
41+
, "input_base" : 2
42+
, "input_digits" : [1, 0, 1, 0, 1, 0]
43+
, "output_base" : 10
44+
, "output_digits": [4, 2]
45+
},
46+
{ "description" : "decimal to binary"
47+
, "input_base" : 10
48+
, "input_digits" : [4, 2]
49+
, "output_base" : 2
50+
, "output_digits": [1, 0, 1, 0, 1, 0]
51+
},
52+
{ "description" : "trinary to hexadecimal"
53+
, "input_base" : 3
54+
, "input_digits" : [1, 1, 2, 0]
55+
, "output_base" : 16
56+
, "output_digits": [2, 10]
57+
},
58+
{ "description" : "hexadecimal to trinary"
59+
, "input_base" : 16
60+
, "input_digits" : [2, 10]
61+
, "output_base" : 3
62+
, "output_digits": [1, 1, 2, 0]
63+
},
64+
{ "description" : "15-bit integer"
65+
, "input_base" : 97
66+
, "input_digits" : [3,46,60]
67+
, "output_base" : 73
68+
, "output_digits": [6,10,45]
69+
},
70+
{ "description" : "empty list"
71+
, "input_base" : 2
72+
, "input_digits" : []
73+
, "output_base" : 10
74+
, "output_digits": null
75+
},
76+
{ "description" : "single zero"
77+
, "input_base" : 10
78+
, "input_digits" : [0]
79+
, "output_base" : 2
80+
, "output_digits": null
81+
},
82+
{ "description" : "multiple zeros"
83+
, "input_base" : 10
84+
, "input_digits" : [0, 0, 0]
85+
, "output_base" : 2
86+
, "output_digits": null
87+
},
88+
{ "description" : "leading zeros"
89+
, "input_base" : 7
90+
, "input_digits" : [0, 6, 0]
91+
, "output_base" : 10
92+
, "output_digits": null
93+
},
94+
{ "description" : "negative digit"
95+
, "input_base" : 2
96+
, "input_digits" : [1, -1, 1, 0, 1, 0]
97+
, "output_base" : 10
98+
, "output_digits": null
99+
},
100+
{ "description" : "invalid positive digit"
101+
, "input_base" : 2
102+
, "input_digits" : [1, 2, 1, 0, 1, 0]
103+
, "output_base" : 10
104+
, "output_digits": null
105+
},
106+
{ "description" : "first base is one"
107+
, "input_base" : 1
108+
, "input_digits" : []
109+
, "output_base" : 10
110+
, "output_digits": null
111+
},
112+
{ "description" : "second base is one"
113+
, "input_base" : 2
114+
, "input_digits" : [1, 0, 1, 0, 1, 0]
115+
, "output_base" : 1
116+
, "output_digits": null
117+
},
118+
{ "description" : "first base is zero"
119+
, "input_base" : 0
120+
, "input_digits" : []
121+
, "output_base" : 10
122+
, "output_digits": null
123+
},
124+
{ "description" : "second base is zero"
125+
, "input_base" : 10
126+
, "input_digits" : [7]
127+
, "output_base" : 0
128+
, "output_digits": null
129+
},
130+
{ "description" : "first base is negative"
131+
, "input_base" : -2
132+
, "input_digits" : [1]
133+
, "output_base" : 10
134+
, "output_digits": null
135+
},
136+
{ "description" : "second base is negative"
137+
, "input_base" : 2
138+
, "input_digits" : [1]
139+
, "output_base" : -7
140+
, "output_digits": null
141+
},
142+
{ "description" : "both bases are negative"
143+
, "input_base" : -2
144+
, "input_digits" : [1]
145+
, "output_base" : -7
146+
, "output_digits": null
147+
} ]
148+
}

all-your-base.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Implement general base conversion. Given a number in base **a**,
2+
represented as a sequence of digits, convert it to base **b**.
3+
4+
## Note
5+
- Try to implement the conversion yourself.
6+
Do not use something else to perform the conversion for you.
7+
8+
## About [Positional Notation](https://en.wikipedia.org/wiki/Positional_notation)
9+
10+
In positional notation, a number in base **b** can be understood as a linear
11+
combination of powers of **b**.
12+
13+
The number 42, *in base 10*, means:
14+
15+
(4 * 10^1) + (2 * 10^0)
16+
17+
The number 101010, *in base 2*, means:
18+
19+
(1 * 2^5) + (0 * 2^4) + (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (0 * 2^0)
20+
21+
The number 112, *in base 3*, means:
22+
23+
(1 * 3^2) + (1 * 3^1) + (2 * 3^0)
24+
25+
I think you got the idea!
26+
27+
28+
*Yes. Those three numbers above are exactly the same. Congratulations!*

all-your-base.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
blurb: "Write a program that will convert a number, represented as a sequence of digits in one base, to any other base."

0 commit comments

Comments
 (0)