I have an assignment which says I have to read from file the distances between 20 citys each. I wonder how to handle that data in the application. I thought of multidimensional array, something like Distances(0, 1, 0)=500 which means that the distance between city0 and city1 is 500 miles. Also I think this is a waste of memory because Distances(0, 1, 0) and Distances(1, 0, 0) is the same thing. My mentor told me to use triangular matrix to save the data into the app. Can you show me an example of a similar data handling or some other idea of how to handle the data? I just can't imagine it. Thank you!
What I think he means is something like this:
http://www.arenalogisticsinc.com/images/chart4.jpg
Basically a 2-D array - And if you want to save space, just remove the top half of the array since it will have repetitions.
Hope this helps.
You want an array of arrays. A multidimensional array is useful if you have a consistent array size for each of the inner arrays, but you want your first array to have length=0, the second to have length=1, etc... So, actually, you don't even need the first array - as it is just empty.
Dim triangle As Array(19)
For i = 0 To 18
Dim innerArray(i+1) As Integer
triangle(i) = innerArray
Next
Related
I'm trying to create a Minesweeper game.
I have a 4x4 set of buttons equally spaced in main.Storyboard.
My plan is to create a random array which places a 0 or * in the 1st/2nd/3rd/4th arrays. I would do this by using the arc4Random method.
With the remaining blank cells, I then have to check how many mines there could be for the 8 (potential) squares around the cell/button. This would be governed by the boundary conditions (0,0 to 3,3).
Once this is set up, I would then set the background and number label to the same colour. I could then write an if or else statement to change the colour after each button is pressed.
I'm quite struggling how to start this off and actually write this. Can anyone please give me some advice please?
Well,
you can get a boolean like this.
bool hasMine = arc4random() % 2;
this will give you 50% chance to get a bomb... if you want less bomb, increase the value (3 will give you 2 bomb free square, for one with a bomb, etc..)
then a "" or a "*" like this;
NSSString * value = hasMine ? #"*" : #"" ;
then it's just a matter of a for loop to populate your arrays.
for the sake of performance, I wouldn't use a n x n nested array but a single arrray of nxn size (in your case a array with 16 value). Then I will set a tag for 0 to (nxn -1) to each button based on its position, and on click I'll get the tag of the pressed button and retrive the value of the object at this position in the array
There must be some 'pythonic' way to do this, but I don't think np.place, np.insert, or np.put are what I'm looking for. I want to replace the values inside of a large 3D array A with those of a smaller 3D array B, starting at location [i,j,k] in the larger array. See drawing:
I want to type something like A[i+, j+, k+] = B, or np.embed(B, A, (i,j,k)) but of course those are not right.
EDIT: Oh, there is this. So I should modify the question to ask if this is the best way (where "best" means fastest for a 500x500x50 array of floats on a laptop):
s0, s1, s2 = B.shape
A[i:i+s0, j:j+s1, k:k+s2] = B
Your edited answer looks fine for the 3D case.
If you want the "embed" function you mentioned in the original post, for arrays of any number of dimensions, the following should work:
def embed( small_array, big_array, big_index):
"""Overwrites values in big_array starting at big_index with those in small_array"""
slices = [np.s_[i:i+j] for i,j in zip(big_index, small_array.shape)]
big_array[slices]=small_array
It may be worth noting that it's not obvious how one would want "embed" to perform in cases where big_array has more dimensions than small_array does. E.g., I could imagine someone wanting a 1:1 mapping from small_array members to overwritten members of big_array (equivalent to adding extra length-1 dimensions to small_array to bring its ndim up to that of big_array), or I could imagine someone wanting small_array to broadcast out to fill the remainder of big_array for the "missing" dimensions of small_array. Anyway, you might want to avoid calling the function in those cases, or to tweak the function to ensure it will do what you want in those cases.
I am not sure how this is possible but I have a for loop not incrementing properly through an array.
Basically what I have is:
for (AISMessage *report in disarray){
NSLog(#"Position of array = %ld\n", [aisArray indexOfObject:report]);
}
There is more code in the loop but is nothing strange just formatting some of the data in the object and outputting it to a file.
The output of these lines would look something like:
Position of array = 0
...
Posiiton of array = 78176
Posiiton of array = 78177
Posiiton of array = 78178
Posiiton of array = 78178
Posiiton of array = 78180
Posiiton of array = 78181
...
Posiiton of array = 490187
For some reason the report at index 78178 gets read in twice and the report at 78179 gets skipped completely.
Any ideas on what may cause this?
I am totally confused.
Thanks in advance,
Jason
The object occurs twice in the array, so the indexOfObject finds the element at index 78179 at index 78178.
In other words, you have this case:
...
[78177] = x
[78178] = y
[78179] = y
[78180] = z
...
Also, you're not searching the same array you're iterating over, that might have something to do with it as well.
Since the positions reported are so high, I would try to find a better data structure than a simple array. For it to report a position of 78178, it will have to have compared the object against the preceeding 78177 elements, and this will only take more and more time as you get further into the array.
From the posted code you are iterating over AISMessage objects in the disarray array, but you are reporting on the position of the object in the aisArray array.
I have no idea if these are meant to be the same array or not. But if they are different arrays, then do you expect the objects to be in the same order?
I am writing a game, which need a map, and I want to store the map. The first thing I can think of, is using a 2D-array. But the problem is what data should I store in the 2D-array. The player can tap different place to have different reaction. So, I am thinking store a 2D-array with objects, when player click some position, and I find it in the array, and use the object in that array to execute a cmd. But I have a concern that storing lots of object may use lots of memory. So, I am think storing char/int only. But it seems that not enough for me. I want to store the data like that:
{
Type:1
Color:Green
}
No matter what color is, if they are all type 1, the have same reactions in logic, but the visual effect is based on the color. So, it is not easy to store using a prue char/int data, unless I make something like this:
1-5 --> all type 1. 1=color green ,
2=color red, 3 = color yellow.... ...
6-10 --> all type 2. 2 = color green,
2 = color red ... ...
So, do you have any ideas on how to minimize the ram use, but also easy for me to read... ...thx
Go ahead and store a bunch of objects in the array, but with these two refinements:
Store a pointer to the object, not the object itself. (Objective C may handle this for you automatically; I don't know.)
Remember that a pointer to a single object can appear in more than one position in the array. All squares that share the same color and behavior can share an object.
It would also help if you did the math on the size of the array and the number of distinct squares so we can know exactly how much RAM you are talking about.
Array 1 | Array 2
=================
1 | 2
2 | 3
3 | 4
5 | 5
| 6
What is a good algorithm to 'sync' or combine Array 2 into Array 1? The following needs to happen:
Integers in Array 2 but not in Array 1 should be added to Array 1.
Integers in both Arrays can be left alone.
Integers in Array 1 but not in Array 2 should be removed from Array 1.
I'll eventually be coding this in Obj-C, but I'm really just looking for a pseudo-code representation of an efficient algorithm to solve this problem so feel free to suggest an answer in whatever form you'd like.
EDIT:
The end result I need is bit hard to explain without giving the backstory. I have a Cocoa application that has a Core Data entity whose data needs to be updated with data from a web service. I cannot simply overwrite the contents of Array 1 (the core data entity) with the content of Array 2 (the data parsed from the web into an array) because the Array 1 has relationships with other core data entities in my application. So basically it is important that integers contained in both Arrays are not overwritten in array one.
Array1 = Array2.Clone() or some equivalent might be the simplest solution, unless the order of elements is important.
I'm kind of guessing since your example leaves some things up in the air, but typically in situations like this I would use a set. Here's some example code in Obj-C.
NSMutableSet *set = [NSMutableSet set];
[set addObjectsFromArray:array1];
[set addObjectsFromArray:array2];
NSArray *finalArray = [[set allObjects] sortedArrayUsingSelector:#selector(compare:)];
(Assuming this is not a simple Array1 = Array2 question,) if the arrays are sorted, you're looking at a single O(n+m) pass over both arrays. Point to the beginning of both arrays, then advance the pointer containing the smaller element. Keep comparing the elements as you go and add/delete elements accordingly. The efficiency of this might be worth the cost of sorting the arrays, if they aren't already such.
In my approach, You will need Set data structure. I hope you can find some implementations in Obj-C.
Add all elements of Array1 to Set1
and do the same for Array2 to Set2.
Loop through elements of Array1.
Check if it is contained in Set2
(using provided method.) If it is
not, removed the element from Set1.
Loop through elements of Array2. If it
is not existed in Set1 yet, add it
to Set1.
All elements of Set1 is now your "synced" data.
The algorithm complexity of "contains","delete", and "add" operation of "Set" on some good implementation, such as HashSet, would give you the efficiency you want.
EDIT: Here is a simple implementation of Set assumed that the integer are in limited range of 0 - 100 with every elements initialized to 0, just to give more clear idea about Set.
You first need to define array bucket of size 101. And then for ..
contains(n) - check if bucket[n] is 1 or not.
add(n) - set bucket[n] to 1.
delete(n) - set bucket[n] to 0.
You say:
What is a good algorithm to 'sync' or combine Array 2 into Array 1? The following needs to happen:
Integers in Array 2 but not in Array 1 should be added to Array 1.
Integers in both Arrays can be left alone.
Integers in Array 1 but not in Array 2 should be removed from Array 1.
Here's some literal algorithmic to help you (python):
def sync(a, b):
# a is array 1
# b is array 2
c = a + b
for el in c:
if el in b and el not in a:
a.append(el) # add to array 1
elif el in a and el not in b:
a.remove(el) # remove from array 1
# el is in both arrays, skip
return a # done
Instead of "here's what needs to happen", try describing the requirements in terms of
"here's the required final condition". From that perspective it appears that the desired end-state is for array1 to contain exactly the same values as array2.
If that's the case, then why not the equivalent of this pseudocode (unless your environment has a clone or copy method)?
array1 = new int[array2.length]
for (int i in 0 .. array2.length - 1) {
array1[i] = array2[i]
}
If order, retention of duplicates, etc. are issues, then please update the question and we can try again.
Well, if the order doesn't matter, you already have your algorithm:
Integers in Array 2 but not in Array 1 should be added to Array 1.
Integers in both Arrays can be left alone.
Integers in Array 1 but not in Array 2 should be removed from Array 1.
If the order of the integers matter, what you want is a variation of the algorithms that determine the "difference" between two strings. The Levenshtein algorithm should suit your well.
However, I suspect you actually want the first part. In that case, what exactly is the question? How to find the integers in an array? Or ... what?
Your question says nothing about order, and if you examine your three requirements, you'll see that the postcondition says that Array2 is unchanged and Array1 now contains exactly the same set of integers that is in Array2. Unless there's some requirement on the order that you're not telling us about, you may as well just make a copy of Array2.