Add an object to an Array based on dictionary Array - arrays

I believe this would be an algorithm question, so it doesn't matter what language this is in, although I think in my case it will be implemented in jquery.
So the idea is I have two arrays of type Object. In this object there are many variables, but the only relevant one to this is a String. Lets say for argument sake, I have array [A, A, A, A, B, B, E, E, F] and the 'dictionary' array [A, B, C, D, E, F]. Whenever I want to add something to the first array, I want it to be added by the location of the object in the second array. So for instance, if I wanted to add D, the first array would now look like [A, A, A, A, B, B, D, E, E, F].
To add D, there is not necessarily a C or D to search for to put the D in after. In addition, there can be repeats like in the example. The array could also be empty that the program is adding 'D' to. Also, if there are already Ds in the first array, the D would go after the last D.
Important: The letters I used are only for clarification sake, the actual code uses words that are NOT in alphabetical order, so solutions using alphabetical order do NOT work in this instance.
Is there a better solution to this then brute force?

Related

Matlab: How to do `repelem` where each element is a matrix?

Suppose I have m-by-n matrices A, B, C arranged in an m-by-n-by-3 tensor P:
P = cat(3, A, B, C);
I now want to make a new tensor where each matrix is repeated K times, making the third dimension size 3K. That is, if K=2 then I want to build the tensor
Q = cat(3, A, A, B, B, C, C);
Is there a nice builtin way to achieve this, or do I need to write a loop for it? Preferably as fast or faster than the manual way.
If A, B, C were scalars I could have used repelem, but it does not work the way I want for matrices. repmat can be used to build
cat(3, A, B, C, A, B, C)
but that is not what I am after either.
As noted by #Cris Luengo, repelem(P, 1, 1, k) will actually do what you want (in spite of what the MATLAB documentation says), but I can think of two other ways to achieve this.
First, you could use repmat to duplicate the tensor k times in the second dimension and then reshape:
Q = reshape(repmat(P, 1, k, 1), m, n, []);
Second, you could use repelem to give you the indices of the third dimension to construct Q from:
Q = P(:, :, repelem(1:size(P,3), k));

Does Kotlin support string/array slices by reference like Rust and Swift?

In Rust and Swift you can create a slice of either/both arrays and strings which doesn't make a new copy of the elements but gives you a view into the existing elements using a range or iterator and implemented internally a reference pair or reference + length.
Does Kotlin have a similar concept? It seems to have a similar concept for lists.
It's hard to Google since there is a function called "slice" which seems to copy the elements. Have I got it all wrong or is it missing?
(I'm aware I can work around it easily. I have no Java background btw.)
I don't think so.
You can use asList to get a view of the Array as a list. Then the function you already found, subList works as usual. However, I'm not aware of an equivalent of subList for Arrays. The list returned from asList is immutable, so you cannot use it to modify the array. If you attempt to make the list mutable via toMutableList, it will just make a new copy.
fun main() {
val alphabet = CharArray(26) { 'a' + it }
println(alphabet.joinToString(", "))
val listView = alphabet.asList().subList(10, 15)
println(listView)
for (i in alphabet.indices) alphabet[i] = alphabet[i].toUpperCase()
println(alphabet.joinToString(", "))
// listView is also updated
println(listView)
}
Output:
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
[k, l, m, n, o]
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
[K, L, M, N, O]
Using lists (or Collections) is preferred in Kotlin, and comes with much better support for generics and immutability than arrays.
If you need to mutate arrays, you probably have to manage it yourself, passing around (a reference to) the array and an IntRange of the indices you care about.

JSON schema: Enforce array conent by options

I am having a JSON array for which I would like to enforce it's contents.
Let's say I have four object types A, B, and C and D. My array may contain an arbitrary number of items from either type A, B, C or string. D or other types are however not allowed.
i.e.
[A, B, C, A, A, B, "stuff", "morestuff", C] is valid
[A, A, 3, C] is not valid (contains number 3)
[A, D, A, B] is not valid (containts D)
Reading the spec it looks as if this is not possible. There is only tuple validation and list validation.
However with tuple validation a) the order matters and b) there can not be an arbitrary number of objects of the same type. List validation fails as there are objects of different types.
Is this really not possible?
Yes it's possible by using array of types as type for items:
{
"type": "array",
"items": {
"type": [A, B, C, "string"]
}
}
reference:
Array's items type:
https://json-schema.org/understanding-json-schema/reference/array.html#list-validation
more than one option for a type:
http://json-schema.org/understanding-json-schema/reference/type.html

Processing: How can I make a game board with an array?

I need to make a game board - using an array - for a Bomberman game remake in Processing.
This means the game board / array (which is rather large [9x9] and has 3 values [a,b,c] throughout), has to be able to:
Define the color/sprite of the according fields
Set the limit for where the character can walk
Have unique properties for each value/field (one value represents the
open space where the player can move around, another is a solid
block, and another one breaks when a bomb is blown up next to it and
transforms into an open field type)
I'm pretty much a programming noob and I barely a clue how to make all of this work..
I've already set up the array though which look like this:
int [][] board = {
{b, b, b, b, b, b, b, b, b},
{b, a, b, a, b, a, b, c, b},
{b, c, c, a, a, a, a, a, b},
{b, c, a, a, c, a, a, a, b},
{b, c, c, b, a, b, c, a, b},
{b, a, c, a, a, a, a, a, b},
{b, b, a, b, c, b, b, c, b},
{b, a, a, a, c, a, a, c, b},
{b, b, b, b, b, b, b, b, b} };
And I have managed to draw it as a monochrome chessboard.
Now I just have to figure out how to give each value the properties of the according block type.
Thanks for any help in advance :)
Stack Overflow isn't really designed for general "how do I do this" type questions. It's for specific "I tried X, expected Y, but got Z instead" type questions. But I'll try to help in a general sense:
You need to break your problem down into smaller pieces and then approach each of those pieces one at a time.
For example, I'd start with a simple program that just draws a grid of squares. Then work your way up from there: can you make it so the number of squares is a variable (or two variables) that you define at the top of your sketch? Now make it so each square is a different color. Now make it so each square is reading from the array.
Work your way up in small pieces, and if you get stuck, post a MCVE in a new question and we'll go from there. Good luck.
Going off of what Kevin Workman said, here is code you could possibly use for a grid of squares:
void setup(){
size(800, 800);
background(255, 204, 5);
fill(158, 10, 10);
for(int i = width/8; i <= width; i += width/4){
for(int v = height/8; v <= height; v += height/4){
rect(i, v - height/8, width/8, height/8);
rect(i - width/8, v, width/8, height/8);
}
}
}
It's not necessarily an array, but a for loop does the job.

Find all "and" combinations from multiple sets

Let's say I have x sets of objects, and each set has a certain number objects. I want create an array which will store all the unique "and" combinations of these objects.
For example, if I have 5 objects in set A, 10 objects in set B, and 8 objects in set C, then I know that there are 5*10*8 = 400 unique ways of picking one object from each set. But I want to actually store these combinations in an array.
So the array would be multidimensional, something like:
{
{ a, a, a }
{ a, a, b }
{ a, a, c }
...
{ a, b, a }
{ a, b, b }
and so on...
}
I need the solution to as efficient as possible, because I am dealing with situations where there are potentially tens of millions of combinations. I am not exactly sure how to begin to approach this problem.
Sorry if it's not clear, but I don't really know what to call what I am trying to achieve, so I am just describing it as best I can. Thank you for any help you can provide.
Edit: Here is some more information about the problem:
The purpose of this problem is that I am going to compute a "score" value from each resulting array. Then, I want to find the top n scores and return them to the user. So actually, I believe that I wouldn't need to have the entire array in memory. I can just iterate through the array, calculate the score, and add it to the returned array if its score is high enough. That way, I only need the top n objects in memory continuously.
I hope this makes things more clear.
Quick python, probably can't get much more efficient, since you need to iterate at some point...
getItems(A, B, C):
for a in A:
for b in B:
for c in C:
items = (a, b, c) ## or [a, b, c], as desired
yield items
Or, if you're familiar with generator expressions:
gen = ((a, b, c) for a in A for b in B for c in C)
Then to use:
for combo in getItems(A, B, C): ## or for combo in gen:
## do stuff here
Edit:
def getItems(*allSets):
if len(allSets) == 0:
yield []
return
thisSet, theRest = allSets[0], allSets[1:]
for value in thisSet:
for values in getItems(*theRest):
yield [value] + values
Do you know the number of sets at design-time? If so, I would do nested for loops. If you don't know the number of sets, then you're potentially do some form of recursion to handle the looping.
With that said, I think what you're doing is, by definition, NOT efficient. Is there a reason you need to store all the possible combinations in memory, rather than generating them as needed on the fly?

Resources