Skip to content

Commit b2f7222

Browse files
Add all-your-base exercise (#47)
1 parent d42c4e9 commit b2f7222

File tree

8 files changed

+261
-0
lines changed

8 files changed

+261
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,14 @@
214214
"prerequisites": [],
215215
"difficulty": 3
216216
},
217+
{
218+
"slug": "all-your-base",
219+
"name": "All Your Base",
220+
"uuid": "8e88bcf3-a492-4a09-8c3c-33718446ec1a",
221+
"practices": [],
222+
"prerequisites": [],
223+
"difficulty": 4
224+
},
217225
{
218226
"slug": "armstrong-numbers",
219227
"name": "Armstrong Numbers",
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+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"all_your_base.fut"
8+
],
9+
"test": [
10+
"test.fut"
11+
],
12+
"example": [
13+
".meta/example.fut"
14+
]
15+
},
16+
"blurb": "Convert a number, represented as a sequence of digits in one base, to any other base."
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def unify (input_base: i32) (digits: []i32): i32 =
2+
let n = loop n = 0 for digit in digits do
3+
assert (digit >= 0 && digit < input_base) (n * input_base + digit)
4+
in
5+
n
6+
7+
def separate (output_base: i32) (number: i32): []i32 =
8+
if number == 0 then [0] else
9+
let (_, a, i) = loop (n, a, i) = (number, replicate 32 0, 32) while n > 0 do
10+
(n / output_base, a with [i - 1] = n % output_base, i - 1)
11+
in
12+
a[i:]
13+
14+
def rebase (input_base: i32) (digits: []i32) (output_base: i32): []i32 =
15+
assert (input_base >= 2 && output_base >= 2) digits
16+
|> unify input_base
17+
|> separate output_base
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def rebase (input_base: i32) (digits: []i32) (output_base: i32): []i32 = ???
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import "all_your_base"
2+
3+
-- single bit one to decimal
4+
-- ==
5+
-- input { 2 [1] 10 }
6+
-- output { [1] }
7+
8+
-- binary to single decimal
9+
-- ==
10+
-- input { 2 [1, 0, 1] 10 }
11+
-- output { [5] }
12+
13+
-- single decimal to binary
14+
-- ==
15+
-- input { 10 [5] 2 }
16+
-- output { [1, 0, 1] }
17+
18+
-- binary to multiple decimal
19+
-- ==
20+
-- input { 2 [1, 0, 1, 0, 1, 0] 10 }
21+
-- output { [4, 2] }
22+
23+
-- decimal to binary
24+
-- ==
25+
-- input { 10 [4, 2] 2 }
26+
-- output { [1, 0, 1, 0, 1, 0] }
27+
28+
-- trinary to hexadecimal
29+
-- ==
30+
-- input { 3 [1, 1, 2, 0] 16 }
31+
-- output { [2, 10] }
32+
33+
-- hexadecimal to trinary
34+
-- ==
35+
-- input { 16 [2, 10] 3 }
36+
-- output { [1, 1, 2, 0] }
37+
38+
-- 15-bit integer
39+
-- ==
40+
-- input { 97 [3, 46, 60] 73 }
41+
-- output { [6, 10, 45] }
42+
43+
-- empty list
44+
-- ==
45+
-- input { 2 empty([0]i32) 10 }
46+
-- output { [0] }
47+
48+
-- single zero
49+
-- ==
50+
-- input { 10 [0] 2 }
51+
-- output { [0] }
52+
53+
-- multiple zeros
54+
-- ==
55+
-- input { 10 [0, 0, 0] 2 }
56+
-- output { [0] }
57+
58+
-- leading zeros
59+
-- ==
60+
-- input { 7 [0, 6, 0] 10 }
61+
-- output { [4, 2] }
62+
63+
-- input base is one
64+
-- ==
65+
-- input { 1 [0] 10 }
66+
-- error: Error*
67+
68+
-- input base is zero
69+
-- ==
70+
-- input { 0 empty([0]i32) 10 }
71+
-- error: Error*
72+
73+
-- input base is negative
74+
-- ==
75+
-- input { -2 [1] 10 }
76+
-- error: Error*
77+
78+
-- negative digit
79+
-- ==
80+
-- input { 2 [1, -1, 1, 0, 1, 0] 10 }
81+
-- error: Error*
82+
83+
-- invalid positive digit
84+
-- ==
85+
-- input { 2 [1, 2, 1, 0, 1, 0] 10 }
86+
-- error: Error*
87+
88+
-- output base is one
89+
-- ==
90+
-- input { 2 [1, 0, 1, 0, 1, 0] 1 }
91+
-- error: Error*
92+
93+
-- output base is zero
94+
-- ==
95+
-- input { 10 [7] 0 }
96+
-- error: Error*
97+
98+
-- output base is negative
99+
-- ==
100+
-- input { 2 [1] -7 }
101+
-- error: Error*
102+
103+
-- both bases are negative
104+
-- ==
105+
-- input { -2 [1] -7 }
106+
-- error: Error*
107+
108+
def main (input_base: i32) (digits: []i32) (output_base: i32): []i32 =
109+
rebase input_base digits output_base

0 commit comments

Comments
 (0)