Skip to content

Commit 7a75b7d

Browse files
all your base exercise (#18)
1 parent 1e94f63 commit 7a75b7d

8 files changed

Lines changed: 189 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@
125125
"practices": [],
126126
"prerequisites": [],
127127
"difficulty": 2
128+
},
129+
{
130+
"slug": "all-your-base",
131+
"name": "All Your Base",
132+
"uuid": "b858df1d-c175-4bc2-a6f8-b4f4339d60c2",
133+
"practices": [],
134+
"prerequisites": [],
135+
"difficulty": 5
128136
}
129137
]
130138
},
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+
"quintuple-mallard"
4+
],
5+
"files": {
6+
"solution": [
7+
"all-your-base.nu"
8+
],
9+
"test": [
10+
"tests.nu"
11+
],
12+
"example": [
13+
".meta/example.nu"
14+
]
15+
},
16+
"blurb": "Convert a number, represented as a sequence of digits in one base, to any other base."
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std/assert
2+
def base_ten [base: number]: list<number> -> number {
3+
$in | reverse | enumerate | reduce --fold 0 {|el, acc|
4+
let i = $el.index
5+
let digit = $el.item
6+
$acc + $digit * $base ** $i
7+
}
8+
}
9+
def rebase_from_ten [base: number]: number -> list<number> {
10+
mut num = $in
11+
mut out = []
12+
while $num > 0 {
13+
let digit = $num mod $base
14+
$out ++= [$digit]
15+
$num -= $digit
16+
$num /= $base
17+
}
18+
$out | reverse
19+
}
20+
export def rebase [--input-base (-i): number, --output-base (-o): number]: list<number> -> list<number> {
21+
assert ($input_base >= 2) "input base must be >= 2"
22+
assert ($output_base >= 2) "output base must be >= 2"
23+
let number = $in
24+
assert ($number | all {|d| 0 <= $d and $d < $input_base }) "all digits must satisfy 0 <= d < input base"
25+
let number = $number | base_ten $input_base
26+
let rebased = $number | rebase_from_ten $output_base
27+
if $rebased == [] { [0] } else $rebased
28+
}
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export def rebase [--input-base (-i): number, --output-base (-o): number]: list<number> -> number {
2+
error make {msg: "Please implement rebase"}
3+
NaN # This is to prevent the typechecker from erroring
4+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std/assert
2+
use all-your-base.nu rebase
3+
assert equal ([1] | rebase -i 2 -o 10) [1]
4+
assert equal ([1, 0, 1] | rebase -i 2 -o 10) [5]
5+
assert equal ([5] | rebase -i 10 -o 2) [1, 0, 1]
6+
assert equal ([1, 0, 1, 0, 1, 0] | rebase -i 2 -o 10) [4, 2]
7+
assert equal ([4, 2] | rebase -i 10 -o 2) [1, 0, 1, 0, 1, 0]
8+
assert equal ([1, 1, 2, 0] | rebase -i 3 -o 16) [2, 10]
9+
assert equal ([2, 10] | rebase -i 16 -o 3) [1, 1, 2, 0]
10+
assert equal ([3, 46, 60] | rebase -i 97 -o 73) [6, 10, 45]
11+
assert equal ([] | rebase -i 2 -o 10) [0]
12+
assert equal ([0] | rebase -i 10 -o 2) [0]
13+
assert equal ([0, 0, 0] | rebase -i 10 -o 2) [0]
14+
assert equal ([0, 6, 0] | rebase -i 7 -o 10) [4, 2]
15+
assert equal (try { [0] | rebase -i 1 -o 10 } catch {|e| $e.msg}) "input base must be >= 2"
16+
assert equal (try { [] | rebase -i 0 -o 10 } catch {|e| $e.msg}) "input base must be >= 2"
17+
assert equal (try { [1] | rebase -i -2 -o 10 } catch {|e| $e.msg}) "input base must be >= 2"
18+
assert equal (try { [1,-1, 1, 0, 1, 0] | rebase -i 2 -o 10 } catch {|e| $e.msg}) "all digits must satisfy 0 <= d < input base"
19+
assert equal (try { [1, 2, 1, 0, 1, 0] | rebase -i 2 -o 10 } catch {|e| $e.msg}) "all digits must satisfy 0 <= d < input base"
20+
assert equal (try { [1, 0, 1, 0, 1, 0] | rebase -i 2 -o 1 } catch {|e| $e.msg}) "output base must be >= 2"
21+
assert equal (try { [7] | rebase -i 10 -o 0 } catch {|e| $e.msg}) "output base must be >= 2"
22+
assert equal (try { [1] | rebase -i 2 -o -7 } catch {|e| $e.msg}) "output base must be >= 2"
23+
assert equal (try { [1] | rebase -i -2 -o -7 } catch {|e| $e.msg}) "input base must be >= 2"

0 commit comments

Comments
 (0)