1
0
Fork 0

Update /r/dailyprogrammer challenge #285 [Easy]

This commit is contained in:
Nguyễn Gia Phong 2016-10-10 10:55:59 +07:00
parent 4bc9c7d398
commit de5650a437
15 changed files with 295 additions and 385 deletions

View File

@ -2,8 +2,49 @@
var
fi, fo: text;
n, m, i, j: byte;
s: string[4];
a: array[1 .. 4] of byte;
begin
assign(fi, '1dec.inp');
reset(fi);
assign(fo, '1dec.out');
rewrite(fo);
repeat
read(f
read(fi, s[1]);
n := ord(s[1]) - 32;
if n mod 3 > 0 then
n := n div 3 + 1
else
n := n div 3;
for i := 1 to n do
begin
read(fi, s);
for j := 1 to 4 do
a[j] := ord(s[j]) - 32;
m := a[1] * 4 + a[2] div 16;
if m = 0 then
break;
write(fo, chr(m));
m := a[2] mod 16 * 16 + a[3] div 4;
if m = 0 then
break;
write(fo, chr(m));
m := a[3] mod 4 * 64 + a[4];
if m = 0 then
break;
write(fo, chr(m));
end;
readln(fi)
until eof(fi);
close(fi);
close(fo)
end.

Binary file not shown.

View File

@ -1 +0,0 @@
I feel very strongly about you doing duty. Would you give me a little more documentation about your reading in French? I am glad you are happy — but I never believe much in happiness. I never believe in misery either. Those are things you see on the stage or the screen or the printed pages, they never really happen to you in life.

Binary file not shown.

View File

@ -1,8 +0,0 @@
M22!F965L('9E<GD@<W1R;VYG;'D@86)O=70@>6]U(&1O:6YG(&1U='DN(%=O
M=6QD('EO=2!G:79E(&UE(&$@;&ET=&QE(&UO<F4@9&]C=6UE;G1A=&EO;B!A
M8F]U="!Y;W5R(')E861I;F<@:6X@1G)E;F-H/R!)(&%M(&=L860@>6]U(&%R
M92!H87!P>2#B@)0@8G5T($D@;F5V97(@8F5L:65V92!M=6-H(&EN(&AA<'!I
M;F5S<RX@22!N979E<B!B96QI979E(&EN(&UI<V5R>2!E:71H97(N(%1H;W-E
M(&%R92!T:&EN9W,@>6]U('-E92!O;B!T:&4@<W1A9V4@;W(@=&AE('-C<F5E
M;B!O<B!T:&4@<')I;G1E9"!P86=E<RP@=&AE>2!N979E<B!R96%L;'D@:&%P
4<&5N('1O('EO=2!I;B!L:69E+@

View File

@ -2,77 +2,50 @@
var
fi, fo: text;
s: string[3] = 'CnX';
n: cardinal = 0;
i: cardinal;
j: byte;
function enc(s: string): string;
var
c1, c2, c3: byte;
begin
c1 := ord(s[1]);
if length(s) < 2 then
c2 := 0
else
c2 := ord(s[2]);
if length(s) < 3 then
c3 := 0
else
c3 := ord(s[3]);
enc := chr(c1 div 4 + 32)
+ chr(c1 mod 4 * 16 + c2 div 16 + 32)
+ chr(c2 mod 16 * 4 + c3 div 64 + 32)
+ chr(c3 mod 64 + 32)
end;
s: string[45] = ' ';
n, i: shortint;
c1, c2, c3: byte;
begin
assign(fi, '1enc.inp');
reset(fi);
assign(fo, '1enc.out');
reset(fi);
while not eof(fi) do
begin
read(fi, s[1]);
inc(n)
end;
if n = 0 then
writeln(fo, ' ');
reset(fi);
rewrite(fo);
for i := 1 to n div 45 do
begin
write(fo, 'M');
for j := 1 to 15 do
begin
read(fi, s);
write(fo, enc(s))
end;
writeln(fo);
end;
n := n mod 45;
if n > 0 then
begin
write(fo, chr(n + 32));
for i := 1 to n div 3 do
begin
read(fi, s);
write(fo, enc(s))
end;
if n mod 3 > 0 then
begin
read(fi, s);
writeln(fo, enc(s))
end
repeat
n := 0;
while (n < 45) and not(eof(fi)) do
begin
inc(n);
read(fi, s[n])
end;
write(fo, chr(n + 32));
for i := 0 to n div 3 - 1 do
begin
c1 := ord(s[i * 3 + 1]);
c2 := ord(s[i * 3 + 2]);
c3 := ord(s[i * 3 + 3]);
write(fo, chr(c1 div 4 + 32));
write(fo, chr(c1 mod 4 * 16 + c2 div 16 + 32));
write(fo, chr(c2 mod 16 * 4 + c3 div 64 + 32));
write(fo, chr(c3 mod 64 + 32))
end;
if n mod 3 > 0 then
begin
c1 := ord(s[n div 3 * 3 + 1]);
if n mod 3 = 2 then
c2 := ord(s[n div 3 * 3 + 2])
else
writeln(fo)
end;
c2 := 0;
c3 := 0;
write(fo, chr(c1 div 4 + 32));
write(fo, chr(c1 mod 4 * 16 + c2 div 16 + 32));
write(fo, chr(c2 mod 16 * 4 + c3 div 64 + 32));
writeln(fo, chr(c3 mod 64 + 32))
end
until eof(fi);
close(fi);
close(fo)

View File

@ -1,14 +1,22 @@
You are trapped at uninhabited island only with your laptop. Still you don't want your significant other to worry about you, so you are going to send a message in a bottle with your picture or at least a couple of words from you (sure, you could just write down the words, but that would be less fun). You're going to use uuencoding for that.
# [[2016-08-16] Challenge #279 [Easy] Uuencoding](https://www.reddit.com/r/dailyprogrammer/comments/4xy6i1/20160816_challenge_279_easy_uuencoding/)
Uuencoding is a form of binary-to-text encoding, which uses only symbols from 32-95 diapason, which means all symbols used in the encoding are printable.
You are trapped at uninhabited island only with your laptop. Still you don't
want your significant other to worry about you, so you are going to send a
message in a bottle with your picture or at least a couple of words from you
(sure, you could just write down the words, but that would be less fun). You're
going to use uuencoding for that.
#Description of encoding
Uuencoding is a form of binary-to-text encoding, which uses only symbols from
32-95 diapason, which means all symbols used in the encoding are printable.
## Description of encoding
A uuencoded file starts with a header line of the form:
begin <mode> <file><newline>
<mode> is the file's Unix file permissions as three octal digits (e.g. 644, 744). For Windows 644 is always used.
<mode> is the file's Unix file permissions as three octal digits (e.g. 644,
744). For Windows 644 is always used.
<file> is the file name to be used when recreating the binary data.
@ -18,40 +26,50 @@ Each data line uses the format:
<length character><formatted characters><newline>
<length character> is a character indicating the number of data bytes which have been encoded on that line. This is an ASCII character determined by adding 32 to the actual byte count, with the sole exception of a grave accent "`" (ASCII code 96) signifying zero bytes. All data lines except the last (if the data was not divisible by 45), have 45 bytes of encoded data (60 characters after encoding). Therefore, the vast majority of length values is 'M', (32 + 45 = ASCII code 77 or "M").
<length character> is a character indicating the number of data bytes which
have been encoded on that line. This is an ASCII character determined by adding
32 to the actual byte count, with the sole exception of a grave accent "`"
(ASCII code 96) signifying zero bytes. All data lines except the last (if the
data was not divisible by 45), have 45 bytes of encoded data (60 characters
after encoding). Therefore, the vast majority of length values is 'M', (32 + 45
= ASCII code 77 or "M").
<formatted characters> are encoded characters.
The mechanism of uuencoding repeats the following for every 3 bytes (if there are less than 3 bytes left, trailing 0 are added):
The mechanism of uuencoding repeats the following for every 3 bytes (if there
are less than 3 bytes left, trailing 0 are added):
1. Start with 3 bytes from the source, 24 bits in total.
2. Split into 4 6-bit groupings, each representing a value in the range 0 to 63: bits (00-05), (06-11), (12-17) and (18-23).
3. Add 32 to each of the values. With the addition of 32 this means that the possible results can be between 32 (" " space) and 95 ("_" underline). 96 ("`" grave accent) as the "special character" is a logical extension of this range.
2. Split into 4 6-bit groupings, each representing a value in the range 0 to
63: bits (00-05), (06-11), (12-17) and (18-23).
3. Add 32 to each of the values. With the addition of 32 this means that the
possible results can be between 32 (" " space) and 95 ("_" underline). 96
("`" grave accent) as the "special character" is a logical extension of this
range.
4. Output the ASCII equivalent of these numbers.
For example, we want to encode a word "Cat". ASCII values for C,a,t are 67,97,116, or `010000110110000101110100` in binary. After dividing into four groups, we get 010000 110110 000101 110100, which is 16,54,5,52 in decimal. Adding 32 to this values and encoding back in ASCII, the final result is `0V%T`.
For example, we want to encode a word "Cat". ASCII values for C,a,t are
67,97,116, or `010000110110000101110100` in binary. After dividing into four
groups, we get 010000 110110 000101 110100, which is 16,54,5,52 in decimal.
Adding 32 to this values and encoding back in ASCII, the final result is
`0V%T`.
The file ends with two lines:
`<newline>
end<newline>
#Formal Inputs & Outputs
## Formal Inputs & Outputs
##Input
### Input
a byte array or string.
##Output
### Output
a string containing uuencoded input.
#Examples
## Examples
Input: Cat
@ -63,8 +81,12 @@ Output:
end
Input:
I feel very strongly about you doing duty. Would you give me a little more documentation about your reading in French? I am glad you are happy — but I never believe much in happiness. I never believe in misery either. Those are things you see on the stage or the screen or the printed pages, they never really happen to you in life.
I feel very strongly about you doing duty. Would you give me a little more
documentation about your reading in French? I am glad you are happy — but I
never believe much in happiness. I never believe in misery either. Those are
things you see on the stage or the screen or the printed pages, they never
really happen to you in life.
Output:
@ -80,25 +102,26 @@ Output:
`
end
#Bonuses
## Bonuses
##Bonus 1
### Bonus 1
Write uudecoder, which decodes uuencoded input back to a byte array or string
##Bonus 2
### Bonus 2
Write encoder for files as well.
##Bonus 3
### Bonus 3
Make encoding parallel.
#Further Reading
## Further Reading
[Binary-to-text encoding](https://en.wikipedia.org/wiki/Binary-to-text_encoding) on Wikipedia.
[Binary-to-text encoding](https://en.wikipedia.org/wiki/Binary-to-text_encoding)
on Wikipedia.
#Finally
##Finally
This challenge is posted by /u/EvgeniyZh

166
daily/285easy/README.md Normal file
View File

@ -0,0 +1,166 @@
# [[2016-09-26] Challenge #285 [Easy] Cross Platform/Language Data Encoding part 1](https://www.reddit.com/r/dailyprogrammer/comments/54lu54/20160926_challenge_285_easy_cross/)
We will make a binary byte oriented encoding of data that is self describing
and extensible, and aims to solve the following problems:
* Portability between 32 and 64 (and any other) bit systems, and languages, and
endian-ness.
* Type system independent of underlying language.
* Allow heterogeneous arrays (differing types of array elements) where the
underlying language has poor support for them.
* Leverage power of homogeneous arrays in a language.
* Support records regardless of underlying language (array of records is
homogeneous, even though a record is a heterogeneous list of fields)
* Allow ragged arrays (a table where each row is a list, but the rows do not
have a uniform size (or shape))
* Provide basic in memory compression. Allow deferred decoding of partial
data.
## 1. base64 encoding (used in later challenges)
To read and write binary data on reddit, we will use
[base64 encoding](279easy.md).
## 2. Extendible byte base.
Any size integer can be coded into a variable byte array by using the maximum
byte value as a marker to add the next byte value to decode the total.
This is useful for coding numbers that you think can be limited to around 255
or close to it, without being "hard constrained" by that limit. "256 possible
op codes (or characters) ought to be enough for everyone forever thinking"
**Unsigned byte input**
12
255
256
510
512 44 1024
Last input is a list of 3 integers to encode
**Sample outputs**
12
255 0
255 1
255 255 0
255 255 2 44 255 255 255 255 4
Every element that is not 255 marks the end of "that integer" in a list. You
should also write a decoder that transforms output into input.
## 3. Multibyte and variable byte encodings
Instead of a single byte target encoding, 2,4,8 and variable defined byte sizes
are also desirable to cover integers with larger ranges. An account balance
might have a 40 bit practical limit, but you might not guarantee it forever.
64 bits might not be enough for Zimbabwe currency balances for example.
For compressing a list of numbers, often it is useful to set the whole list to
one "byte size". Other choices include,
* Setting an enum/table of possible byte size codings of 1 2 4 8 sizes, and
then encoding, the number of elements, the table/enum size and definition,
and then 2 lists (enum key, data items)
* Interleave bytesize, data
The latter will often be longer for long lists, but does not encode the table
so is simpler to encode/decode.
### Encoding format for table definition:
1. 4 bytes: first 30 bits - length of list. Last 2 bits: key into 1 2 4 8. If
first 30 bits are max value, then following 4 bytes are added to count until
a non-max value is taken. Similar to challenge #2.
2. List of byte lengths defined by key in 1. If last 2 bits of 1 are 3
(signifies up to 8 distinct integer sizes), then this list has 8 items. If
there only 6 distinct integer size codings, then the last 2 items in this
list would be ignored and set to 0. Values over 255 are encoded as in
challenge 2.
3. List of ordered data encodings in boolean form, if there are more than 1. 1
bit for 2, 2 bits for 4, 3 bits for 8.
4. List of data elements.
### Challenges
Encode list of integers from 0 to 1025 using 8 or 16 bit variable encoding.
With the shortest encoding that will contain the number. Just print the sum of
all the bytes as result for output brevity.
### Solution
1. First 4 bytes are (1025 * 4) + 1 (leading 0 bytes for smaller than "full
size" numbers)
2. 2 byte list: 1 2
3. 0 for first 256 bits, 1 for remaining bits (total 1032 bits long with
padding)
4. 256 + (769 * 2) bytes long encoding of the numbers.
# 4. Balanced signed numbers
Some numbers are negative. The common computer encoding for signed number
ranges is to subtract half the max power of 2 from the value. A signed byte has
range -128 to 127, where a 0 value corresponds to -128 (in our encoding).
For numbers outside this range encoded in a single byte, the process is to take
the first byte to determine the sign, and then following bytes add or subtract
up to 255 per byte until a non 255 value is reached.
# 5. Unbalanced signed numbers
Instead of the midpoint marking 0, a byte can encode a value within any defined
range. Another important application is to use "negative" numbers as codes of
some sort. These include:
* An expectation that negative numbers are less frequent and smaller relative
to 0
* Coding special values such as null, infinity, undeterminable (0/0)
* Using codes to hint at extended byte encodings and sign of the number, or
even data type
**sample 0 index codes** (for 16 reserved codes) (new paragraph for multiline
explained codes)
Null
Infinity
Negative Infinity
Negative 1 byte
Negative 2 bytes
Negative 4 bytes
Negative 8 bytes
Negative custom byte length (value is encoded into 2 numbers. First is byte length (in 255 terminated bytes, followed by that number of bytes to represent the number)
Positive 1 byte (first number indicates range of 468 to 723). 467 could have been encoded as 255 254 without this special code.
Positive 2 byte
Positive 4 byte
Positive 8 byte
Positive 16 byte
Positive 64 byte
Positive custom byte length (3 to 262 excluding other defined lengths)
Positive custom 2 byte length (16 bit unsigned number defines byte length of number, followed by encoded number)
**Sample inputs**
10
123123
-55
Null
**Sample output**
26
9 123123
3 54 (minimum range value is -1)
0
**Challenge input**
192387198237192837192837192387123817239182737 _44 981237123
Array of 3 numbers (_44 is -44) to be encoded

View File

@ -1,8 +0,0 @@
M22!F965L('9E<GD@<W1R;VYG;'D@86)O=70@>6]U(&1O:6YG(&1U='DN(%=O
M=6QD('EO=2!G:79E(&UE(&$@;&ET=&QE(&UO<F4@9&]C=6UE;G1A=&EO;B!A
M8F]U="!Y;W5R(')E861I;F<@:6X@1G)E;F-H/R!)(&%M(&=L860@>6]U(&%R
M92!H87!P>2#B@)0@8G5T($D@;F5V97(@8F5L:65V92!M=6-H(&EN(&AA<'!I
M;F5S<RX@22!N979E<B!B96QI979E(&EN(&UI<V5R>2!E:71H97(N(%1H;W-E
M(&%R92!T:&EN9W,@>6]U('-E92!O;B!T:&4@<W1A9V4@;W(@=&AE('-C<F5E
M;B!O<B!T:&4@<')I;G1E9"!P86=E<RP@=&AE>2!N979E<B!R96%L;'D@:&%P
4<&5N('1O('EO=2!I;B!L:69E+@

View File

@ -1,94 +0,0 @@
<p>We will make a binary byte oriented encoding of data that is self describing and extensible, and aims to solve the following problems:</p>
<ul>
<li>portability between 32 and 64 (and any other) bit systems, and languages, and endian-ness.</li>
<li>type system independent of underlying language.<br />
</li>
<li>Allow heterogeneous arrays (differing types of array elements) where the underlying language has poor support for them.</li>
<li>leverage power of homogeneous arrays in a language.</li>
<li>support records regardless of underlying language (array of records is homogeneous, even though a record is a heterogeneous list of fields)</li>
<li>Allow ragged arrays (a table where each row is a list, but the rows do not have a uniform size (or shape))</li>
<li>Provide basic in memory compression. Allow deferred decoding of partial data.</li>
</ul>
<h1 id="base64-encoding-used-in-later-challenges">1. base64 encoding (used in later challenges)</h1>
<p>To read and write binary data on reddit, we will use <a href="problem1.html">base64 encoding</a>.</p>
<h1 id="extendible-byte-base.">2. Extendible byte base.</h1>
<p>Any size integer can be coded into a variable byte array by using the maximum byte value as a marker to add the next byte value to decode the total.</p>
<p>This is useful for coding numbers that you think can be limited to around 255 or close to it, without being &quot;hard constrained&quot; by that limit. &quot;256 possible op codes (or characters) ought to be enough for everyone forever thinking&quot;</p>
<p><strong>unsigned byte input</strong><br />
12<br />
255<br />
256<br />
510<br />
512 44 1024</p>
<p>last input is a list of 3 integers to encode</p>
<p><strong>sample outputs</strong><br />
12<br />
255 0<br />
255 1<br />
255 255 0<br />
255 255 2 44 255 255 255 255 4</p>
<p>every element that is not 255 marks the end of &quot;that integer&quot; in a list. You should also write a decoder that transforms output into input.</p>
<h1 id="multibyte-and-variable-byte-encodings">3. multibyte and variable byte encodings</h1>
<p>Instead of a single byte target encoding, 2,4,8 and variable defined byte sizes are also desirable to cover integers with larger ranges. An account balance might have a 40 bit practical limit, but you might not guarantee it forever. 64 bits might not be enough for Zimbabwe currency balances for example.</p>
<p>For compressing a list of numbers, often it is useful to set the whole list to one &quot;byte size&quot;. Other choices include,</p>
<ul>
<li>setting an enum/table of possible byte size codings of 1 2 4 8 sizes, and then encoding, the number of elements, the table/enum size and definition, and then 2 lists (enum key, data items)</li>
<li>interleave bytesize, data</li>
</ul>
<p>The latter will often be longer for long lists, but does not encode the table so is simpler to encode/decode.</p>
<p><strong>Encoding format for table definition:</strong></p>
<ol style="list-style-type: decimal">
<li>4 bytes: first 30 bits - length of list. last 2 bits: key into 1 2 4 8. If first 30 bits are max value, then following 4 bytes are added to count until a non-max value is taken. Similar to challenge #2.<br />
</li>
<li>list of byte lengths defined by key in 1. If last 2 bits of 1 are 3 (signifies up to 8 distinct integer sizes), then this list has 8 items. If there only 6 distinct integer size codings, then the last 2 items in this list would be ignored and set to 0. Values over 255 are encoded as in challenge 2.</li>
<li>list of ordered data encodings in boolean form, if there are more than 1. 1 bit for 2, 2 bits for 4, 3 bits for 8.</li>
<li>list of data elements.</li>
</ol>
<p><strong>challenges</strong><br />
encode list of integers from 0 to 1025 using 8 or 16 bit variable encoding. With the shortest encoding that will contain the number. Just print the sum of all the bytes as result for output brevity.</p>
<p><strong>solution</strong></p>
<ol style="list-style-type: decimal">
<li>first 4 bytes are (1025 * 4) + 1 (leading 0 bytes for smaller than &quot;full size&quot; numbers)</li>
<li>2 byte list: 1 2</li>
<li>0 for first 256 bits, 1 for remaining bits (total 1032 bits long with padding)</li>
<li>256 + (769 * 2) bytes long encoding of the numbers.</li>
</ol>
<h1 id="balanced-signed-numbers">4. balanced signed numbers</h1>
<p>Some numbers are negative. The common computer encoding for signed number ranges is to subtract half the max power of 2 from the value. A signed byte has range -128 to 127, where a 0 value corresponds to -128 (in our encoding).</p>
<p>For numbers outside this range encoded in a single byte, the process is to take the first byte to determine the sign, and then following bytes add or subtract up to 255 per byte until a non 255 value is reached.</p>
<h1 id="unbalanced-signed-numbers">5. unbalanced signed numbers</h1>
<p>Instead of the midpoint marking 0, a byte can encode a value within any defined range. Another important application is to use &quot;negative&quot; numbers as codes of some sort. These include:</p>
<ul>
<li>An expectation that negative numbers are less frequent and smaller relative to 0</li>
<li>coding special values such as null, infinity, undeterminable (0/0)</li>
<li>Using codes to hint at extended byte encodings and sign of the number, or even data type</li>
</ul>
<p><strong>sample 0 index codes</strong> (for 16 reserved codes) (new paragraph for multiline explained codes)<br />
Null<br />
Infinity<br />
Negative Infinity<br />
Negative 1 byte<br />
Negative 2 bytes<br />
Negative 4 bytes<br />
Negative 8 bytes<br />
Negative custom byte length (value is encoded into 2 numbers. First is byte length (in 255 terminated bytes, followed by that number of bytes to represent the number)</p>
<p>Positive 1 byte (first number indicates range of 468 to 723). 467 could have been encoded as 255 254 without this special code.</p>
<p>Positive 2 byte<br />
Positive 4 byte<br />
Positive 8 byte<br />
Positive 16 byte<br />
Positive 64 byte<br />
Positive custom byte length (3 to 262 excluding other defined lengths) Positive custom 2 byte length (16 bit unsigned number defines byte length of number, followed by encoded number)</p>
<p><strong>sample inputs</strong><br />
10<br />
123123<br />
-55<br />
Null</p>
<p><strong>sample output</strong><br />
26<br />
9 123123<br />
3 54 (minimum range value is -1)<br />
0</p>
<p><strong>challenge input</strong></p>
<p>192387198237192837192837192387123817239182737 _44 981237123</p>
<p>array of 3 numbers (_44 is -44) to be encoded</p>

View File

@ -1,121 +0,0 @@
We will make a binary byte oriented encoding of data that is self describing and extensible, and aims to solve the following problems:
* portability between 32 and 64 (and any other) bit systems, and languages, and endian-ness.
* type system independent of underlying language.
* Allow heterogeneous arrays (differing types of array elements) where the underlying language has poor support for them.
* leverage power of homogeneous arrays in a language.
* support records regardless of underlying language (array of records is homogeneous, even though a record is a heterogeneous list of fields)
* Allow ragged arrays (a table where each row is a list, but the rows do not have a uniform size (or shape))
* Provide basic in memory compression. Allow deferred decoding of partial data.
# 1. base64 encoding (used in later challenges)
To read and write binary data on reddit, we will use [base64 encoding](problem1.html).
# 2. Extendible byte base.
Any size integer can be coded into a variable byte array by using the maximum byte value as a marker to add the next byte value to decode the total.
This is useful for coding numbers that you think can be limited to around 255 or close to it, without being "hard constrained" by that limit. "256 possible op codes (or characters) ought to be enough for everyone forever thinking"
**unsigned byte input**
12
255
256
510
512 44 1024
last input is a list of 3 integers to encode
**sample outputs**
12
255 0
255 1
255 255 0
255 255 2 44 255 255 255 255 4
every element that is not 255 marks the end of "that integer" in a list. You should also write a decoder that transforms output into input.
# 3. multibyte and variable byte encodings
Instead of a single byte target encoding, 2,4,8 and variable defined byte sizes are also desirable to cover integers with larger ranges. An account balance might have a 40 bit practical limit, but you might not guarantee it forever. 64 bits might not be enough for Zimbabwe currency balances for example.
For compressing a list of numbers, often it is useful to set the whole list to one "byte size". Other choices include,
* setting an enum/table of possible byte size codings of 1 2 4 8 sizes, and then encoding, the number of elements, the table/enum size and definition, and then 2 lists (enum key, data items)
* interleave bytesize, data
The latter will often be longer for long lists, but does not encode the table so is simpler to encode/decode.
**Encoding format for table definition:**
1. 4 bytes: first 30 bits - length of list. last 2 bits: key into 1 2 4 8. If first 30 bits are max value, then following 4 bytes are added to count until a non-max value is taken. Similar to challenge #2.
2. list of byte lengths defined by key in 1. If last 2 bits of 1 are 3 (signifies up to 8 distinct integer sizes), then this list has 8 items. If there only 6 distinct integer size codings, then the last 2 items in this list would be ignored and set to 0. Values over 255 are encoded as in challenge 2.
3. list of ordered data encodings in boolean form, if there are more than 1. 1 bit for 2, 2 bits for 4, 3 bits for 8.
4. list of data elements.
**challenges**
encode list of integers from 0 to 1025 using 8 or 16 bit variable encoding. With the shortest encoding that will contain the number. Just print the sum of all the bytes as result for output brevity.
**solution**
1. first 4 bytes are (1025 * 4) + 1 (leading 0 bytes for smaller than "full size" numbers)
2. 2 byte list: 1 2
3. 0 for first 256 bits, 1 for remaining bits (total 1032 bits long with padding)
4. 256 + (769 * 2) bytes long encoding of the numbers.
# 4. balanced signed numbers
Some numbers are negative. The common computer encoding for signed number ranges is to subtract half the max power of 2 from the value. A signed byte has range -128 to 127, where a 0 value corresponds to -128 (in our encoding).
For numbers outside this range encoded in a single byte, the process is to take the first byte to determine the sign, and then following bytes add or subtract up to 255 per byte until a non 255 value is reached.
# 5. unbalanced signed numbers
Instead of the midpoint marking 0, a byte can encode a value within any defined range.
Another important application is to use "negative" numbers as codes of some sort. These include:
* An expectation that negative numbers are less frequent and smaller relative to 0
* coding special values such as null, infinity, undeterminable (0/0)
* Using codes to hint at extended byte encodings and sign of the number, or even data type
**sample 0 index codes** (for 16 reserved codes) (new paragraph for multiline explained codes)
Null
Infinity
Negative Infinity
Negative 1 byte
Negative 2 bytes
Negative 4 bytes
Negative 8 bytes
Negative custom byte length (value is encoded into 2 numbers. First is byte length (in 255 terminated bytes, followed by that number of bytes to represent the number)
Positive 1 byte (first number indicates range of 468 to 723). 467 could have been encoded as 255 254 without this special code.
Positive 2 byte
Positive 4 byte
Positive 8 byte
Positive 16 byte
Positive 64 byte
Positive custom byte length (3 to 262 excluding other defined lengths)
Positive custom 2 byte length (16 bit unsigned number defines byte length of number, followed by encoded number)
**sample inputs**
10
123123
-55
Null
**sample output**
26
9 123123
3 54 (minimum range value is -1)
0
**challenge input**
192387198237192837192837192387123817239182737 _44 981237123
array of 3 numbers (_44 is -44) to be encoded

View File

@ -1,61 +0,0 @@
<p>You are trapped at uninhabited island only with your laptop. Still you don't want your significant other to worry about you, so you are going to send a message in a bottle with your picture or at least a couple of words from you (sure, you could just write down the words, but that would be less fun). You're going to use uuencoding for that.</p>
<p>Uuencoding is a form of binary-to-text encoding, which uses only symbols from 32-95 diapason, which means all symbols used in the encoding are printable.</p>
<h1 id="description-of-encoding">Description of encoding</h1>
<p>A uuencoded file starts with a header line of the form:</p>
<pre><code>begin &lt;mode&gt; &lt;file&gt;&lt;newline&gt;</code></pre>
<p><mode> is the file's Unix file permissions as three octal digits (e.g. 644, 744). For Windows 644 is always used.</p>
<p><file> is the file name to be used when recreating the binary data.</p>
<p><newline> signifies a newline character, used to terminate each line.</p>
<p>Each data line uses the format:</p>
<pre><code>&lt;length character&gt;&lt;formatted characters&gt;&lt;newline&gt;</code></pre>
<p><length character> is a character indicating the number of data bytes which have been encoded on that line. This is an ASCII character determined by adding 32 to the actual byte count, with the sole exception of a grave accent &quot;`&quot; (ASCII code 96) signifying zero bytes. All data lines except the last (if the data was not divisible by 45), have 45 bytes of encoded data (60 characters after encoding). Therefore, the vast majority of length values is 'M', (32 + 45 = ASCII code 77 or &quot;M&quot;).</p>
<p><formatted characters> are encoded characters.</p>
<p>The mechanism of uuencoding repeats the following for every 3 bytes (if there are less than 3 bytes left, trailing 0 are added):</p>
<ol style="list-style-type: decimal">
<li><p>Start with 3 bytes from the source, 24 bits in total.</p></li>
<li><p>Split into 4 6-bit groupings, each representing a value in the range 0 to 63: bits (00-05), (06-11), (12-17) and (18-23).</p></li>
<li><p>Add 32 to each of the values. With the addition of 32 this means that the possible results can be between 32 (&quot; &quot; space) and 95 (&quot;_&quot; underline). 96 (&quot;`&quot; grave accent) as the &quot;special character&quot; is a logical extension of this range.</p></li>
<li><p>Output the ASCII equivalent of these numbers.</p></li>
</ol>
<p>For example, we want to encode a word &quot;Cat&quot;. ASCII values for C,a,t are 67,97,116, or <code>010000110110000101110100</code> in binary. After dividing into four groups, we get 010000 110110 000101 110100, which is 16,54,5,52 in decimal. Adding 32 to this values and encoding back in ASCII, the final result is <code>0V%T</code>.</p>
<p>The file ends with two lines:</p>
<pre><code>`&lt;newline&gt;
end&lt;newline&gt;</code></pre>
<h1 id="formal-inputs-outputs">Formal Inputs &amp; Outputs</h1>
<h2 id="input">Input</h2>
<p>a byte array or string.</p>
<h2 id="output">Output</h2>
<p>a string containing uuencoded input.</p>
<h1 id="examples">Examples</h1>
<p>Input: Cat</p>
<p>Output:</p>
<pre><code>begin 644 cat.txt
#0V%T
`
end</code></pre>
<p>Input: I feel very strongly about you doing duty. Would you give me a little more documentation about your reading in French? I am glad you are happy — but I never believe much in happiness. I never believe in misery either. Those are things you see on the stage or the screen or the printed pages, they never really happen to you in life.</p>
<p>Output:</p>
<pre><code>begin 644 file.txt
M22!F965L(&#39;9E&lt;GD@&lt;W1R;VYG;&#39;D@86)O=70@&gt;6]U(&amp;1O:6YG(&amp;1U=&#39;DN(%=O
M=6QD(&#39;EO=2!G:79E(&amp;UE(&amp;$@;&amp;ET=&amp;QE(&amp;UO&lt;F4@9&amp;]C=6UE;G1A=&amp;EO;B!A
M8F]U=&quot;!Y;W5R(&#39;)E861I;F&lt;@:6X@1G)E;F-H/R!)(&amp;%M(&amp;=L860@&gt;6]U(&amp;%R
M92!H87!P&gt;2#B@)0@8G5T($D@;F5V97(@8F5L:65V92!M=6-H(&amp;EN(&amp;AA&lt;&#39;!I
M;F5S&lt;RX@22!N979E&lt;B!B96QI979E(&amp;EN(&amp;UI&lt;V5R&gt;2!E:71H97(N(%1H;W-E
M(&amp;%R92!T:&amp;EN9W,@&gt;6]U(&#39;-E92!O;B!T:&amp;4@&lt;W1A9V4@;W(@=&amp;AE(&#39;-C&lt;F5E
M;B!O&lt;B!T:&amp;4@&lt;&#39;)I;G1E9&quot;!P86=E&lt;RP@=&amp;AE&gt;2!N979E&lt;B!R96%L;&#39;D@:&amp;%P
3&lt;&amp;5N(&#39;1O(&#39;EO=2!I;B!L:69E+C P
`
end</code></pre>
<h1 id="bonuses">Bonuses</h1>
<h2 id="bonus-1">Bonus 1</h2>
<p>Write uudecoder, which decodes uuencoded input back to a byte array or string</p>
<h2 id="bonus-2">Bonus 2</h2>
<p>Write encoder for files as well.</p>
<h2 id="bonus-3">Bonus 3</h2>
<p>Make encoding parallel.</p>
<h1 id="further-reading">Further Reading</h1>
<p><a href="https://en.wikipedia.org/wiki/Binary-to-text_encoding">Binary-to-text encoding</a> on Wikipedia.</p>
<h1 id="finally">Finally</h1>
<p>This challenge is posted by /u/EvgeniyZh</p>
<p>Also have a good challenge idea?</p>
<p>Consider submitting it to /r/dailyprogrammer_ideas</p>