-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef20bcf
commit cc5b2d0
Showing
8 changed files
with
366 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
exercises/practice/variable-length-quantity/.docs/instructions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Instructions | ||
|
||
Implement variable length quantity encoding and decoding. | ||
|
||
The goal of this exercise is to implement [VLQ][vlq] encoding/decoding. | ||
|
||
In short, the goal of this encoding is to encode integer values in a way that would save bytes. | ||
Only the first 7 bits of each byte are significant (right-justified; sort of like an ASCII byte). | ||
So, if you have a 32-bit value, you have to unpack it into a series of 7-bit bytes. | ||
Of course, you will have a variable number of bytes depending upon your integer. | ||
To indicate which is the last byte of the series, you leave bit #7 clear. | ||
In all of the preceding bytes, you set bit #7. | ||
|
||
So, if an integer is between `0-127`, it can be represented as one byte. | ||
Although VLQ can deal with numbers of arbitrary sizes, for this exercise we will restrict ourselves to only numbers that fit in a 32-bit unsigned integer. | ||
Here are examples of integers as 32-bit values, and the variable length quantities that they translate to: | ||
|
||
```text | ||
NUMBER VARIABLE QUANTITY | ||
00000000 00 | ||
00000040 40 | ||
0000007F 7F | ||
00000080 81 00 | ||
00002000 C0 00 | ||
00003FFF FF 7F | ||
00004000 81 80 00 | ||
00100000 C0 80 00 | ||
001FFFFF FF FF 7F | ||
00200000 81 80 80 00 | ||
08000000 C0 80 80 00 | ||
0FFFFFFF FF FF FF 7F | ||
``` | ||
|
||
[vlq]: https://en.wikipedia.org/wiki/Variable-length_quantity |
17 changes: 17 additions & 0 deletions
17
exercises/practice/variable-length-quantity/.meta/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"authors": ["ryanplusplus"], | ||
"files": { | ||
"solution": [ | ||
"src/variable_length_quantity.cr" | ||
], | ||
"test": [ | ||
"spec/variable_length_quantity_spec.cr" | ||
], | ||
"example": [ | ||
".meta/src/example.cr" | ||
] | ||
}, | ||
"blurb": "Implement variable length quantity encoding and decoding.", | ||
"source": "A poor Splice developer having to implement MIDI encoding/decoding.", | ||
"source_url": "https://splice.com" | ||
} |
30 changes: 30 additions & 0 deletions
30
exercises/practice/variable-length-quantity/.meta/src/example.cr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module VariableLengthQuantity | ||
def self.decode(bytes : Array(Number)) | ||
values = [] of UInt32 | ||
n : UInt32 = 0 | ||
bytes.each_with_index do |byte, i| | ||
n = n << 7 | ||
n = n + (byte & 0x7f) | ||
if byte & 0x80 == 0 | ||
values << n | ||
n = 0 | ||
end | ||
raise ArgumentError.new unless (i != bytes.size - 1) || (byte & 0x80 == 0) | ||
end | ||
values | ||
end | ||
|
||
def self.encode(values : Array(Number)) | ||
values.flat_map { |value| encode(value) } | ||
end | ||
|
||
private def self.encode(n : Number) | ||
bytes = [n & 0x7f] | ||
n = n >> 7 | ||
while n > 0 | ||
bytes << (n & 0x7f | 0x80) | ||
n = n >> 7 | ||
end | ||
bytes.reverse | ||
end | ||
end |
22 changes: 22 additions & 0 deletions
22
exercises/practice/variable-length-quantity/.meta/test_template.ecr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require "spec" | ||
require "../src/*" | ||
|
||
describe <%= to_capitalized(@json["exercise"].to_s).inspect %> do | ||
<% @json["cases"].as_a.each do |cases| %> | ||
describe <%= cases["description"].inspect %> do | ||
<%- cases["cases"].as_a.each do |cases| %> | ||
<%= status()%> "<%-= cases["description"] %>" do | ||
<% if cases["expected"].as_h? %> | ||
expect_raises(ArgumentError) do | ||
<%= to_capitalized(@json["exercise"].to_s) %>.<%= cases["property"] %>(<%= cases["input"]["integers"].as_a.inspect %>) | ||
end | ||
<% else %> | ||
input = <%= cases["input"]["integers"].as_a.inspect %> | ||
expected = <%= cases["expected"].as_a.inspect %> | ||
<%= to_capitalized(@json["exercise"].to_s) %>.<%= cases["property"] %>(input).should eq(expected) | ||
<% end %> | ||
end | ||
<% end %> | ||
end | ||
<% end %> | ||
end |
88 changes: 88 additions & 0 deletions
88
exercises/practice/variable-length-quantity/.meta/tests.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[35c9db2e-f781-4c52-b73b-8e76427defd0] | ||
description = "Encode a series of integers, producing a series of bytes. -> zero" | ||
|
||
[be44d299-a151-4604-a10e-d4b867f41540] | ||
description = "Encode a series of integers, producing a series of bytes. -> arbitrary single byte" | ||
|
||
[ea399615-d274-4af6-bbef-a1c23c9e1346] | ||
description = "Encode a series of integers, producing a series of bytes. -> largest single byte" | ||
|
||
[77b07086-bd3f-4882-8476-8dcafee79b1c] | ||
description = "Encode a series of integers, producing a series of bytes. -> smallest double byte" | ||
|
||
[63955a49-2690-4e22-a556-0040648d6b2d] | ||
description = "Encode a series of integers, producing a series of bytes. -> arbitrary double byte" | ||
|
||
[29da7031-0067-43d3-83a7-4f14b29ed97a] | ||
description = "Encode a series of integers, producing a series of bytes. -> largest double byte" | ||
|
||
[3345d2e3-79a9-4999-869e-d4856e3a8e01] | ||
description = "Encode a series of integers, producing a series of bytes. -> smallest triple byte" | ||
|
||
[5df0bc2d-2a57-4300-a653-a75ee4bd0bee] | ||
description = "Encode a series of integers, producing a series of bytes. -> arbitrary triple byte" | ||
|
||
[f51d8539-312d-4db1-945c-250222c6aa22] | ||
description = "Encode a series of integers, producing a series of bytes. -> largest triple byte" | ||
|
||
[da78228b-544f-47b7-8bfe-d16b35bbe570] | ||
description = "Encode a series of integers, producing a series of bytes. -> smallest quadruple byte" | ||
|
||
[11ed3469-a933-46f1-996f-2231e05d7bb6] | ||
description = "Encode a series of integers, producing a series of bytes. -> arbitrary quadruple byte" | ||
|
||
[d5f3f3c3-e0f1-4e7f-aad0-18a44f223d1c] | ||
description = "Encode a series of integers, producing a series of bytes. -> largest quadruple byte" | ||
|
||
[91a18b33-24e7-4bfb-bbca-eca78ff4fc47] | ||
description = "Encode a series of integers, producing a series of bytes. -> smallest quintuple byte" | ||
|
||
[5f34ff12-2952-4669-95fe-2d11b693d331] | ||
description = "Encode a series of integers, producing a series of bytes. -> arbitrary quintuple byte" | ||
|
||
[7489694b-88c3-4078-9864-6fe802411009] | ||
description = "Encode a series of integers, producing a series of bytes. -> maximum 32-bit integer input" | ||
|
||
[f9b91821-cada-4a73-9421-3c81d6ff3661] | ||
description = "Encode a series of integers, producing a series of bytes. -> two single-byte values" | ||
|
||
[68694449-25d2-4974-ba75-fa7bb36db212] | ||
description = "Encode a series of integers, producing a series of bytes. -> two multi-byte values" | ||
|
||
[51a06b5c-de1b-4487-9a50-9db1b8930d85] | ||
description = "Encode a series of integers, producing a series of bytes. -> many multi-byte values" | ||
|
||
[baa73993-4514-4915-bac0-f7f585e0e59a] | ||
description = "Decode a series of bytes, producing a series of integers. -> one byte" | ||
|
||
[72e94369-29f9-46f2-8c95-6c5b7a595aee] | ||
description = "Decode a series of bytes, producing a series of integers. -> two bytes" | ||
|
||
[df5a44c4-56f7-464e-a997-1db5f63ce691] | ||
description = "Decode a series of bytes, producing a series of integers. -> three bytes" | ||
|
||
[1bb58684-f2dc-450a-8406-1f3452aa1947] | ||
description = "Decode a series of bytes, producing a series of integers. -> four bytes" | ||
|
||
[cecd5233-49f1-4dd1-a41a-9840a40f09cd] | ||
description = "Decode a series of bytes, producing a series of integers. -> maximum 32-bit integer" | ||
|
||
[e7d74ba3-8b8e-4bcb-858d-d08302e15695] | ||
description = "Decode a series of bytes, producing a series of integers. -> incomplete sequence causes error" | ||
|
||
[aa378291-9043-4724-bc53-aca1b4a3fcb6] | ||
description = "Decode a series of bytes, producing a series of integers. -> incomplete sequence causes error, even if value is zero" | ||
|
||
[a91e6f5a-c64a-48e3-8a75-ce1a81e0ebee] | ||
description = "Decode a series of bytes, producing a series of integers. -> multiple values" |
164 changes: 164 additions & 0 deletions
164
exercises/practice/variable-length-quantity/spec/variable_length_quantity_spec.cr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
require "spec" | ||
require "../src/*" | ||
|
||
describe "VariableLengthQuantity" do | ||
describe "Encode a series of integers, producing a series of bytes." do | ||
it "zero" do | ||
input = [0] | ||
expected = [0] | ||
VariableLengthQuantity.encode(input).should eq(expected) | ||
end | ||
|
||
pending "arbitrary single byte" do | ||
input = [64] | ||
expected = [64] | ||
VariableLengthQuantity.encode(input).should eq(expected) | ||
end | ||
|
||
pending "largest single byte" do | ||
input = [127] | ||
expected = [127] | ||
VariableLengthQuantity.encode(input).should eq(expected) | ||
end | ||
|
||
pending "smallest double byte" do | ||
input = [128] | ||
expected = [129, 0] | ||
VariableLengthQuantity.encode(input).should eq(expected) | ||
end | ||
|
||
pending "arbitrary double byte" do | ||
input = [8192] | ||
expected = [192, 0] | ||
VariableLengthQuantity.encode(input).should eq(expected) | ||
end | ||
|
||
pending "largest double byte" do | ||
input = [16383] | ||
expected = [255, 127] | ||
VariableLengthQuantity.encode(input).should eq(expected) | ||
end | ||
|
||
pending "smallest triple byte" do | ||
input = [16384] | ||
expected = [129, 128, 0] | ||
VariableLengthQuantity.encode(input).should eq(expected) | ||
end | ||
|
||
pending "arbitrary triple byte" do | ||
input = [1048576] | ||
expected = [192, 128, 0] | ||
VariableLengthQuantity.encode(input).should eq(expected) | ||
end | ||
|
||
pending "largest triple byte" do | ||
input = [2097151] | ||
expected = [255, 255, 127] | ||
VariableLengthQuantity.encode(input).should eq(expected) | ||
end | ||
|
||
pending "smallest quadruple byte" do | ||
input = [2097152] | ||
expected = [129, 128, 128, 0] | ||
VariableLengthQuantity.encode(input).should eq(expected) | ||
end | ||
|
||
pending "arbitrary quadruple byte" do | ||
input = [134217728] | ||
expected = [192, 128, 128, 0] | ||
VariableLengthQuantity.encode(input).should eq(expected) | ||
end | ||
|
||
pending "largest quadruple byte" do | ||
input = [268435455] | ||
expected = [255, 255, 255, 127] | ||
VariableLengthQuantity.encode(input).should eq(expected) | ||
end | ||
|
||
pending "smallest quintuple byte" do | ||
input = [268435456] | ||
expected = [129, 128, 128, 128, 0] | ||
VariableLengthQuantity.encode(input).should eq(expected) | ||
end | ||
|
||
pending "arbitrary quintuple byte" do | ||
input = [4278190080] | ||
expected = [143, 248, 128, 128, 0] | ||
VariableLengthQuantity.encode(input).should eq(expected) | ||
end | ||
|
||
pending "maximum 32-bit integer input" do | ||
input = [4294967295] | ||
expected = [143, 255, 255, 255, 127] | ||
VariableLengthQuantity.encode(input).should eq(expected) | ||
end | ||
|
||
pending "two single-byte values" do | ||
input = [64, 127] | ||
expected = [64, 127] | ||
VariableLengthQuantity.encode(input).should eq(expected) | ||
end | ||
|
||
pending "two multi-byte values" do | ||
input = [16384, 1193046] | ||
expected = [129, 128, 0, 200, 232, 86] | ||
VariableLengthQuantity.encode(input).should eq(expected) | ||
end | ||
|
||
pending "many multi-byte values" do | ||
input = [8192, 1193046, 268435455, 0, 16383, 16384] | ||
expected = [192, 0, 200, 232, 86, 255, 255, 255, 127, 0, 255, 127, 129, 128, 0] | ||
VariableLengthQuantity.encode(input).should eq(expected) | ||
end | ||
end | ||
|
||
describe "Decode a series of bytes, producing a series of integers." do | ||
pending "one byte" do | ||
input = [127] | ||
expected = [127] | ||
VariableLengthQuantity.decode(input).should eq(expected) | ||
end | ||
|
||
pending "two bytes" do | ||
input = [192, 0] | ||
expected = [8192] | ||
VariableLengthQuantity.decode(input).should eq(expected) | ||
end | ||
|
||
pending "three bytes" do | ||
input = [255, 255, 127] | ||
expected = [2097151] | ||
VariableLengthQuantity.decode(input).should eq(expected) | ||
end | ||
|
||
pending "four bytes" do | ||
input = [129, 128, 128, 0] | ||
expected = [2097152] | ||
VariableLengthQuantity.decode(input).should eq(expected) | ||
end | ||
|
||
pending "maximum 32-bit integer" do | ||
input = [143, 255, 255, 255, 127] | ||
expected = [4294967295] | ||
VariableLengthQuantity.decode(input).should eq(expected) | ||
end | ||
|
||
pending "incomplete sequence causes error" do | ||
expect_raises(ArgumentError) do | ||
VariableLengthQuantity.decode([255]) | ||
end | ||
end | ||
|
||
pending "incomplete sequence causes error, even if value is zero" do | ||
expect_raises(ArgumentError) do | ||
VariableLengthQuantity.decode([128]) | ||
end | ||
end | ||
|
||
pending "multiple values" do | ||
input = [192, 0, 200, 232, 86, 255, 255, 255, 127, 0, 255, 127, 129, 128, 0] | ||
expected = [8192, 1193046, 268435455, 0, 16383, 16384] | ||
VariableLengthQuantity.decode(input).should eq(expected) | ||
end | ||
end | ||
end |
3 changes: 3 additions & 0 deletions
3
exercises/practice/variable-length-quantity/src/variable_length_quantity.cr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module VariableLengthQuantity | ||
# Write your code for the 'Variable Length Quantity' exercise in this file. | ||
end |