Decompress a Zip-compressed Binary string - zip

I have a binary string in Elixir that's composed of compressed bytes that I want to deflate and extract the "real data" from:
iex(93)> data
<<31, 139, 8, 0, 0, 0, 0, 0, 0, 0, 109, 80, 203, 110, 218, 64, 0, 76, 171, 70, 141, 68, 78, 105, 213, 67, 171, 74, 168, 7, 212, 86, 50, 172, 189, 94, 236, 69, 66, 173, 49, 40, 56, 96, 76, 130, 31, 196, 23, 226, 216, 11, 44, 216, 94, 227, 117, 13, 238, 199, 244, 208, 207, 234, 23, 244, 23, 122, 43, 137, 218, 91, 110, 243, 208, 204, 72, 83, 187, 56, 61, 59, 169, 255, 126, 121, 245, 227, 69, 237, 226, 125, 41, 117, 66, 150, 52, 105, 146, 241, 42, 92, 179, 230, 61, 99, 69, 148, 51, 26, 117, 196, 14, 122, 251, 170, 119, 164, 245, 254, 3, 175, 127, 188, 33, 15, 230, 167, 15, 53, 109, 58, 29, 27, 186, 102, 27, 214, 228, 243, 155, 167, 211, 210, 159, 231, 235, 162, 200, 120, 167, 213, 10, 210, 71, 165, 25, 198, 148, 164, 5, 111, 174, 24, 91, 197, 164, 121, 204, 181, 146, 32, 223, 146, 162, 21, 177, 125, 26, 179, 32, 106, 245, 255, 129, 47, 89, 16, 110, 131, 21, 153, 4, 9, 233, 62, 61, 208, 40, 73, 206, 41, 75, 117, 22, 145, 46, 106, 112, 30, 119, 197, 70, 193, 182, 36, 237, 106, 150, 173, 39, 192, 22, 151, 188, 93, 85, 75, 52, 2, 78, 110, 136, 90, 101, 17, 228, 43, 179, 126, 24, 109, 122, 217, 72, 231, 238, 140, 248, 83, 205, 229, 73, 79, 77, 101, 129, 162, 148, 135, 246, 88, 95, 32, 107, 39, 229, 153, 155, 219, 250, 118, 236, 196, 14, 104, 187, 150, 228, 91, 154, 194, 132, 188, 7, 29, 65, 25, 122, 201, 236, 90, 91, 78, 50, 49, 191, 180, 190, 29, 59, 146, 91, 213, 50, 23, 43, 92, 84, 36, 240, 124, 103, 98, 198, 90, 60, 47, 231, 133, 105, 175, 16, 173, 42, 67, 217, 5, 222, 149, 61, 208, 92, 115, 70, 215, 166, 158, 89, 17, 112, 239, 105, 168, 30, 134, 91, 60, 242, 6, 163, 18, 122, 202, 94, 42, 47, 157, 104, 176, 151, 199, 223, 5, 225, 154, 223, 82, 52, 104, 251, 150, 195, 131, 74, 223, 249, 169, 13, 54, 96, 108, 26, 195, 249, 46, 94, 192, 233, 58, 106, 252, 255, 203, 136, 186, 2, 148, 85, 36, 73, 176, 141, 17, 84, 177, 36, 42, 50, 132, 157, 119, 101, 146, 2, 44, 28, 82, 153, 142, 124, 111, 7, 5, 97, 64, 151, 192, 62, 32, 112, 195, 191, 254, 252, 101, 78, 79, 230, 207, 238, 78, 55, 103, 230, 227, 253, 125, 45, 127, 13, 48, 22, 177, 164, 96, 5, 41, 80, 145, 219, 0, 171, 16, 159, 159, 255, 5, 242, 139, 137, 38, 42, 2, 0, 0>>
I'm not sure how to unzip this data. So far, I've:
Looked through Official Elixir Docs
Tried using Erlang's :zip and :zlib modules but had no success. Both of them throw errors:
iex(100)> :zlib.uncompress(data)
** (ErlangError) erlang error: :data_error
:zlib.call/3
:zlib.inflate/2
:zlib.uncompress/1
iex(101)> :zip.unzip data
{:error,
{:EXIT,
{{:badmatch,
<<31, 139, 8, 0, 0, 0, 0, 0, 0, 0, 109, 80, 203, 110, 218, 64, 0, 76, 171, 70, 141, 68, 78, 105, 213, 67, 171, 74, 168, 7, 212, 86, 50, 172, 189, 94, 236, 69, 66, 173, 49, 40, 56, 96, 76, 130, ...>>},
[{:zip, :binary_io, 2, [file: 'zip.erl', line: 1726]},
{:zip, :get_end_of_central_dir, 3, [file: 'zip.erl', line: 1313]},
{:zip, :get_central_dir, 3, [file: 'zip.erl', line: 1269]},
{:zip, :do_unzip, 2, [file: 'zip.erl', line: 380]},
{:zip, :unzip, 2, [file: 'zip.erl', line: 370]},
{:erl_eval, :do_apply, 6, [file: 'erl_eval.erl', line: 670]},
{:elixir, :erl_eval, 3, [file: 'src/elixir.erl', line: 215]},
{:elixir, :eval_forms, 4, [file: 'src/elixir.erl', line: 203]}]}}}
I know for a fact that the data is correct, I was able to extract information from the same bytes in Javascript using JXG.Util.Unzip(). But, how do I unzip this data in Elixir?

Your data was gzip compressed data according to the file command, so I tried :zlib.gunzip and it worked:
iex(1)> data = <<31, 139, 8, 0, 0, 0, 0, 0, 0, 0, 109, 80, 203, 110, 218, 64, 0, 76, 171, 70, 141, 68, 78, 105, 213, 67, 171, 74, 168, 7, 212, 86, 50, 172, 189, 94, 236, 69, 66, 173, 49, 40, 56, 96, 76, 130, 31, 196, 23, 226, 216, 11, 44, 216, 94, 227, 117, 13, 238, 199, 244, 208, 207, 234, 23, 244, 23, 122, 43, 137, 218, 91, 110, 243, 208, 204, 72, 83, 187, 56, 61, 59, 169, 255, 126, 121, 245, 227, 69, 237, 226, 125, 41, 117, 66, 150, 52, 105, 146, 241, 42, 92, 179, 230, 61, 99, 69, 148, 51, 26, 117, 196, 14, 122, 251, 170, 119, 164, 245, 254, 3, 175, 127, 188, 33, 15, 230, 167, 15, 53, 109, 58, 29, 27, 186, 102, 27, 214, 228, 243, 155, 167, 211, 210, 159, 231, 235, 162, 200, 120, 167, 213, 10, 210, 71, 165, 25, 198, 148, 164, 5, 111, 174, 24, 91, 197, 164, 121, 204, 181, 146, 32, 223, 146, 162, 21, 177, 125, 26, 179, 32, 106, 245, 255, 129, 47, 89, 16, 110, 131, 21, 153, 4, 9, 233, 62, 61, 208, 40, 73, 206, 41, 75, 117, 22, 145, 46, 106, 112, 30, 119, 197, 70, 193, 182, 36, 237, 106, 150, 173, 39, 192, 22, 151, 188, 93, 85, 75, 52, 2, 78, 110, 136, 90, 101, 17, 228, 43, 179, 126, 24, 109, 122, 217, 72, 231, 238, 140, 248, 83, 205, 229, 73, 79, 77, 101, 129, 162, 148, 135, 246, 88, 95, 32, 107, 39, 229, 153, 155, 219, 250, 118, 236, 196, 14, 104, 187, 150, 228, 91, 154, 194, 132, 188, 7, 29, 65, 25, 122, 201, 236, 90, 91, 78, 50, 49, 191, 180, 190, 29, 59, 146, 91, 213, 50, 23, 43, 92, 84, 36, 240, 124, 103, 98, 198, 90, 60, 47, 231, 133, 105, 175, 16, 173, 42, 67, 217, 5, 222, 149, 61, 208, 92, 115, 70, 215, 166, 158, 89, 17, 112, 239, 105, 168, 30, 134, 91, 60, 242, 6, 163, 18, 122, 202, 94, 42, 47, 157, 104, 176, 151, 199, 223, 5, 225, 154, 223, 82, 52, 104, 251, 150, 195, 131, 74, 223, 249, 169, 13, 54, 96, 108, 26, 195, 249, 46, 94, 192, 233, 58, 106, 252, 255, 203, 136, 186, 2, 148, 85, 36, 73, 176, 141, 17, 84, 177, 36, 42, 50, 132, 157, 119, 101, 146, 2, 44, 28, 82, 153, 142, 124, 111, 7, 5, 97, 64, 151, 192, 62, 32, 112, 195, 191, 254, 252, 101, 78, 79, 230, 207, 238, 78, 55, 103, 230, 227, 253, 125, 45, 127, 13, 48, 22, 177, 164, 96, 5, 41, 80, 145, 219, 0, 171, 16, 159, 159, 255, 5, 242, 139, 137, 38, 42, 2, 0, 0>>
<<31, 139, 8, 0, 0, 0, 0, 0, 0, 0, 109, 80, 203, 110, 218, 64, 0, 76, 171, 70, 141, 68, 78, 105, 213, 67, 171, 74, 168, 7, 212, 86, 50, 172, 189, 94, 236, 69, 66, 173, 49, 40, 56, 96, 76, 130, 31, 196, 23, 226, ...>>
iex(2)> :zlib.gunzip(data)
<<11, 18, 5, 8, 0, 32, 232, 7, 74, 158, 4, 11, 18, 29, 118, 50, 58, 99, 111, 109, 46, 105, 109, 112, 115, 121, 99, 104, 111, 46, 98, 111, 111, 116, 100, 114, 111, 105, 100, 58, 49, 58, 53, 26, 19, 66, 111, 111, 116, 32, ...>>
From the docs.
gunzip(Data) -> Decompressed
Types:
Data = iodata()
Decompressed = binary()
Uncompress data (with gz headers and checksum).

Related

FabricJS: Difficulty creating custom filter with WebGl

I'm trying to create a custom filter for the FabricJS library. I got it in parts, because when the filter is used without WebGl it is possible to get the desired result, but when I try to run it with the active resource, I can't get the result. I have no knowledge about this language used to make the filter work with WebGl active.
I've made numerous attempts to write the code for the fragmentSource, but to no avail.
Can anyone help me out with this?
Below is the custom filter code:
fabric.Image.filters.MyFilter = fabric.util.createClass(fabric.Image.filters.BaseFilter, {
type: 'MyFilter',
/**
* Fragment source for the redify program
*/
fragmentSource: 'precision highp float;\n' +
'uniform sampler2D uTexture;\n' +
'uniform float uAlpha;\n' +
'uniform vec4 uFilter;\n' +
'varying vec2 vTexCoord;\n' +
'void main() {\n' +
'vec4 color = texture2D(uTexture, vTexCoord);\n' +
'color.rgb += uAlpha;\n' +
'gl_FragColor = color;\n' +
'}',
alpha: 1,
filter: null,
mainParameter: 'filter',
getUniformLocations: function (gl, program) {
return {
uAlpha: gl.getUniformLocation(program, 'uAlpha'),
uFilter: gl.getUniformLocation(program, 'uFilter'),
};
},
sendUniformData: function (gl, uniformLocations) {
gl.uniform1f(uniformLocations.uAlpha, this.alpha);
gl.uniform1f(uniformLocations.uFilter, this.filter);
},
applyTo2d: function (options) {
const filter = this.filter
const alpha = this.alpha
let imageData = options.imageData,
data = imageData.data,
i, len = data.length;
for (let i = 0; i < len; i += 4) {
data[i] = filter.r[data[i]] * alpha + data[i] * (1 - alpha);
data[i + 1] = filter.g[data[i + 1]] * alpha + data[i + 1] * (1 - alpha);
data[i + 2] = filter.b[data[i + 2]] * alpha + data[i + 2] * (1 - alpha);
}
},
toObject: function () {
return {
filter: this.filter,
alpha: this.alpha
};
}
});
fabric.Image.filters.MyFilter.fromObject = fabric.Image.filters.BaseFilter.fromObject;
The data to load in the filter would be in this format:
{
"r": [28, 29, 29, 30, 30, 31, 31, 32, 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 36, 37, 37, 38, 38, 39, 40, 40, 41, 42, 42, 44, 44, 44, 45, 46, 46, 47, 49, 50, 51, 51, 51, 52, 53, 55, 56, 57, 58, 59, 59, 60, 62, 64, 65, 66, 67, 68, 69, 72, 73, 74, 74, 76, 77, 78, 80, 82, 84, 85, 86, 88, 89, 91, 94, 94, 95, 97, 98, 100, 101, 103, 104, 106, 109, 111, 112, 114, 115, 117, 119, 119, 120, 122, 124, 127, 129, 130, 132, 134, 136, 137, 139, 141, 142, 144, 146, 148, 149, 153, 155, 156, 158, 160, 160, 161, 163, 165, 167, 168, 170, 172, 173, 175, 177, 178, 180, 181, 183, 185, 186, 188, 189, 191, 193, 194, 196, 197, 198, 200, 201, 203, 204, 205, 207, 208, 209, 211, 212, 212, 213, 214, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 226, 227, 227, 228, 229, 230, 230, 231, 232, 232, 233, 233, 234, 234, 235, 235, 236, 236, 237, 237, 237, 238, 238, 238, 239, 239, 239, 239, 240, 240, 240, 240, 240, 241, 241, 241, 241, 241, 241, 241, 241, 241, 241, 242, 242, 242, 242, 242, 242, 242, 242, 241, 241, 241, 241, 241, 241, 241, 241, 241, 241, 241, 241, 240, 240, 240, 240, 240, 240, 239, 239, 239, 239, 239, 239, 238, 238, 238, 238, 237, 237, 237, 237, 237, 236, 236, 236, 236, 235, 235, 235, 235, 235, 234, 234],
"g": [27, 28, 29, 29, 30, 30, 31, 32, 32, 32, 32, 32, 33, 34, 34, 34, 35, 35, 36, 36, 36, 37, 37, 38, 39, 39, 40, 40, 41, 41, 42, 42, 43, 44, 45, 46, 46, 47, 48, 48, 49, 50, 51, 51, 52, 53, 54, 55, 55, 57, 58, 59, 60, 61, 62, 64, 65, 66, 67, 67, 68, 69, 71, 72, 73, 74, 76, 77, 78, 80, 81, 82, 84, 84, 85, 86, 88, 89, 91, 92, 94, 95, 97, 98, 100, 100, 101, 103, 104, 104, 106, 107, 109, 111, 112, 114, 115, 117, 119, 120, 120, 122, 124, 125, 127, 129, 130, 132, 134, 134, 136, 136, 137, 139, 141, 142, 144, 146, 148, 148, 149, 151, 153, 155, 156, 158, 160, 160, 161, 163, 165, 167, 168, 170, 172, 172, 173, 175, 177, 178, 180, 181, 183, 183, 185, 186, 188, 189, 191, 193, 194, 194, 196, 197, 198, 200, 201, 203, 204, 204, 205, 207, 207, 208, 209, 211, 212, 213, 214, 216, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 227, 228, 229, 230, 230, 231, 232, 232, 233, 234, 234, 235, 235, 236, 236, 237, 237, 238, 238, 238, 239, 239, 239, 240, 240, 240, 240, 240, 241, 241, 241, 241, 241, 241, 241, 242, 242, 242, 242, 242, 242, 242, 241, 241, 241, 241, 241, 241, 241, 241, 241, 240, 240, 240, 240, 239, 239, 239, 239, 238, 238, 238, 237, 237, 237, 237, 236, 236, 236, 235, 235, 235, 234, 234],
"b": [29, 30, 31, 31, 31, 31, 32, 32, 33, 33, 33, 33, 34, 34, 34, 35, 35, 35, 36, 36, 36, 36, 36, 37, 37, 38, 38, 39, 39, 40, 40, 40, 40, 41, 42, 42, 42, 43, 44, 44, 44, 44, 45, 46, 46, 46, 47, 48, 48, 49, 49, 50, 51, 51, 51, 52, 53, 54, 54, 55, 55, 56, 57, 57, 58, 59, 59, 60, 61, 62, 62, 64, 65, 65, 65, 66, 67, 68, 68, 69, 71, 71, 72, 73, 73, 74, 76, 76, 77, 77, 78, 78, 80, 81, 81, 82, 84, 84, 85, 86, 86, 88, 88, 89, 91, 91, 92, 94, 94, 95, 95, 97, 97, 98, 98, 100, 101, 101, 103, 104, 104, 106, 106, 107, 109, 109, 111, 112, 112, 114, 115, 115, 117, 119, 119, 120, 122, 122, 124, 125, 125, 127, 129, 130, 130, 132, 134, 136, 136, 137, 139, 141, 141, 142, 144, 146, 146, 148, 149, 151, 153, 155, 155, 156, 158, 160, 161, 163, 165, 167, 168, 170, 172, 172, 173, 175, 177, 178, 180, 181, 183, 186, 188, 189, 191, 193, 194, 196, 197, 198, 201, 203, 204, 205, 207, 209, 211, 212, 213, 216, 217, 218, 220, 221, 222, 224, 225, 227, 227, 228, 230, 230, 232, 232, 234, 234, 235, 236, 237, 238, 238, 239, 239, 240, 240, 240, 241, 241, 241, 241, 241, 242, 242, 242, 242, 241, 241, 241, 241, 241, 240, 240, 240, 239, 239, 239, 238, 238, 237, 237, 237, 236, 235, 235, 235, 234],
"a": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]
}
Sorry for any mistakes. I'm using the translator.

Saving an array of uint8array in an file using node js

I'm trying to save an array of uint8arrays in an file using node JS, does anyone know how can I do that using fs?
Something like that:
[
Uint8Array(162) [
133, 111, 74, 131, 187, 35, 69, 135, 1, 151, 1, 1,
198, 69, 229, 121, 168, 106, 59, 104, 91, 198, 115, 218,
79, 67, 102, 212, 204, 206, 145, 126, 152, 59, 147, 114,
133, 173, 119, 91, 220, 251, 174, 57, 16, 125, 67, 180,
114, 128, 5, 72, 143, 131, 122, 35, 124, 139, 62, 93,
187, 2, 2, 251, 255, 149, 134, 6, 9, 65, 100, 100,
32, 99, 97, 114, 100, 50, 0, 9, 1, 2, 2, 4,
19, 4, 21, 14, 52, 3, 66, 4, 86, 5, 87, 26,
112, 2, 3, 0,
... 62 more items
],
Uint8Array(170) [
133, 111, 74, 131, 67, 31, 18, 227, 1, 159, 1, 1,
187, 35, 69, 135, 0, 7, 102, 52, 237, 242, 169, 192,
199, 237, 238, 142, 226, 200, 98, 129, 144, 184, 181, 198,
33, 248, 228, 223, 158, 171, 250, 192, 16, 125, 67, 180,
114, 128, 5, 72, 143, 131, 122, 35, 124, 139, 62, 93,
187, 3, 5, 251, 255, 149, 134, 6, 8, 65, 100, 100,
32, 99, 97, 114, 100, 0, 10, 1, 2, 2, 4, 17,
4, 19, 4, 21, 14, 52, 3, 66, 4, 86, 5, 87,
29, 112, 2, 3,
... 70 more items
],
Uint8Array(120) [
133, 111, 74, 131, 71, 17, 199, 138, 1, 110, 1, 67,
31, 18, 227, 131, 113, 252, 222, 71, 172, 34, 205, 40,
96, 11, 236, 242, 153, 43, 182, 4, 136, 135, 36, 6,
79, 52, 223, 123, 188, 42, 7, 16, 125, 67, 180, 114,
128, 5, 72, 143, 131, 122, 35, 124, 139, 62, 93, 187,
4, 8, 251, 255, 149, 134, 6, 11, 68, 101, 108, 101,
116, 101, 32, 99, 97, 114, 100, 0, 10, 1, 2, 2,
2, 17, 2, 19, 2, 52, 1, 66, 2, 86, 2, 112,
2, 113, 2, 115,
... 20 more items
],
Uint8Array(126) [
133, 111, 74, 131, 51, 54, 68, 129, 1, 116, 1, 71,
17, 199, 138, 51, 167, 78, 185, 254, 251, 73, 245, 53,
153, 253, 146, 203, 139, 250, 206, 105, 223, 51, 227, 156,
55, 3, 146, 177, 243, 235, 140, 16, 125, 67, 180, 114,
128, 5, 72, 143, 131, 122, 35, 124, 139, 62, 93, 187,
5, 9, 251, 255, 149, 134, 6, 17, 77, 97, 114, 107,
32, 99, 97, 114, 100, 32, 97, 115, 32, 100, 111, 110,
101, 0, 9, 1, 2, 2, 2, 21, 6, 52, 1, 66,
2, 86, 2, 112,
... 26 more items
]
]
I am simply using the following, for exporting into docx file:
buffer = Uint8Array(86762) [
80, 75, 3, 4, 10, 0, 0, 0, 8, 0, 79, 72,
111, 85, 212, 85, 145, 41, 234, 1, 0, 0, 43, 11,
0, 0, 19, 0, 0, 0, 91, 67, 111, 110, 116, 101,
110, 116, 95, 84, 121, 112, 101, 115, 93, 46, 120, 109,
108, 197, 150, 205, 78, 227, 48, 20, 133, 247, 60, 133,
229, 45, 106, 92, 64, 26, 141, 80, 83, 22, 252, 44,
25, 164, 233, 60, 128, 27, 223, 164, 134, 248, 71, 182,
91, 232, 219, 207, 117, 66, 67, 169, 18, 198, 154, 54,
98, 83, 41, 182,
... 86662 more items
]
fs.writeFileSync('output.docx', buffer)
I suppose restoring it will be just fs.readFileSync(pathToFile)

Find the values in a list using a nested list of indices?

I am trying to find the solution to this problem.
Given a list of numbers:
numbers = [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501, 440, 771, 601, 582, 658, 954, 1154, 1175, 1459, 1786, 1667, 2186, 2558, 2774, 3388, 3448, 4070, 4785, 6060, 4268, 5642, 5236, 4774, 5849, 5966, 6361, 6198, 6411, 5841, 7099, 7933, 9623, 10633, 10581, 10102, 10559, 11231, 10699, 10817, 11012, 11656, 10899, 10028, 9974, 10598, 9200, 9709, 8926, 9263, 8764, 8849, 8894, 9434, 8599, 8946, 8915, 8338, 8371, 8572, 8952, 9268, 8485, 8858, 8529, 8823, 8718, 8846, 8971, 8970, 8587, 8393, 8777, 8961, 8697, 8809, 8217, 8241, 7824, 7772, 7971, 7870, 7717, 7586, 7413, 7165, 7105, 6788, 6843, 6784, 6683, 6683, 6550, 6752, 6710, 6623, 6719, 6569, 6363, 6534, 6491, 6623, 6586, 6587, 6511, 6240, 6410, 6415, 6389, 6214, 6096, 5901, 5828, 5850, 5830, 5779, 5833, 5741, 5607, 5380, 5449, 5484, 5468, 5429, 5387, 5364, 5121, 5186, 5239, 5191, 5185, 5155, 5081, 4892, 5054, 5017, 5016, 5030, 4911, 4839, 4718, 4790, 4767, 4838, 4860, 4797, 4688, 4639, 4642, 4675, 4758, 4843, 4897, 4932, 4670, 4893, 4958, 5064, 5144, 5097, 5106, 5020, 5172, 5310, 5421, 5406, 5361, 5414, 5435, 5612, 5667, 5803, 5960, 6043, 6090, 6109, 6330, 6489, 7112, 7421, 7764, 8026, 8129, 8371, 8835, 9294, 9735, 10376, 10757, 11481, 10981, 11345, 11969, 12673, 13442, 13406, 13690, 14041, 13556, 14937, 14703, 14804, 15843, 16108, 15444, 15704, 17077, 16260, 16392, 17148, 16342, 15886, 17418, 17987, 17834, 18381, 18017, 18431, 19483, 19116, 20368, 20109, 20248, 21577, 20765, 19583, 21333, 21717, 22441, 22313, 22562, 22201, 20717, 23337, 24059, 24538, 24295, 24891, 24087, 23393, 25195, 27267, 26809, 26390, 26046, 26126, 25043, 27829, 27078, 28450, 28701, 27798, 25752, 25838, 27562, 28206, 27729, 27651, 26902, 26265, 26074, 27787, 28116, 27772, 28510, 28917, 28340, 26814, 29499, 28595, 28833, 27849, 27363, 26588, 26095, 27329, 26613, 25938, 23845, 23015, 23955, 23902, 23218, 23330, 23012, 22540]
and a nested list of indices:
nested_list = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45], [46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76], [77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106], [107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137], [138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168], [169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198], [199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229], [230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259], [260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290], [291, 292, 293, 294, 295, 296, 297, 298, 299, 300]]
using the nested list of indices I need to return the values of the items in the list.
example output would be:
[[27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501],[440, 771, 601, 582, 658, 954, 1154, 1175, 1459, 1786, 1667, 2186, 2558, 2774, 3388, 3448, 4070, 4785, 6060, 4268, 5642, 5236, 4774, 5849, 5966, 6361, 6198, 6411, 5841, 7099], etc....
The current piece of code I have is
values_from_list = []
for i in nested_list:
values_from_list.append([numbers[i] for i in nested_list[0]])
print(values_from_list)
which is only returning the set of values i require repeated for i in nested_list
output i am currently getting:
[[27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501]]
I hope my question can be understood,
Many Thanks!
Where you are using nested_list[0] you should use i; and then you should use another variable where you use i in the comprehension. Many errors hide in unclear variable naming; one should strive to make sure variable names always correctly identify their purpose. I would rewrite as follows:
values_from_list = []
for sublist in nested_list:
values_from_list.append([numbers[index] for index in sublist])
print(values_from_list)
or, more succintly,
values_from_list = [
[numbers[index] for index in sublist]
for sublist in nested_list
]

PYTHON3: Printing a set of random integers sometimes get sorted output, sometimes unsorted output! Why?

I'm a Python3 newcomer, and I recently get a strange behavior when printing a set of random integers.
I sometimes get a perfectly sorted set, and sometimes not!
Does somebody know the reason why?
Here is my Python3 code:
import random
n=random.randint(30,90)
print("n=",n)
ens=set()
while len(ens)!=n:
ens.add(random.randint(100,199))
print("len(ens)=",len(ens))
print("ens=",str(ens))
car=input("...?")
Here is one non-sorted resulting text:
n= 84
len(ens)= 84
ens= {128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 147, 148, 150, 151, 152, 154, 156, 157, 158, 160, 161, 162, 163, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 177, 178, 179, 181, 182, 183, 185, 186, 188, 189, 190, 192, 193, 194, 195, 196, 197, 198, 199, 100, 101, 102, 103, 104, 105, 106, 107, 108, 110, 111, 112, 113, 114, 116, 117, 118, 119, 121, 123, 124, 125, 126, 127}
...?
And another sorted resulting text:
n= 86
len(ens)= 86
ens= {100, 102, 103, 104, 105, 106, 107, 108, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 128, 129, 130, 131, 132, 133, 134, 136, 137, 138, 139, 140, 141, 142, 143, 144, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 168, 169, 170, 171, 173, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 186, 188, 189, 191, 192, 195, 196, 197, 198, 199}
...?
I know Python sets are unordered collection of items, but then, why do some of my outputs appear to be perfectly sorted, and some are not?
Just in case it is convincing, I either use Geany IDE, Thonny IDE, or directly Python 3.8 (under Win7 32bit).
Sets are unordered, so when you print your set, python doesn't know what order to print the items, so it prints them in sorted order.
>>> s = {5, 2, 3, 4, 6}
>>> print(s)
{2, 3, 4, 5, 6}
If you want to have the items ordered, use a list.
>>> l = [5, 2, 3, 4, 6]
>>> print(l)
[5, 2, 3, 4, 6]

How do I convert the string "[1, 2, 3]" into a Vec<u8>? [duplicate]

This question already has answers here:
How do I convert a vector of strings to a vector of integers in a functional way?
(3 answers)
Closed 3 years ago.
I'm passing an encrypted message between client/server over TCP using AES-GCM-SIV. The received buffer is converted to String and divided into several Vec<&str> using
let v: Vec<&str> = buffer_string.split("?+").collect();
Example of v:
["POST / HTTP/1.1\\r\\n\\", "Uaxh5NUi098q", "178", "[162, 254, 28, 241, ... ]"]
v[3] should be the ciphertext as Vec<u8>. How can the vector be used as Vec<u8>?
iter().map(|c| *c as u8).collect()
would work with chars, not &str.
Here's a complete example on the Playground:
fn main() {
let buffer_string = r##"POST /chat HTTP/1.1\r\n\?+rRMUG4Lg8Gi6?+178?+[136, 136, 144, 59, 173, 25, 204, 247, 151, 53, 2, 137, 100, 45, 198, 58, 65, 210, 134, 165, 163, 156, 136, 148, 46, 31, 16, 184, 179, 73, 220, 14, 113, 152, 85, 1, 233, 208, 53, 27, 124, 52, 41, 175, 86, 109, 134, 103, 93, 148, 208, 114, 123, 97, 18, 53, 149, 195, 51, 55, 213, 114, 184, 72, 109, 30, 217, 206, 212, 58, 253, 141, 9, 45, 173, 213, 96, 35, 77, 122, 113, 240, 22, 222, 194, 11, 123, 221, 176, 116, 161, 196, 84, 203, 203, 184, 140, 42, 169, 244, 211, 1, 189, 96, 16, 62, 173, 50, 65, 48, 176, 44, 176, 246, 246, 242, 18, 146, 105, 29, 13, 223, 185, 151, 114, 30, 27, 36, 48, 178, 16, 3, 250, 49, 229, 84, 121, 135, 197, 204, 42, 140, 220, 244, 73, 184, 250, 104, 125, 224, 219, 94, 111, 247, 92, 16, 168, 50, 249, 10, 65, 214, 217, 157, 7, 113, 217, 141, 174, 139, 183, 86, 17, 24, 221, 134, 222, 240]"##;
let v: Vec<&str> = buffer_string.split("?+").collect();
println!("Vector: v1 {:?}, v2 {:?}, v3: {:?}", v[1], v[2], v[3]);
//only the v[3] is needed as vec<u8>
//error with iter and &str
//let ciphertext_vec: Vec<_> = v[3].iter().map(|c| c.parse::<u8>().unwrap()).collect();
let ciphertext: Vec<u8> = [
136, 136, 144, 59, 173, 25, 204, 247, 151, 53, 2, 137, 100, 45, 198, 58, 65, 210, 134, 165,
163, 156, 136, 148, 46, 31, 16, 184, 179, 73, 220, 14, 113, 152, 85, 1, 233, 208, 53, 27,
124, 52, 41, 175, 86, 109, 134, 103, 93, 148, 208, 114, 123, 97, 18, 53, 149, 195, 51, 55,
213, 114, 184, 72, 109, 30, 217, 206, 212, 58, 253, 141, 9, 45, 173, 213, 96, 35, 77, 122,
113, 240, 22, 222, 194, 11, 123, 221, 176, 116, 161, 196, 84, 203, 203, 184, 140, 42, 169,
244, 211, 1, 189, 96, 16, 62, 173, 50, 65, 48, 176, 44, 176, 246, 246, 242, 18, 146, 105,
29, 13, 223, 185, 151, 114, 30, 27, 36, 48, 178, 16, 3, 250, 49, 229, 84, 121, 135, 197,
204, 42, 140, 220, 244, 73, 184, 250, 104, 125, 224, 219, 94, 111, 247, 92, 16, 168, 50,
249, 10, 65, 214, 217, 157, 7, 113, 217, 141, 174, 139, 183, 86, 17, 24, 221, 134, 222,
240,
]
.to_vec();
let ciphertext2: Vec<u8> = v[3].iter().map(|c| c.parse::<u8>().unwrap()).collect();
assert_eq!(ciphertext, ciphertext2);
// ciphertext: Vec<u8> =
}
I believe that does it.
fn main() {
let s = "[162, 254, 28, 241]";
let v: Vec<u8> = s
.trim_start_matches('[')
.trim_end_matches(']')
.split(',')
.map(|c| c.trim().parse::<u8>().unwrap())
.collect();
for n in v {
println!("{}", n);
}
}
Try it here.

Resources