How to represent (serialise) an arbitrarily nested array, as a one-dimensional array of values (and meta-data) so that the original nested array and it's structure can be recreated from the serialised one-dimensional array?
I'm looking for a space efficient algorithms for this problem.
For example:
[
[
[1, 2, 3, 4]
],
[
[5, 6, 7, 8],
[9, 10]
]
]
Should be serialised / deserialised to / from something like this
[/* elements of meta data* /, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
You could represent a nested array with a sequence that first contains the length of that array (also providing the information that the following content is a sub-array) and then the elements themselves.
If you only have positive numbers as your values, but can store negative numbers, you could use negative numbers as the indicator for sub-arrays (if not, you can of course just use an offset O, which is the highest number you want to store, and treat all numbers greater then O as indicator for a new sub-array). The serialized version of your example would then look like this:
[-2, -1, -4, 1, 2, 3, 4, -2, -4, 5, 6, 7, 8, -2, 9, 10]
To understand better how it's working, here is an indented version of the same serialized array:
[-2,
-1,
-4
1, 2, 3, 4
-2
-4
5, 6, 7, 8
-2
9, 10
]
This structure can be serialized and deserialized in linear time using a recursive algorithm.
Related
I am making a slot-machine game, and I have an array of numbers with 5 or 6 positions containing sorted numbers.
Some rules:
0 is the wild symbol. It forms a pay line with any other paying symbol. For example, if the received array was: [1, 2, 0, 2, 3], we have a pay line;
the paying symbols are: [1, 2, 3, 4, 5, 6, 7, 8, 9];
and there are the non-paying symbols, which are: [10, 11, 12, 13, 14, 15].
I need to build a function in Typescript that parses whether:
array contains at least 3 numbers repeated in sequence
if these repeated numbers are between 0 and 9
when negative returns an empty array as in the example "input [1, 2, 6, 7, 6] the expected output is [[1, 2, 6, 7, 6],[]] as well [[array sorted],[empty array]]"
when positive, return an array as in the example "input [1, 2, 6, 6, 6] the expected output is [6, [2, 3, 4]]] as well [repeated number,[positions of repeated number]] "
type WinningCombinationsResult = [number, number[]][];
function call(lines: number[]): WinningCombinationsResult {
return ;
}
export const WinningCombinations = { call };
So an example of the question is as follows:
Let's say we want to subdivide [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] into 3 sub-arrays.
While I imagine there are many correct answers one of them would be, [10, 8], [9, 7, 2], [1, 3, 4, 5, 6]. The reason being that here the sum of the sub arrays is, 18, 18, 19, meaning they are as close to equal as they can possibly be.
Is there an algorithm that can consistently return such an answer given any starting array and any number of sub-arrays to divide into? (Assuming that: length of the starting array > number of sub-arrays)
(PS If you want to show your logic in code I feel the most comfortable with python.)
Assuming to have an array like this:
a = np.array([[10, 3, 7, 1, 3, 3],
[ 7, 2, 4, 2, 4, 6],
[ 4, 1, 9, 3, 5, 7]])
scipy.stats.scoreatpercentile or numpy.percentile would allow me to get the percentiles for each element of an array:
from scipy import stats
test = stats.scoreatpercentile(a_transposed, 50, axis=1)
print (test)
# Output: [3. 4. 4.5]
For me it is even more interesting to get the score of a percentile. Therefore I would use scipy.stats.percentileofscore but this ('reverse')function is not applicable to this type of array since it does not take an axis-parameter like the related scipy.stats.scoreatpercentile. Is there any other smooth way to apply this function for each element of the array?
I need to Write a Java method int[] easyAs123(int[] nums) that takes an array of integers and returns an array that contains exactly the same numbers as the given array, but rearranged so that every 1 and 2 is immediately followed by a 3, in the order of the appearance, as in the test cases below.
Specifically,
In the input array, every 1 is always immediately followed by a 2.
However, even though every 2 is immediately followed by a number, not every 2 is immediately followed by a 3.
Do not move the 1 and 2's, but every other number may move to swap place with a 3.
The input array contains the same number of 1's, 2's and 3's.
for example:easyAs123([5, 1, 2, 4, 3, 5]) → [5, 1, 2, 3, 4, 5]
easyAs123([1, 2, 9, 8, 3, 5, 3, 7, 1, 2, 6, 4]) → [1, 2, 3, 8, 9, 5, 6, 7, 1, 2, 3, 4]
Suppose I have an array with 10 elements, e.g. a=np.arange(10). If I want to create another array with the 1st, 3rd, 5th, 7th, 9th, 10th elements of the original array, i.e. b=np.array([0,2,4,6,8,9]), how can I do it efficiently?
thanks
a[[0, 2, 4, 6, 8, 9]]
Index a with a list or array representing the desired indices. (Not 1, 3, 5, 7, 9, 10, because indexing starts from 0.) It's a bit confusing that the indices and the values are the same here, so have a different example:
>>> a = np.array([5, 4, 6, 3, 7, 2, 8, 1, 9, 0])
>>> a[[0, 2, 4, 6, 8, 9]]
array([5, 6, 7, 8, 9, 0])
Note that this creates a copy, not a view. Also, note that this might not generalize to multiple axes the way you expect.