Using arc4rand() generate a ordered list - objective-c

Using int rand=((arc4random()%4)+1);
how would you make a sorted list based on the numbers generated which have to be clicked in order .Example if 4 2,3,1 was generated you have to click images based on those numbers ?

You could keep those integers [x,y,z] in a mutable array. and if you want the user to click x then y then z (indexing ascending order) then you could check the object at index 0 and see if it matches your image (I'm assuming you can map an integer to your image somehow). If it does, remove the object at index 0 from the list, if it doesn't, tell the user they have failed.
When the list is empty you know they have succeeded. you should check this after ever time your remove index 0.

Related

Extract rows from numpy and add rows to specific indexes

I have a really big 3D array (1103546X2504X3). These are genotype data, imported from VCF file. First I want to filter it, with my down data. After it, I would like to extract the needed rows, and add the missing one-s, but sorted. Right now my code is:
chr_pos is the position from my "reference" file, pos is the index position from the big array, and needed_index the rows I need.
needed_index = []
for i in range(len(chr_pos)):
for k in range(len(pos)):
if chr_pos[i] == k:
needed_index.append(k)
After the extraction I check is there any missing row from the reference:
list_difference = [item for item in chr_pos if item not in needed_pos]
needed_pos was made with the same code, but with .append(pos[k]).
My questions would be:
How to extract specific array rows, according to the list needed_index or needed pos?
How to add the missing items to the array? It would be in the format indexesX2504X[0,0], where indexes is from list_difference, 2504 is the columns (sample numbers], and [0,0] is the value for every position i want to add.
Edit1: So basically I want to find the rows in the array, what i need (from a reference file), and if some of the positions are not in the main array, add them to the specific position with the 2504 column and [0,0] value as the third dimension

How to find original position of item in ArrayList which is suggested in MultiAutoCompleteTextView?

I applied auto suggest operation on arraylist through MultiAutoCompleteTextView. When I click on suggested list I get the position of item as per suggested list but I want position of item as per items saved in arraylist. One way to get the position is
int position=arrayList.indexOf(item);
But there may be possible duplicate items in the arraylist.How do I get the actual index from arraylist?
Use values model and save one unique string per row, for e.g.,
ArrayList<Person> listPerson=new ArrayList<Person>;
listPerson.add(new Person("AbcName","uniqueKey1"));
listPerson.add(new Person("AbcName","uniqueKey2"));

Include fields of array type with dim > 1 in generated equals and hashCode?

Using IntelliJ IDEA 12.1.4, is there a way to make it possible for the equals- and hashCode-method generator (accessed via Alt + Insert) to use fields of array types with dimension larger than one ?
EDIT: Below are two screenshots. When the dimension of the array is > 1 the field show up unchecked and cannot be checked, while when it is = 1 it is in the list and automatically checked.

How to avoid multiple Iterations to search and assign one of the possible object property to a variable ?

How can we avoid multiple iteration to search for an object's property and if found then assign it to a variable else search for another key ?
eq we have Video Class with one of the field as videoType which can have values as hq(high-quality),normal(normal), def(default)..etc and so on.
From an array containing multiple video objects, how can we search and return a particular object in an order that if the array contains object with property hq then first return it,else search for normal and proceed so on. if a set of n keys are to be tested in the key set (hq,normal,def,....) then do we always need to iterate the entire array "n" times unless the key is found.
Can this be done is single iteration ? Do we need to first sort the original array in the order of occurrence of the keys in desired key set. I hope my problem statement is clear.
One possible solution for this would be to create separate NSMutableArrays for each videoType. Then, as you iterate once over your array of video objects, you check its array type and add the video to the correct array.
After you finished iterating, you create the final mutable array by concatenating the other array with addObjectsFromArray.
If you have a lot or variable list of video types, you can create the separate mutable arrays as values in an NSDictionary, where the keys are the video types. This way, you can get the target array with one step, by fetching it from the dictionary.

Fuzzy NSDictionary - use nearest key

I have a list of around 4000 numbers e.g: {10, 20, 30, 40, 50,...}
Each number is a key in an NSDictionary, so I can fetch the object associated with a number, e.g.
[NSDictionary objectForKey:[NSNumber numberWithInt:20];
However if the key is not in the dictionary, I'd like to find the nearest key (assuming there is a meaningful relationship between values, in my example, 10>20>30 etc).
So e.g.
[NSDictionary objectForKey:[NSNumberWithInt:19]] would return the value for key:20.
Or is there another data structure that would be more appropriate for doing this? I'd thought of using a sorted NSArray, where the key would be the array index, then if object was null keep incrementing the array pointer until the object is found, however this would result in a sparsly populated array with 999,999 elements :)
Thanks
Essentially you need to keep a sorted list (NSMutableArray) of keys. To find a key use the indexOfObject:inSortedRange:options:usingComparator: method of NSArray passing in NSBinarySearchingInsertionIndex as the options which will perform a binary search giving you an index even if it doesnt find the exact element. You'll have to fetch both keys yourself and compare them.
Assuming your list of numbers is in ascending order you could do a binary search in the array.
So when looking for the key x, you would start at index array.length/2, compare the key at that position with x and continue with the left part if it was greater than x or with the right part if it was less than x. Continue until you've found the closest key to x.
Thats very fast (in your case about log(4000) ~ 12 array lookups) and does not need additional storage.