Create JSON Array with values from table - arrays

i like to create an array and call the values. I stuck # starting :(
My Table (static)
Level | RPM 20 30 40 50 60 70 80 90 100 110 120 130
------------------------------------------------------
6 | 15 31 52 75 105 135 166 202 231 275 289
7 | 16 35 58 85 118 152 185 226 260 305 332
8 | 18 39 65 96 131 169 208 249 289 333 375
for example: # Level 6 and RPM >60<70 = 135
# Level 7 and RPM >50<60 = 118
And the bigger idea, can i interpolate between values so when i for example #65 RPM have the right values?
Is a array the right choice for this? How to build this array?
I find no example for this.
Is this the right way?
{
{
"Level6": "RPM20",
"Watt": 15,
}
{
"Level6": "RPM30",
"Watt": 31,
}
}
But in this way i type like hell!?
This is more interesting:
[
{
"Level X RPM": 6,
"20": 15,
"30": 31,
"40": 52,
"50": 69,
"60": 89,
"70": 106,
"80": 125,
"90": 143
},
{
"Level X RPM": 7,
"20": 16,
"30": 35,
"40": 52,
"50": 70,
"60": 88,
"70": 107,
"80": 124,
"90": 142
},
{
"Level X RPM": 8,
"20": 18,
"30": 39,
"40": 65,
"50": 87,
"60": 111,
"70": 135,
"80": 158,
"90": 180
}
]

I have changed and my List is now Python:
LEVEL_TO_POWER = {
1: [6,12,20,29,40,53,69,79,92,106,121],
2: [8,16,26,38,53,68,88,103,120,138,152],
3: [9,20,32,47,66,84,107,125,148,172,186],
4: [11,23,39,56,79,101,126,150,173,206,219],
5: [13,27,45,65,92,117,145,175,202,238,254],
6: [15,31,52,75,105,135,166,202,231,275,289],
7: [16,35,58,85,118,152,185,226,260,305,332],
8: [18,39,65,96,131,169,208,249,289,333,375],
9: [19,42,71,104,144,184,227,272,318,361,408],
10:[21,46,77,113,157,199,245,295,345,386,442],
11:[23,50,84,123,170,216,262,318,372,413,480],
12:[24,53,89,131,183,230,279,342,398,441,512],
13:[26,56,94,139,196,245,296,365,424,468,548],
14:[28,60,101,148,209,261,318,389,449,494,585],
15:[30,64,108,158,222,277,337,415,476,518,620],
16:[32,68,115,168,235,296,355,439,503,548,658],
17:[33,72,122,177,248,312,373,463,530,576,694],
18:[35,76,129,187,261,328,390,484,556,606,727],
19:[37,79,134,195,274,342,407,507,572,632,763],
20:[39,83,140,204,287,354,424,528,598,659,790],
21:[40,87,146,213,300,368,442,551,616,689,812],
22:[42,91,153,223,313,385,461,574,645,720,840],
23:[44,95,160,234,326,401,479,598,673,752,872],
24:[47,101,171,246,340,418,501,625,706,788,908],
}

Related

Combining elements within a 2d array python 3.x

i have this 2d array, where each array is made up of 4 lots of 4 hex values:
list1=[['74 68 61 74', '73 20 6D 79', '20 6B 75 6E', '67 20 66 75'], ['C2 5B FC F1', 'B1 7B 91 88', '91 10 E4 E6', 'F6 30 82 93']]
And I would like to firstly split the array into multiple 1d arrays then combine the array into multiple single elements rather than each element having 4 values, like follows:
list2=[74, 68, 61, 74, 73, 20, 6D, 79, 20, 6B, 75, 6E, 67, 20, 66, 75]
list 3=[C2, 5B, FC, F1, B1, 7B, 91, 88, 91, 10, E4, E6, F6, 30, 82, 93]
Please let me know how this can be done. Thank you in advance.
What you essentially want to do is "flatten" the inner lists. There are several approaches to this. The example below borrows from here. Realize that the values in your lists are just strings (even though they could represent hex values) so you need to treat them like strings when splitting them up.
In [25]: list1
Out[25]:
[['74 68 61 74', '73 20 6D 79', '20 6B 75 6E', '67 20 66 75'],
['C2 5B FC F1', 'B1 7B 91 88', '91 10 E4 E6', 'F6 30 82 93']]
In [26]: list2 = [t for sublist in list1[0] for t in sublist.split(' ')]
In [27]: list3 = [t for sublist in list1[1] for t in sublist.split(' ')]
In [28]: list2
Out[28]:
['74',
'68',
'61',
'74',
'73',
'20',
'6D',
'79',
'20',
'6B',
'75',
'6E',
'67',
'20',
'66',
'75']
In [29]: list3
Out[29]:
['C2',
'5B',
'FC',
'F1',
'B1',
'7B',
'91',
'88',
'91',
'10',
'E4',
'E6',
'F6',
'30',
'82',
'93']
In [30]:

Haproxy header differs from what is expected

I am working with Haproxy and need to read the v2 header from TCP stream, according to spec it is supposed to have first 13 bytes following:
\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A\x02
in byte
{byte[13]}
[0]: 13
[1]: 10
[2]: 13
[3]: 10
[4]: 0
[5]: 13
[6]: 10
[7]: 81
[8]: 85
[9]: 73
[10]: 84
[11]: 10
[12]: 2
however I get
[0]: 13
[1]: 10
[2]: 0
[3]: 13
[4]: 10
[5]: 81
[6]: 85
[7]: 73
[8]: 84
[9]: 10
[10]: 33
[11]: 17
[12]: 0
My read does seem to be missing first two bytes 13 10 from the spec, however ending seems to differ too, I can't seem to figure out why...
What is even more confusing I can't see from IP address (54.219.177.232) nor to (45.32.136.69) nowhere in the header they are supposed to be 17th byte onward
Count = 91
[0]: 12
[1]: 180
[2]: 183
[3]: 211
[4]: 173
[5]: 172
[6]: 31
[7]: 23
[8]: 237
[9]: 228
[10]: 70
[11]: 1
[12]: 187
[13]: 22
[14]: 3
[15]: 1
[16]: 0
[17]: 178
[18]: 1
[19]: 0
[20]: 0
[21]: 174
[22]: 3
[23]: 3
[24]: 65
[25]: 71
[26]: 172
[27]: 83
[28]: 43
[29]: 146
[30]: 158
[31]: 30
[32]: 150
[33]: 85
[34]: 84
[35]: 34
[36]: 158
[37]: 111
[38]: 242
[39]: 247
[40]: 173
[41]: 198
[42]: 13
[43]: 133
[44]: 9
[45]: 39
[46]: 111
[47]: 9
[48]: 44
[49]: 64
[50]: 232
[51]: 25
[52]: 14
[53]: 51
[54]: 110
[55]: 76
[56]: 0
[57]: 0
[58]: 32
[59]: 74
[60]: 74
[61]: 192
[62]: 43
[63]: 192
[64]: 47
[65]: 192
[66]: 44
[67]: 192
[68]: 48
[69]: 204
[70]: 169
[71]: 204
[72]: 168
[73]: 204
[74]: 20
[75]: 204
[76]: 19
[77]: 192
[78]: 19
[79]: 192
[80]: 20
[81]: 0
[82]: 156
[83]: 0
[84]: 157
[85]: 0
[86]: 47
[87]: 0
[88]: 53
[89]: 0
[90]: 10`
Code that reads header looks following:
var childSocketThread = new Thread(() =>
{
var getOrPostHttp1Acc = new List<byte>();
var getOrPostHttp1 = new byte[1];
while (getOrPostHttp1[0] != 10)
{
socket.Receive(getOrPostHttp1);
getOrPostHttp1Acc.Add(getOrPostHttp1[0]);
}
bool isGet = Encoding.ASCII.GetString(getOrPostHttp1Acc.ToArray()).ToUpper().Contains("GET");
byte[] proxyv2headerIdentifier = new byte[13];
socket.Receive(proxyv2headerIdentifier);
var proxyv2header = new List<byte>();
var proxyv2HeaderBuffer = new byte[1];
if(proxyv2headerIdentifier.SequenceEqual(proxyv2HeaderStartRequence))//TODO:uncomment this, hardcoded (true) is only for testing
{
while (proxyv2HeaderBuffer[0] != 10)
{
socket.Receive(proxyv2HeaderBuffer);
proxyv2header.Add(proxyv2HeaderBuffer[0]);
}
string headerString = $"proxyv2:{Encoding.ASCII.GetString(proxyv2header.ToArray())}";
byte[] bodyBuff = new byte[0];
byte[] buffer = new byte[1];
int contentLength = 0;
while (!headerString.Contains("\r\n\r\n"))
//at this point header is read into proxyv2header ....
Where am I misreading specification, or doing something wrong?
Ok I have figured this one myself...
mistake was to read the http method first (if it's GET or POST) haproxy does not start with that... so removing method read
var getOrPostHttp1Acc = new List<byte>();
var getOrPostHttp1 = new byte[1];
while (getOrPostHttp1[0] != 10)
{
socket.Receive(getOrPostHttp1);
getOrPostHttp1Acc.Add(getOrPostHttp1[0]);
}
bool isGet = Encoding.ASCII.GetString(getOrPostHttp1Acc.ToArray()).ToUpper().Contains("GET");
and reducing start sequence to 12 bytes did it...
In regards to IP, it was hiding in the plain sight all this time...
(check bytes 4-7)
Hope this saves you some time.

Create 3-dimensional array from 2 dimensional array in matlab

I would like to know how to generate a 3-d array from a 2-d array in matlab. My lack of understanding may simply be the result of not knowing the correct nomenclature.
I have a 2-dimensional array or matrix, A:
A = [12, 62, 93, -8, 22; 16, 2, 87, 43, 91; -4, 17, -72, 95, 6]
and I would like to add a 3rd dimension with the same values such that:
A(:,:,1) = 12 62 93 -8 22
16 2 87 43 91
-4 17 -72 95 6
and
A(:,:,2) = 12 62 93 -8 22
16 2 87 43 91
-4 17 -72 95 6
to
A(:,:,p) = 12 62 93 -8 22
16 2 87 43 91
-4 17 -72 95 6
how would I go about doing so in the most efficient way (I might have a much larger array where m = 100, n = 50, p= 1000 where A(m,n,p).
Try
result = reshape(repmat(A,1,p),m,n,p)

Ordering array according to index ~ text

Please, put in R these datas:
S.names <- c("FXI", "XLB", "GLD", "IWM", "XLE", "XLF", "EWZ", "GDX", "XLK",
"TLT", "IYR", "QQQ", "SLV", "EWJ", "XLV", "DIA", "XHB", "EEM",
"USO", "VWO", "SPY", "EFA")
strike_vec <- structure(list(Strike = c(152, 43, 61, 11, 56, 37, 36, 159, 96,
74, 71, 27, 163, 128, 35, 44, 30, 40, 81, 19, 31, 48)), .Names = "Strike", row.names =
c("DIA", "EEM", "EFA", "EWJ", "EWZ", "FXI", "GDX", "GLD", "IWM", "IYR",
"QQQ", "SLV", "SPY", "TLT", "USO", "VWO", "XHB", "XLB", "XLE",
"XLF", "XLK", "XLV"), class = "data.frame")
As you can see, strike_vec row names are equal to S.names elements.
I would like to order strike_vec elements according to the order of S.names, that is associating each strike_vec element to the position in which you find the corresponding S.names row name.
The final result should be something like
> strike_vec.new
[,1]
FXI 37
XLB 40
GLD 159
IWM 96
...
...
...
where rownames(strike_vec.new) follows exactly the order of S.names.
How may I do?
Just use :
strike_vec[S.names,,drop=FALSE]
Which gives :
Strike
FXI 37
XLB 40
GLD 159
IWM 96
XLE 81
XLF 19
EWZ 56
GDX 36
XLK 31
TLT 128
IYR 74
QQQ 71
SLV 27
EWJ 11
XLV 48
DIA 152
XHB 30
EEM 43
USO 35
VWO 44
SPY 163
EFA 61
This works because if you index the rows of a data frame with a character vector, indexing will be based on row names.

separate chaining vs linear probing

a set of objects with keys: 12, 44, 13, 88, 23, 94, 11, 39, 20, 16, 5
Write the hash table where M=N=11 and collisions are handled using separate chaining.
h(x) = | 2x + 5 | mod M
So I did it with linear probing and got
11 39 20 5 16 44 88 12 23 13 94
which I am pretty sure is right, but how do you do it with separate chaining? I realize separate chaining uses linked lists, but how would the hash table look like?

Resources