2D Array vs Jagged array? - arrays

So, I've searched a bit, but not entirely sure what to search on, tbh...
I'm currently doing some "level generation" code, and creating objects to contain the objects for my generation.
Basically I have a "Cell" class which is defined by a Coordinate.
I'm then trying to create a "CellArea" class, which holds multiple cells in an area. It'd be simple enough if this was a rectangle, as I'd just use Cell[, ] then. But since this could be an "L" shape of sorts (I'm fine with limiting it to 2 "Corridors"), how would I go about doing that? Or is it simply more efficient to do some sort of list/collection?
I was wondering if you can do a Jagged Array that'd look something like...
{0, 1, 2}
{0, 1, 2}
{0, 1, 2}
{0, 1, 2}
{0, 1, 2, 3, 4, 5}
{0, 1, 2, 3, 4, 5}
{0, 1, 2, 3, 4, 5}
I hope I made my question clear? Obviously I could do a bidim array that is [7, 6] and then not fill in the "null" places, but isn't that inefficient in a way?

Yes of course you can!
int[][] jaggedArray = new int[7][];
jaggedArray[0] = new int[] {0, 1, 2};
jaggedArray[1] = new int[] {0, 1, 2};
jaggedArray[2] = new int[] {0, 1, 2};
jaggedArray[3] = new int[] {0, 1, 2};
jaggedArray[4] = new int[] {0, 1, 2, 3, 4, 5};
jaggedArray[5] = new int[] {0, 1, 2, 3, 4, 5};
jaggedArray[6] = new int[] {0, 1, 2, 3, 4, 5};
Reference: http://msdn.microsoft.com/en-us/library/2s05feca.aspx

Related

Match lengths of multiple Numpy arrays of unequal length

I'm doing some data processing on data that comes in sets of 3 thousands of values long. Sometimes the arrays are of slightly different lengths, and I am trying to find a way to find the minimal length array and match the other two to that.
# Some randomly generated sequences
a = array([7, 1, 7, 8, 0, 0, 1, 2, 8, 7, 2, 3])
b = array([0, 1, 1, 8, 3, 4, 1, 5])
c = array([8, 3, 3, 1, 4, 6, 6, 7, 3, 8, 8])
# What I'd like accomplished
a = array([7, 1, 7, 8, 0, 0, 1, 2])
b = array([0, 1, 1, 8, 3, 4, 1, 5])
c = array([8, 3, 3, 1, 4, 6, 6, 7])
This problem seems well covered for 2 arrays of different lengths but my searches didn't bring up anything for matching the lengths of multiple arrays. Looking at some of the Numpy methods like resize and array_split didn't seem to have the functionality I was looking for. Before I dive into writing some type of ugly recursive function using the directions I found matching 2 arrays, does anyone have any suggestions about how this can be accomplished conveniently?
First we can do return the min length
mlen = min(map(len, [a, b, c]))
8
Then
newl=[x[: mlen ] for x in [a,b,c]]

How to reverse multiple lists using the Wolfram-Language

I'm trying to find the best way to solve the question: "Use Range, Reverse and Join to create {3, 2, 1, 4, 3, 2, 1, 5, 4, 3, 2, 1}"
So basically the given lists are {1, 2, 3, 4, 5}, {1, 2, 3, 4}, {1, 2, 3}.
I could easily solve this question but wanted to know if there is a better way (more efficient) than what i've come up with.:
My Solutions:
In[136]:= Join[ Reverse[Range[3]], Reverse[Range[4]], Reverse[Range[5]] ]
In[141]:= Reverse[Join[ Range[5], Range[4], Range[3] ]]
given lists: {1, 2, 3, 4, 5}, {1, 2, 3, 4}, {1, 2, 3}, where you have to use the functions Range, Reverse and Join to create the expected output:
{3, 2, 1, 4, 3, 2, 1, 5, 4, 3, 2, 1}
My solution will not be efficient if there were to be 100 lists instead of three.
Thanks in advance for the help
RUNNING EACH ELEMENT OF A LIST THROUGH A FUNCTION:
listA = {}
Function[x, listA = Join[listA, x]] /# {Range[5], Range[4], Range[3]}
listB = Reverse[listA]
Clear[listA]
output:
result -> listB: {3, 2, 1, 4, 3, 2, 1, 5, 4, 3, 2, 1}
Range[#] & /* Reverse /# {3, 4, 5} // Flatten
{3, 2, 1, 4, 3, 2, 1, 5, 4, 3, 2, 1}
Update
Someone voted to delete my answer without providing a reason. Perhaps because it did not use Join. To address that
Range[#] & /* Reverse /# {3, 4, 5} // Apply[Join]

How get sets of integer which appear in at least K or more than K sub-array(s) from array. Each set must have exactly L elements ?

I want to write program with input an array which contains N sub-array(s). Each sub-array is an array of integers which has M element(s). N and M can be very large (e.g. 1.000.000). It assume that the arrays can be stored in physical memory. The program would output: All sets of integers which appear in at least K or more than K sub-array(s). Each set must have exactly L elements.
Ex:
Input:
N = 5
{1, 2, 3, 4, 5}
{1, 3, 2}
{1, 4, 3, 2, 9}
{2, 3, 4, 7, 9}
{3, 4, 5, 9, 10}
Output:
1. In case K = 3, L = 2
{1, 2}
{1, 3}
{2, 3}
{2, 4}
{3, 4}
{3, 9}
{4, 9}
2. In case K = 3, L = 3
{1, 2, 3}
{2, 3, 4}
How can I do ?
thanks

LingPipe LDA matrix representation

I am trying to extract possible topics from list of tweets and LingPipe LDA seems easy to understand and well documented with code sample.
My challenge is to produce the matrix representation using tweets data. For example,
static String[] WORDS = new String[] {
"river", "stream", "bank", "money", "loan"
};
static final int[][] DOC_WORDS = new int[][] {
{ 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 0, 0, 0 },
{ 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 0, 0 },
{ 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 0 },
{ 0, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4 }
}
The zero at the end of the above matrix is supposed to represent that none of the word in WORDS array is found in the content. However in this representation, it is presumed to be the zero index or the word 'river' is found.
As tweet is short, I am not sure how I can represent the matrix so that it can show the 'absence' of the word too.
Any advice or suggestion of other method is mush appreciated.

Finding completely dissimilar permutations

For a sequence of n distinct elements, like for example {0, 1, 2, ..., n-1}, where n is even, it is possible to identify n permutations of the sequence that are completely dissimilar from each other. If n is odd, then it is possible to identify n-1 such permutations. Note, this is just a claim and not yet a fact.
By completely dissimilar (which might not be accurate), I mean that there is no "adjacent and ordered pair" of elements, like (0,1), (2,3) and (n-3, n-2), from a certain permutation that repeats in any of the remaining permutations.
For example:
If n=2, then we can identify {0, 1} and {1, 0} as two completely dissimilar permutations.
If n=3, then we can identify {0, 1, 2} and {2, 1, 0} as two completely dissimilar permutations.
If n=4, then we can identify {0, 1, 2, 3}, {1, 3, 0, 2}, {2, 0, 3, 1} and {3, 2, 1, 0} as four completely dissimilar permutations.
If n=5, then we can identify {0, 1, 2, 3, 4}, {1, 4, 2, 0, 3}, {3, 0, 2, 4, 1}, {4, 3, 2, 1, 0} as four completely dissimilar permutations.
I will like to know:
1) Is there a general rule or algorithm to find, given a sequence, any n (if n is even) or n-1 (if n is odd) completely dissimilar permutations of that sequence?
2) Is there a formal definition of this problem?

Resources