Palindrome in Python not working - python-3.x

I am trying to make a simple program that displays the palindrome numbers between 2 numbers in Python3 but it doesn't seem to work. I get only 4 output which are 1,2,4 and 8. What about the other numbers like 11, 22, 33, ..., 111, 121,131,..., 191, 222, etc?
Here is my code. I can't figure out why it's not working.
a = 0
b = 500
a += 1
for i in range(a,b):
if(str(a) == str(a)[::-1]):
print(a)
a += a

Maybe
for i in range (0, 500):
if str(i) == str(i)[::-1]:
print(i)

>>> palindromes = [a for a in range(500) if str(a) == str(a)[::-1]]
>>> palindromes
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131,
141, 151, 161, 171, 181, 191, 202, 212, 222, 232, 242, 252, 262, 272, 282, 292, 303,
313, 323, 333, 343, 353, 363, 373, 383, 393, 404, 414, 424, 434, 444, 454, 464, 474,
484, 494]
That shows you the principle. If you want to do it in a function, you can use yield instead of constructing the whole list in memory (much more efficient):
>>> def palindromes(a, b):
... """Return palindromes in closed interval from a to b"""
... for i in range(a, b):
... if str(i) == str(i)[::-1]:
... yield i
...
>>> list(palindromes(0, 500))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, 212, 222, 232, 242, 252, 262, 272, 282, 292, 303, 313, 323, 333, 343, 353, 363, 373, 383, 393, 404, 414, 424, 434, 444, 454, 464, 474, 484, 494]

Related

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.

Compute optimal route for picking ressources

Here are the rules (Halite 3):
At each turn, you have to take one decision for your ship. You can either move right, move left, move up, move down or stay still (and gather ressoureces).
Moving your ship cost your ship 10% of current's cell available ressources.
Staying still cost you nothing and you pick 25% of current cell available ressources. If a ship doesn't have enought ressources to pay the 10% to move, he is pinned on the cell for this turn (can't go negative).
My goal: The ship starts at the center (0) and should return at this point with atleast (X amount of ressources) in the less turn possible.
How can I compute the most efficient path (round wise) that my ship should take to achieve my goal ?
Here is some code to compute a random map (the map is random every game) :
import random
how_Big = 11
center_Pos = int(how_Big/2) #how_Big must be even
game_Map = [[random.randint(1,500) for i in range(how_Big)] for x in range(how_Big)]
game_Map[center_Pos][center_Pos] = 0
for y in range(how_Big):
print(game_Map[y])
[110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123]
[415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252]
[368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344]
[391, 480, 485, 358, 416, 479, 270, 354, 203, 436, 146]
[62, 132, 490, 33, 445, 172, 127, 274, 130, 77, 356]
[239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202]
[241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237]
[85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182]
[398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428]
[213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53]
[207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188]
I am not really sure how to start. Is there any known algorithm for this ?
Thanks
Additionnal informations :
The matrix is the available ressources at the beginning on each cell.
Ship begins with 0 ressources.
Ship gains ressources by staying still for a round on a cell
(collecting 25% of the cell's ressources).
Ship spends ressources (of his own cargo) by moving (the cost is 10%
of current cell's available ressources).
The path can be returned in anyway, for example (moves = ["up",
"still", "still", "up", "still", "still", "down", "down"]).
BruteForcing it would be acceptable.
X would be the minimum ammount I want my ship's cargo to have when
returning to center.
Map always has a center Cell
Ressources collected or used are always rounded to nearest Int
Ressources collected by a ship are removed from the cell
Here is an example of what can happen :
move_Dict = {
"up": [-1, 0],
"down": [1, 0],
"right": [0, 1],
"left": [0, -1],
"still": [0, 0],
}
moves = ["up", "still", "still", "up", "still", "still", "down", "down"]
position = [5, 5]
cargo = 0
print("initial map :")
for y in range(how_Big):
print(game_Map[y])
for move in moves:
if move == "still":
ressources_Collected = round(game_Map[position[0]][position[1]] * 0.25)
cargo += ressources_Collected
game_Map[position[0]][position[1]] -= ressources_Collected
else:
ressources_Used = round(game_Map[position[0]][position[1]] * 0.10)
cargo -= ressources_Used
position = [position[0]+move_Dict[move][0], position[1]+move_Dict[move][1]]
print(f"ship is at position {position}, cargo = {cargo}")
for y in range(how_Big):
print(game_Map[y])
Output:
initial map :
[110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123]
[415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252]
[368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344]
[391, 480, 485, 358, 416, 479, 270, 354, 203, 436, 146]
[62, 132, 490, 33, 445, 172, 127, 274, 130, 77, 356]
[239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202]
[241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237]
[85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182]
[398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428]
[213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53]
[207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188]
ship is at position [4, 5], cargo = 0
[110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123]
[415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252]
[368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344]
[391, 480, 485, 358, 416, 479, 270, 354, 203, 436, 146]
[62, 132, 490, 33, 445, 172, 127, 274, 130, 77, 356]
[239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202]
[241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237]
[85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182]
[398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428]
[213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53]
[207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188]
ship is at position [4, 5], cargo = 43
[110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123]
[415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252]
[368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344]
[391, 480, 485, 358, 416, 479, 270, 354, 203, 436, 146]
[62, 132, 490, 33, 445, 129, 127, 274, 130, 77, 356]
[239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202]
[241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237]
[85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182]
[398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428]
[213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53]
[207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188]
ship is at position [4, 5], cargo = 75
[110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123]
[415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252]
[368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344]
[391, 480, 485, 358, 416, 479, 270, 354, 203, 436, 146]
[62, 132, 490, 33, 445, 97, 127, 274, 130, 77, 356]
[239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202]
[241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237]
[85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182]
[398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428]
[213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53]
[207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188]
ship is at position [3, 5], cargo = 65
[110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123]
[415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252]
[368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344]
[391, 480, 485, 358, 416, 479, 270, 354, 203, 436, 146]
[62, 132, 490, 33, 445, 97, 127, 274, 130, 77, 356]
[239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202]
[241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237]
[85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182]
[398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428]
[213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53]
[207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188]
ship is at position [3, 5], cargo = 185
[110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123]
[415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252]
[368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344]
[391, 480, 485, 358, 416, 359, 270, 354, 203, 436, 146]
[62, 132, 490, 33, 445, 97, 127, 274, 130, 77, 356]
[239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202]
[241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237]
[85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182]
[398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428]
[213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53]
[207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188]
ship is at position [3, 5], cargo = 275
[110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123]
[415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252]
[368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344]
[391, 480, 485, 358, 416, 269, 270, 354, 203, 436, 146]
[62, 132, 490, 33, 445, 97, 127, 274, 130, 77, 356]
[239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202]
[241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237]
[85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182]
[398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428]
[213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53]
[207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188]
ship is at position [4, 5], cargo = 248
[110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123]
[415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252]
[368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344]
[391, 480, 485, 358, 416, 269, 270, 354, 203, 436, 146]
[62, 132, 490, 33, 445, 97, 127, 274, 130, 77, 356]
[239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202]
[241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237]
[85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182]
[398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428]
[213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53]
[207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188]
ship is at position [5, 5], cargo = 238
[110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123]
[415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252]
[368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344]
[391, 480, 485, 358, 416, 269, 270, 354, 203, 436, 146]
[62, 132, 490, 33, 445, 97, 127, 274, 130, 77, 356]
[239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202]
[241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237]
[85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182]
[398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428]
[213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53]
[207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188]
[Finished in 0.1s]

Decompress a Zip-compressed Binary string

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).

Resources