JSON schema: Enforce array conent by options - arrays

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

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

Return a map and store the result in diferent variables

In JavaScript I can store the values of array into variables like this:
[a, b, c] = [1, 2, 3]
I created a variable function( it return a map with the same number of arguments), I would like to know if Go has a shortcut like JavaScript
Based on the suggestion from comments ,I created a small example for your scenario:
package main
import (
"fmt"
)
func main() {
myArray := []int{1, 2, 3}
fmt.Println(myArray)
a, b, c := myArray[0], myArray[1], myArray[2]
fmt.Println(a, b, c)
}
Output:
[1 2 3]
1 2 3
Javascript's destructuring assignment syntax is not available in Go, however Go's syntax is sometimes concise too, depending what your input looks like.
Declaring and initializing several variables in a single LOC is straightforward, and it works even with variables of different types:
a, b, c := 42, "hello", 5.0
Source. Playground.
You can assign values to existing variables as well:
a, b, c = 42, "hello", 5.0
Playground.
If your input data is a slice s, then per #Gopher's answer the code will look like:
a, b, c := s[0], s[1], s[2]

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?

Add an object to an Array based on dictionary Array

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?

Resources