Related
I know there are already similar questions (1, 2, 3) but they are all in Python and they do not fit what I need.
Given two sorted list, say 1, 6, 13, 21, 28 and 2, 15, 20. The indices, without repeating (unlike link 1 above), of the closest number in the first array are returned, in this case 0, 2, 3.
The tricky point is, in the case 1, 4, 66, 67, 68, 71 and 68, 68, 68, 82, 82, returning 2, 3, 4, 5, 1 is more preferable than 1, 2, 3, 4, 5.
It is possible that the length of the first list < the length of the second list. 1, 7, 11 and 6, 24, 28, 32, 34 should return 0, 1, 2, X, X, where X can be any integer other than 0, 1 and 2. (Both 0, 1, 2, -1, -1 and 0, 1, 2, 3, 4 are acceptable.)
Edit: Just swap the two lists and return 0, 1, 2.
Codes given in C-like languages or pseudocode is preferable.
Any idea better than a brute-force search?
Edit
The given examples may not be the best solutions, e.g. the final (struckthrough) example could return 1, 0, 2 (1, 0, 2, X, X) instead.
Let the first array be a and the second be b.
Let the cost of assigning index i to b[j] be abs(a[i] - b[j]). Then we can find a solution by modelling our problem as an unbalanced assignment problem in O(nr2) time where r is the size of the smallest array and n the biggest.
hi there i need to parse the byte data of a response from api the data is
[0, 2, 0, 44, 0, 6, 58, 1, 0, 1, 109, 85, 0, 0, 0, 1, 0, 1, 111, 97, 0, 115, 224, 4, 0, 0, 0, 0, 0, 0, 1, 114, 0, 1, 115, 169, 0, 1, 116, 18, 0, 1, 108, 121, 0, 1, 113, 241, 0, 44, 0, 13, 128, 1, 0, 0, 55, 200, 0, 0, 0, 10, 0, 0, 55, 227, 5, 172, 149, 3, 0, 0, 84, 154, 0, 0, 0, 0, 0, 0, 56, 79, 0, 0, 57, 28, 0, 0, 54, 226, 0, 0, 56, 89]
there are certain rules to parse the data according to api provider as below
A The first two bytes ([0 - 2] -- SHORT or int16) represent the number of packets in the message.
B The next two bytes ([2 - 4] -- SHORT or int16) represent the length (number of bytes) of the first packet.
C The next series of bytes ([4 - 4+B]) is the quote packet.
D The next two bytes ([4+B - 4+B+2] -- SHORT or int16) represent the length (number of bytes) of the second packet.
C The next series of bytes ([4+B+2 - 4+B+2+D]) is the next quote packet.
please someone help me how to parse this data according to these rules in dart. i am stuck thanks for help
First, you need to convert the list of integers into a list of bytes:
final byteList = Uint8List.fromList(responseList);
Then you need to create a ByteData from that byte list:
final byteData = ByteData.view(byteList.buffer);
Then you can do your parsing of various bytes, shorts, ints, or longs that you want at various byte offsets. For example:
final packetNum = byteData.getUint16(0);
final firstPacketLength = byteData.getUint16(2);
final firstPacketView = ByteData.sublistView(byteData, 4, 4 + firstPacketLength);
// Do whatever you need for that packet
final secondPacketPos = 4 + firstPacketLength;
final secondPacketLength = byteData.getUint16(secondPacketPos),
final secondPacketView = ByteData.sublistView(byteData, secondPacketPos + 2, 4 + secondPacketLength);
// Do whatever you need for the saecond packet
I have an array of arrays that looks like
time
array([array([ 0, 1, 0, 10, 12, 2011], dtype=int16),
array([ 0, 1, 0, 10, 12, 2011], dtype=int16),
array([ 0, 1, 0, 10, 12, 2011], dtype=int16), ...,
array([ 0, 59, 23, 10, 12, 2011], dtype=int16),
array([ 0, 59, 23, 10, 12, 2011], dtype=int16),
array([ 0, 59, 23, 10, 12, 2011], dtype=int16)],
dtype=object)
and I would like to transform this into something like
time
array([0:1:0 10-12-2011,
etc
0:59:23 10-12-2011])
I feel like I should be able to do this for the whole structure without having to loop through each individual row/column.
I would say you cannot avoid loops, but you can get a pretty decent result by looping through the outer array and converting your data into datetime objects. Let's say a is your array:
from datetime import datetime
results = array([datetime(*row[::-1]) for row in a])
I asked a question here about numerically integrating on a 2d array with fixed length. Now what if the integration length is not fixed? For each cell as the starting point, I want to keep integrating until it encounters a cell with value of the opposite sign. So suppose in a column from bottom to top it is [1,2,5,4,-2,-3,2], if I do the integral for the first element, it will integrate the first four elements (they are all positive). If I start from the fifth element, it will just integrate -2 and -3. Are there any ways to vectorize it or speed it up instead of using a double for loop to first find the integration length for each cell and then do the integral?
Or a simplified problem is just to integrate the positive elements:
example:
data = [
-2, -1, 4, -2,-1;
1, 2, 3, 4, 5;
5, -4, -3, 2, 5;
3, -3, -9, 5, 7;
2, -2, 7, -5, 1;
2, 3, 1, -3, -3]
integrated_data = [
0, 0, 7, 0, 0;
13, 2, 3, 11, 18;
12, 0, 0 7, 13;
7, 0, 0, 5, 8;
4, 0, 8, 0, 1;
2, 3, 1, 0, 0]
A vectorization solution in MATLAB
data = [
-2, -1, 4, -2,-1;
1, 2, 3, 4, 5;
5, -4, -3, 2, 5;
3, -3, -9, 5, 7;
2, -2, 7, -5, 1;
2, 3, 1, -3, -3];
data1 = [-ones(1,size(data,2)) ;flipud(data)]
df = find([-1 ;diff((data1(:))>=0)] == 1)-1;
data1(data1<0) =0;
c1 = cumsum(data1(:));
data1(df) = data1(df) - [0 ;diff(c1(df))];
c2 = cumsum(data1(:));
c2(data1==0)=0;
c2=reshape(c2,size(data1));
result = flipud(c2(2:end,:))
Its was runnning fine and then it through me this error
1125 Error #: 117 index is beyond the scope of 115.
It doesn't list a row number but the function below is the only place where a long array is referred to.
The error means its trying to access between end of the vector array- It shouldn't be possible.
Relevant code parts (the rest-public functions and other functions not include all work fine).
public class Main extends Sprite
{
internal var oneoff:Boolean = true;
internal var kanaList:Vector.<String> = new <String>["あ/ア", "あ/ア", "え/え", "え/え", "い/イ", "い/イ", "お/オ", "お/オ", "う/ウ", "う/ウ", "う/ウ", "う/ウ", "か/カ", "か/カ", "け/ケ", "け/ケ", "き/キ", "き/キ", "く/ク", "く/ク", "こ/コ", "こ/コ", "さ/サ", "さ/サ", " し/シ", " し/シ", "す/ス", "す/ス", "そ/ソ", "そ/ソ", "す/ス", "す/ス", "た/タ", "た/タ", "て/テ", "て/テ", " ち/チ", " ち/チ", "と/ト", "と/ト", "つ/ツ", "つ/ツ", "ら/ラ", "ら/ラ", "れ/レ", "れ/レ", "り/リ", "り/リ", "ろ/ロ", "ろ/ロ", "る/ル", "る/ル", "だ/ダ", "で/デ", "じ/ジ", "ど/ド", "ず/ズ", "ざ/ザ", "ぜ/ゼ", "ぞ/ゾ", "な/ナ", "ね/ネ", "に/二", "の/ノ", "ぬ/ヌ", "じゃ/ジャ", "じゅ/ジュ", "じょ/ジョ", "ん/ン", "しゃ/シャ", "しゅ/シュ", "しょ/ショ", "や/ヤ", "ゆ/ユ", "よ/ヨ", "は/ハ", "ひ/ヒ", "ふ/フ", "へ/ヘ", "ほ/ホ", "ば/バ", "ば/バ", "ぶ/ブ", "ぶ/ブ", "び/ビ", "び/ビ", "ぼ/ボ", "ぼ/ボ", "べ/ベ", "べ/ベ", "ぱ/パ", "ぴ/ピ", "ぷ/プ", "ぺ/ペ", "ぽ/ポ", "ま/マ", "み/ミ", " む/ム", "め/メ", "も/モ", "を/ヲ", "みゃ/ミャ", "みゅ/ミャ", "みょ/ミョ", "きゃ/キャ", "きゅ/キュ", "きょ/キョ", "にゃ/ニャ", "にゅ/ニュ", "にょ/ニョ", "びゃ/びゃ", "びゅ/ビュ", "びょ/ビョ", " ひゃ/ヒャ", "ひゅ/ヒュ", "ひょ/ヒョ", "ぴゃ/ピャ", "ぴゅ/ピュ", "ぴょ/ピョ", "っ/ッ", "っ/ッ"];
internal var valueList:Vector.<uint>= new <uint>[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 10, 10, 5, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 20, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1, 1];
// Lists of Kana that can be replaced in the replace mode and the substitute Kana and Values
internal var selectghostList:Vector.<String>=new<String>["ま/マ","む/ム","も/モ","か/カ","く/ク","こ/コ","な/ナ","ぬ/ヌ","の/ノ","ば/バ","ぶ/ブ","ぼ/ボ","は/ハ","ふ/フ","ほ/ホ","ぱ/パ","ぷ/プ","ぽ/ポ"];
internal var selectkanaList:Vector.<String>=new <String>["みゃ/ミャ", "みゅ/ミャ", "みょ/ミョ", "きゃ/キャ", "きゅ/キュ", "きょ/キョ", "にゃ/ニャ", "にゅ/ニュ", "にょ/ニョ", "びゃ/びゃ", "びゅ/ビュ", "びょ/ビョ", " ひゃ/ヒャ", "ひゅ/ヒュ", "ひょ/ヒョ", "ぴゃ/ピャ", "ぴゅ/ピュ", "ぴょ/ピョ"];
internal var selectghostvalueList:Vector.<uint>=new <uint>[2, 2, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2];
//Start list of playerHand contents as I don't know if Null is 0
internal var playernumber:uint;
internal var allplayersHand:Array = [[0], [0], [0], [0],[0], [0]];
internal var playerRound:uint = 1;
internal var round:uint = 1;
internal var aplayersHand:Array;
internal function create():void
{ var listLength:uint;
var row:uint
listLength = kanaList.length;
aplayersHand = allplayersHand[playerRound];
for (var i:uint = (aplayersHand.length); i <= 7; i+=1)
{row = int(Math.random() * listLength);
trace (row);
trace(i);
aplayersHand[i] = [0, kanaList[row], valueList[row],]
trace (aplayersHand);
trace (aplayersHand[i]);
kanaList.splice(row,1);
valueList.splice(row,1);
}
deal();
}
I'm assuming it's throwing the error intermittently. The reason I think it's happening is that you stored long array's length in listLength, but didn't decrement its value after
kanaList.splice(row,1);
valueList.splice(row,1);
which is why, I think, row value calculated like
row = int(Math.random() * listLength);
would sometimes return a value which is greater than array's length at that iteration.
On a sidenote, it'd be great to have what all was traced till the point you got the error. Also, the exception should show stack trace, if you compile a debug version of swf and run it in a debug flash player. The stack trace is very very useful to track down bugs like these.