How to setup the data structure? - algorithm

First let me explain what I want to do. I have n data elements. Every element needs to be checked with every other element but not with itself. The function that checks the elements returns true if everything is ok. If something is wrong than the function deletes both elements and replace them with new ones. But the new ones need to be checked with every other element too. This will be repeated until every element was checked with every other and all checks are fine.
I am asking how to setup the data structure in a most efficient way. When I test ele1 with all other n-elements and all are fine and then I test ele2 with ele84 and both get replaced I need to check ele1 and ele2 again, if these are now not fine I need to check all for ele1 again. But how to remember in the most efficient way which elements need to be check and which don’t to avoid double checking of elements?

You can use three lists: Main, CurrentNew, and NextNew
Main is initialized with the elements - use a nested loop to check them all, if you delete any elements then add the new elements to NextNew
On the next iteration, NextNew becomes CurrentNew - allocate a new NextNew list. First use a nested loop to check all elements of CurrentNew with the other elements of CurrentNew, then check the elements of Main with the elements of CurrentNew. New elements go to NextNew. Note that you're not checking the elements of Main with the other elements of Main - you already know that they're valid with each other.
On the next and each subsequent iteration, merge CurrentNew into Main, then NextNew becomes CurrentNew, repeat until all elements are valid.

Related

Jquery ajax, $.each wait to finish

I've been looking for the last couples days for a good solution for this problem. but can't seem to find one.
I have a Jquery script that has to loop through each element get a value and then do an Ajax call, however this is thousands of elements so i would like for the script to go element by element and only move to the next one once the previous action has finished, i tried asyc but that does not seem to wait properly and causes the page to freeze and miss a lot of elements and cause errors.
what advise/script would you suggest i try or look into?
I found a solution to my problem, but it's probably very specific, since i have unique ID's for each element what i did was:
first i defined an empty array and a variable for counting
on the initial click for each loop placing each SKU in an array, once done execute the ajax function
ajax function get's the index of current count then executes function
then i +1 the count and if it is less than the length of the array call ajax function again
i tried to explain it as open as possible in case someone has the same sort of situation, but you can always find a way to assign a random value to your element to somehow identify it

Replace 2nd part of list returned with function squared

As mentioned above, I want to replace the 2nd part of a list returned from a function with the 2nd part squared.
n[s]:= {1*s,2*s};
ReplacePart[n[s],2->?^2]
I need the question mark to equal the current value returned. What is the most concise way of doing this with or without ReplacePart?
Perhaps
n[s]:= {1*s,2*s};
n[s]/.{y_,z_}->{y,z^2}
which returns {s,4s^2}
You can also write that as
ReplaceAll[n[s],{y_,z_}->{y,z^2}]
If the list may or may not have more than two elements then
ReplaceAll[n[s],{y_,z_,x___}->{y,z^2,x}]
will maintain any additional elements unchanged

App Inventor TinyWebDB List Problems

I got a problem using the TinyWebDB in App Inventor 2. Here's a Screenshot of the blockcode.
The goal of this Screen is to store a list(array) of images and later query them with a button but my problem starts already earlier. First there is a variable initialized called fotoList and declared as an empty list.
When this Screen initializes (left block) I store the empty fotoList under the tag FotoListTag. Then if the image under the tag "SteckbriefFoto" is not in this list -> getValue with tag "FotoListTag". Then he jumps into the block on the right and adds the photo .. other stuff not important .. at the end I store the list again in the TinyWebDB (and also in the TinyDB) with the tag "FotoListTag". Then it goes back to the block on the left where at the end I want to set an image.picture to the photo I stored in variable fotoList.
When I compile the code there is an error opening the page that says
Select list item: List index too large
Select list item: Attempt to get item number 1 of a list of length 0:()
I just don't get the problem with this code and i hope someone can help me.
For lists, valueIfTagNotThere should be create empty list instead of an empty string
On first run of your app, TinyDB is empty, which means, for tag = FotoListTag you get no value back, therefore this should be an empty list in the beginning.
Later you are trying to select the first item from the list (zahl is 1). As you know, the list is empty in the beginning, so probably you should add an if statement to check, if the list is not empty and only then select the first item... same for tag = Schriftlist.
You also have a timing issue. in Screen.Initialize you are trying to get a value from TinyWebDB. This is an asynchronous call, you get the result back in TinyWebDB.GotResult event and this takes a little bit (let's say 500 milliseconds), but meanwhile the complete blocks of the Screen.Initialize event will be executed. Probably you are expecting, that meanwhile tag = FotolistTag is not empty anymore, but this is not the case.

Why can't I move unique_ptr out of multiset while compiler is happy to do it with vector? [duplicate]

I want to change the element in a set, so I used set<T>::iterator. However, the compiler argues "the element is const". Then I realized that set<T>::iterator is a const_iterator...
So, how I can change the element? Erase it and then insert a new one?
The elements of the set will be in sorted order. If you are allowed to modify an element, then this sorting order can not be maintained. Hence you can not modify the item. You need to erase the existing element and insert a new one.
Set elements are constant and may not be modified in place. Modification could change the ordering predicate without changing the position of the element which would violate the constraints of the data structure.
However now in the future (C++17), modifying the element without erasure is possible with the extract member function:
std::set<std::string> stringset{"a", "b", "c"};
auto node = stringset.extract(stringset.begin());
std::string& str = node.value();
str = "d";
stringset.insert(std::move(node));
There is still the cost of list operations but the object itself is not destroyed or copied.
EDIT: You cant add an element to a specific location in set. the set should be in the sorted order whatever operations you do. So you have to erase the specific element and insert the new element so that the ordering of set isnt lost.
Also read more about set!

Prototype $$ returns array, should return one element like $

When using the dollar-dollar-function in prototype I alway get an array of elements back, instead of just one element with the dollar-function. How can I combine the power of CSS-selectors of $$ but still get only one element back?
Changing the structure of the source is not possible, so I can't just select it with the id. It needs to get selected with CSS, but should just return one element.
You can also do
$$('.foo').first()
It looks cleaner than $$('.foo')[0] for my taste :)
It does not make sense to return a single element when selecting by class name because potentially there could be many elements in the DOM that have this class. So you could always use the first element of the returned array if you are sure that it will be unique.
$$('.foo')[0]

Resources