Skip to content

Commit d109704

Browse files
authored
Add all-your-base exercise (#343)
* Add all-your-base exercise * change implementation to just a single function
1 parent fc14fb6 commit d109704

File tree

8 files changed

+348
-0
lines changed

8 files changed

+348
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,14 @@
730730
"practices": [],
731731
"prerequisites": [],
732732
"difficulty": 5
733+
},
734+
{
735+
"slug": "all-your-base",
736+
"name": "All Your Base",
737+
"uuid": "b3bfbbd9-a0f4-4cbf-848b-f889bb0241a0",
738+
"practices": [],
739+
"prerequisites": [],
740+
"difficulty": 3
733741
}
734742
]
735743
},
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Instructions
2+
3+
Convert a sequence of digits in one base, representing a number, into a sequence of digits in another base, representing the same number.
4+
5+
~~~~exercism/note
6+
Try to implement the conversion yourself.
7+
Do not use something else to perform the conversion for you.
8+
~~~~
9+
10+
## About [Positional Notation][positional-notation]
11+
12+
In positional notation, a number in base **b** can be understood as a linear combination of powers of **b**.
13+
14+
The number 42, _in base 10_, means:
15+
16+
`(4 × 10¹) + (2 × 10⁰)`
17+
18+
The number 101010, _in base 2_, means:
19+
20+
`(1 × 2⁵) + (0 × 2⁴) + (1 × 2³) + (0 × 2²) + (1 × 2¹) + (0 × 2⁰)`
21+
22+
The number 1120, _in base 3_, means:
23+
24+
`(1 × 3³) + (1 × 3²) + (2 × 3¹) + (0 × 3⁰)`
25+
26+
_Yes. Those three numbers above are exactly the same. Congratulations!_
27+
28+
[positional-notation]: https://en.wikipedia.org/wiki/Positional_notation
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Introduction
2+
3+
You've just been hired as professor of mathematics.
4+
Your first week went well, but something is off in your second week.
5+
The problem is that every answer given by your students is wrong!
6+
Luckily, your math skills have allowed you to identify the problem: the student answers _are_ correct, but they're all in base 2 (binary)!
7+
Amazingly, it turns out that each week, the students use a different base.
8+
To help you quickly verify the student answers, you'll be building a tool to translate between bases.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"all-your-base.coffee"
8+
],
9+
"test": [
10+
"all-your-base.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
14+
]
15+
},
16+
"blurb": "Convert a number, represented as a sequence of digits in one base, to any other base."
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
allYourBase = ({inputBase, outputBase, digits}) ->
2+
throw new Error "input base must be >= 2" unless inputBase >= 2
3+
throw new Error "output base must be >= 2" unless outputBase >= 2
4+
5+
if digits.some ((d) => d < 0 or d >= inputBase)
6+
throw new Error "all digits must satisfy 0 <= d < input base"
7+
8+
decimal = digits.reduce ((dec, d) => dec * inputBase + d), 0
9+
10+
if decimal is 0
11+
[0]
12+
else
13+
outputDigits = []
14+
while decimal > 0
15+
outputDigits.unshift decimal % outputBase
16+
decimal //= outputBase
17+
outputDigits
18+
19+
20+
module.exports = allYourBase
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[5ce422f9-7a4b-4f44-ad29-49c67cb32d2c]
13+
description = "single bit one to decimal"
14+
15+
[0cc3fea8-bb79-46ac-a2ab-5a2c93051033]
16+
description = "binary to single decimal"
17+
18+
[f12db0f9-0d3d-42c2-b3ba-e38cb375a2b8]
19+
description = "single decimal to binary"
20+
21+
[2c45cf54-6da3-4748-9733-5a3c765d925b]
22+
description = "binary to multiple decimal"
23+
24+
[65ddb8b4-8899-4fcc-8618-181b2cf0002d]
25+
description = "decimal to binary"
26+
27+
[8d418419-02a7-4824-8b7a-352d33c6987e]
28+
description = "trinary to hexadecimal"
29+
30+
[d3901c80-8190-41b9-bd86-38d988efa956]
31+
description = "hexadecimal to trinary"
32+
33+
[5d42f85e-21ad-41bd-b9be-a3e8e4258bbf]
34+
description = "15-bit integer"
35+
36+
[d68788f7-66dd-43f8-a543-f15b6d233f83]
37+
description = "empty list"
38+
39+
[5e27e8da-5862-4c5f-b2a9-26c0382b6be7]
40+
description = "single zero"
41+
42+
[2e1c2573-77e4-4b9c-8517-6c56c5bcfdf2]
43+
description = "multiple zeros"
44+
45+
[3530cd9f-8d6d-43f5-bc6e-b30b1db9629b]
46+
description = "leading zeros"
47+
48+
[a6b476a1-1901-4f2a-92c4-4d91917ae023]
49+
description = "input base is one"
50+
51+
[e21a693a-7a69-450b-b393-27415c26a016]
52+
description = "input base is zero"
53+
54+
[54a23be5-d99e-41cc-88e0-a650ffe5fcc2]
55+
description = "input base is negative"
56+
57+
[9eccf60c-dcc9-407b-95d8-c37b8be56bb6]
58+
description = "negative digit"
59+
60+
[232fa4a5-e761-4939-ba0c-ed046cd0676a]
61+
description = "invalid positive digit"
62+
63+
[14238f95-45da-41dc-95ce-18f860b30ad3]
64+
description = "output base is one"
65+
66+
[73dac367-da5c-4a37-95fe-c87fad0a4047]
67+
description = "output base is zero"
68+
69+
[13f81f42-ff53-4e24-89d9-37603a48ebd9]
70+
description = "output base is negative"
71+
72+
[0e6c895d-8a5d-4868-a345-309d094cfe8d]
73+
description = "both bases are negative"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
allYourBase = (args) ->
2+
3+
module.exports = allYourBase
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
allYourBase = require './all-your-base'
2+
3+
describe "AllYourBase", ->
4+
it "single bit one to decimal", ->
5+
actual = allYourBase {
6+
inputBase: 2,
7+
outputBase: 10,
8+
digits: [1]
9+
}
10+
expected = [1]
11+
expect(actual).toEqual expected
12+
13+
xit "binary to single decimal", ->
14+
actual = allYourBase {
15+
inputBase: 2,
16+
outputBase: 10,
17+
digits: [1, 0, 1]
18+
}
19+
expected = [5]
20+
expect(actual).toEqual expected
21+
22+
xit "single decimal to binary", ->
23+
actual = allYourBase {
24+
inputBase: 10,
25+
outputBase: 2,
26+
digits: [5]
27+
}
28+
expected = [1, 0, 1]
29+
expect(actual).toEqual expected
30+
31+
xit "binary to multiple decimal", ->
32+
actual = allYourBase {
33+
inputBase: 2,
34+
outputBase: 10,
35+
digits: [1, 0, 1, 0, 1, 0]
36+
}
37+
expected = [4, 2]
38+
expect(actual).toEqual expected
39+
40+
xit "decimal to binary", ->
41+
actual = allYourBase {
42+
inputBase: 10,
43+
outputBase: 2,
44+
digits: [4, 2]
45+
}
46+
expected = [1, 0, 1, 0, 1, 0]
47+
expect(actual).toEqual expected
48+
49+
xit "trinary to hexadecimal", ->
50+
actual = allYourBase {
51+
inputBase: 3,
52+
outputBase: 16,
53+
digits: [1, 1, 2, 0]
54+
}
55+
expected = [2, 10]
56+
expect(actual).toEqual expected
57+
58+
xit "hexadecimal to trinary", ->
59+
actual = allYourBase {
60+
inputBase: 16,
61+
outputBase: 3,
62+
digits: [2, 10]
63+
}
64+
expected = [1, 1, 2, 0]
65+
expect(actual).toEqual expected
66+
67+
xit "15-bit integer", ->
68+
actual = allYourBase {
69+
inputBase: 97,
70+
outputBase: 73,
71+
digits: [3, 46, 60]
72+
}
73+
expected = [6, 10, 45]
74+
expect(actual).toEqual expected
75+
76+
xit "empty list", ->
77+
actual = allYourBase {
78+
inputBase: 2,
79+
outputBase: 10,
80+
digits: []
81+
}
82+
expected = [0]
83+
expect(actual).toEqual expected
84+
85+
xit "single zero", ->
86+
actual = allYourBase {
87+
inputBase: 10,
88+
outputBase: 2,
89+
digits: [0]
90+
}
91+
expected = [0]
92+
expect(actual).toEqual expected
93+
94+
xit "multiple zeros", ->
95+
actual = allYourBase {
96+
inputBase: 10,
97+
outputBase: 2,
98+
digits: [0, 0, 0]
99+
}
100+
expected = [0]
101+
expect(actual).toEqual expected
102+
103+
xit "leading zeros", ->
104+
actual = allYourBase {
105+
inputBase: 7,
106+
outputBase: 10,
107+
digits: [0, 6, 0]
108+
}
109+
expected = [4, 2]
110+
expect(actual).toEqual expected
111+
112+
xit "input base is one", ->
113+
expect ->
114+
allYourBase {
115+
inputBase: 1,
116+
outputBase: 10,
117+
digits: [0]
118+
}
119+
.toThrow new Error "input base must be >= 2"
120+
121+
xit "input base is zero", ->
122+
expect ->
123+
allYourBase {
124+
inputBase: 0,
125+
outputBase: 10,
126+
digits: []
127+
}
128+
.toThrow new Error "input base must be >= 2"
129+
130+
xit "input base is negative", ->
131+
expect ->
132+
allYourBase {
133+
inputBase: -2,
134+
outputBase: 10,
135+
digits: [0]
136+
}
137+
.toThrow new Error "input base must be >= 2"
138+
139+
xit "negative digit", ->
140+
expect ->
141+
allYourBase {
142+
inputBase: 2,
143+
outputBase: 10,
144+
digits: [1, -1, 1, 0, 1, 0]
145+
}
146+
.toThrow new Error "all digits must satisfy 0 <= d < input base"
147+
148+
xit "invalid positive digit", ->
149+
expect ->
150+
allYourBase {
151+
inputBase: 2,
152+
outputBase: 10,
153+
digits: [1, 2, 1, 0, 1, 0]
154+
}
155+
.toThrow new Error "all digits must satisfy 0 <= d < input base"
156+
157+
xit "output base is one", ->
158+
expect ->
159+
allYourBase {
160+
inputBase: 2,
161+
outputBase: 1,
162+
digits: [1, 0, 1, 0, 1, 0]
163+
}
164+
.toThrow new Error "output base must be >= 2"
165+
166+
xit "output base is zero", ->
167+
expect ->
168+
allYourBase {
169+
inputBase: 10,
170+
outputBase: 0,
171+
digits: [7]
172+
}
173+
.toThrow new Error "output base must be >= 2"
174+
175+
xit "output base is negative", ->
176+
expect ->
177+
allYourBase {
178+
inputBase: 2,
179+
outputBase: -7,
180+
digits: [1]
181+
}
182+
.toThrow new Error "output base must be >= 2"
183+
184+
xit "both bases are negative", ->
185+
expect ->
186+
allYourBase {
187+
inputBase: -2,
188+
outputBase: -7,
189+
digits: [1]
190+
}
191+
.toThrow new Error "input base must be >= 2"

0 commit comments

Comments
 (0)