Comapring Two Arrays in Perl For Same Values - perl-module

Lets say i have two arrays
#array1 holds the below values
branches/test1/a.txt
branches/test2/b.txt
branches/test2/c.txt
and
#array2
branches/test1/a.txt
branches/test2/b.txt
branches/test2/c.txt
I need to Compare both the arrays and do something if it matches.
I find Array::Compare is has too much dependencies to be installed. Any alternative ways for this ?
Thanks

How do you want to compare two arrays?
In which case two arrays are equal:
1. Elements in both arrays are same
2. Elements in both arrays are same and there order is also same

Related

How to join two already sorted arrays into one sorted array in M$ Flow efficiently

Microsoft Flow doesn't support any sort function for arrays or lists.
For my problems I can use sort function within ODATA request to have some data presorted by the databases I'm accessing. In my case, I want to have a list of all start and end dates from a sharepoint calendar in a single array.
I can pull all dates sorted by the start date and I can pull all dates sorted by the end date into separate arrays. Now I have two sorted arrays which I want to join into a single array.
There are very few possibilites in iterating over an array. But the task has some properties which could ease the problem.
Two arrays,
both presorted by the same property as the desired final arrays.
same size.
Perhaps I'm missing some feature of the ODATA-request or there's a simple workaround. I'd prefer not to use REST-api or messing around with the JSON or manually, but if there's really an elegant solution I won't reject it.
I have a solution, but I don't think it is a good one.
Prerequesites are the two already sorted arrays and two additional arrays.
Let's call the two sorted arrays I have extracted from the sharepoint list array A and B.
And let's call the additional arrays array S1 and S2.
Then I set up a foreach-loop on array B.
Within that loop I filter array A for all elements lesser or equal to the current item of array B.
The output of the filter operation is saved to array S1.
current item of array B is appendet to array S1.
Again filter Array A for all elements, but this time for greater than the current item of array B.
save the output of the filter operation to array S2.
make a union from S1 and S2.
save the output of the union expression to array A.
As every element of array A has to be copied n times for a n-element array, the effort for processing two arrays of n elements is not quite optimal, especially if you consider both arrays already sorted in advance.
n² comparisons
2n²+n copy operations (not taking into account the imperfections of the implementation of flow)
If I'd implement a complete sort from scratch it would perform better, I think, but I also think, there must be other means to join two presorted arrays of compatible content.

Best Logic to implement comparison of two arrays

I have two arrays -arr 1 and arr 2, here both the arrays will have some common items and many uncommon items, first the common items should be removed from both the arrays.
therefore for each uncommon item in arr 1 may probably be a sum of two or more values in arr 2 or vice versa.if the sum is found the values must be removed from the respective arrays. Finally the output should only be the unmatched values on both the arrays   
I need a logic where i can do this calculation in much faster way.
I'm not going to give out the code that implements your logic but rather I would be happy to point you in the right direction.
I code in C++, so I'm gonna answer with respect to that. If you want a different language, I'm sure you can freely do so.
To remove the common elements:
You first sort the arrays arr1 and arr2. Then do a set_symmetric_difference on them. This will effectively run in linear time. Then create two sets out of them, say set1 and set2.
To remove pairs that sum up to an element in the other array
For this, you need to loop through all the possible pairs of elements in arr1 and check if this sum of this pair exists in set2. Likewise do for arr2 as well and remove the element when necessary.
This can be done in O(n2). If you don't want to create the extra sets, you can always trade performance for memory, by just looping through the pairs of arr1 and checking for the sum in the arr2 by doing a binary search.
Then the time complexity may shoot up to O(n2 log(n)).

algorithm to accomplish comparing two arrays with user define criteria

I want to compare tow float arrays' value. But it may be different from other criteria. Here is how I define which array is the best.
Say we have two array named a,b.First, we compare the max value of these two array, and the array with smaller max value wins. If they have same value, then we can divide each array into two parts. The first part is a[1:max_loc(a)-1] and a[max_loc(a)+1,len(a)], and b is similar. Then we use the same criteria on a[1:max_loc(a)-1] and b[1:max_loc(b)-1] to see which array has the smaller max value. If they have the same max value on these intervals, then divide them to smaller arrays and do the same comparison. We also do the same thing for the a[max_loc(a)+1,len(a)] and b[max_loc(b)+1,len(b)]. Until we find smaller max value on the same intervals, the program end and print out the best array.
What's the algorithm to fulfill this comparison?
P.S. these two arrays may have different length.
Most of the time, what you search is somewhere already on the Internet :
https://www.ics.uci.edu/~eppstein/161/960118.html
Here you got 2 examples with full explanations which follows the divide and conquer idea (MergeSort and QuickSort)

Aligning overlap of ordered and unordered lists

I'm looking for an algorithm that can find/assign order and overlap given a list of ordered elements and a list of unordered elements. (of which overlap might or might not exist).
For this example I'll use the integers but they could just as well be peoples names, ID codes etc. IE the number can't be used to solve the real problem but to help explain the problem I used the ordered set (1,2,3,4,5,6,7,8,9,10) as the holy grail answer.
Input:
Ordered List of Lists: (1,2,3,4), (8,9,10), (3,4,5)
UnOrdered List of Lists: (3,4,2), (6,4,5,7), (10,9)
Thought process in how I do this algorithm in my head:
list 3,4,5 and 1,2,3,4 are ordered and have 3,4 in common therefore the 2 ordered lists overlap to form: 1,2,3,4,5 in that order.
The unordered list 3,4,2 is a subset of ordered list 1,2,3,4,5 therefor it could be reordered as 2,3,4 and said to overlap the ordered list 1,2,3,4,5
Same idea (as step 2) for the ordered list 8,9,10 when compared with the unordered 10,9. It should be 9,10 overlapped with 8,9,10.
Now comparing ordered list 1,2,3,4,5 and the unordered 6,4,5,7 they have an intersection set of 4,5 so you could conclude that its 1,2,3,4,5,(6,7|7,6) where (6,7|7,6) means that its either a 6 followed by a 7 or a 7 followed by a 6 (but its unknown which is correct)
Output:
I would like to beable to parse a matrix/tree/whatever kind of data structure to see what overlapped where and in what order
and a summarized list containing sets of partially known order
set1: 1,2,3,4,5,(6,7|7,6)
set2: 2: 8,9,10
Does anyone know of a similar problem or algorithm I could use? Ideally it would be in Perl but pseudo code or algorithms from another language would be fine.
Thanks
If I understand this right, you need one ordered list from a set of ordered and unordered lists. A possible solution would be to iterate over all the values in all the sets and add them to a hash table structure. In implementation terms that could be a c++ map, java hashmap, python dictionary etc. That would look like:
for i over all sets S //(Ordered and
Unordered)
for j over all values in S[i]
H.insert(S[i][j]) //H is the hash table
Now iterate over the hash table entries to get the required ordered list. This is quite practical and optimal.
A not-so-practical-solution but worth mentioning for its niceness is this:
Assign every unique number a corresponding prime number. For example, in your example case, map the numbers as follows:
p[1] = 2, p[2]=3, p[3]=5, p[4]=7, p[5]=11, p[6]= 13, p[7]=17, p[8]=19, p[9]=23, p[10]=29;
Now, each set Si can be represented by a value Vi- the product of the corresponding primes. So a set Si=(1,2,3) (or for that matter (2,1,3)) would have value Vi=p[1]*p[2]*p[3]
Find the LCM of all the Vi 's. Call this V.
V=For all i LCM{Vi}
Factorise V into its prime factors. Each prime number represents an element in your final ordered list.
This second solution is neat but breaks down for practical purposes because we enter bignum space very quickly.
Hope at least one of these work for you!

randomize NSArray

I need to randomize an NSArray that I have loaded with quiz questions, but I need to keep the elements in a specific order.
NSArray is an immutable object, meaning you can't change the order of elements. I think i get what you are getting at though. Just write some functionality overtop of your datastructure to choose elements of this array at random. You will need to keep track of what elements have been accessed already so you don't re-select them.
You could also make a copy of your array using the subtype NSMutableArray. Once a question has been selected, remove it from this array (you can do this because the array is now mutable)
You use a C function for random numbers. See rand() and srand()
If I understand the question correctly: you have an array of questions, you want to show a subset (of a presumably fixed size?) of them, but the subset needs to preserve the order as in the original array?
Let's say you have N questions and want to randomly pick M of them. You could create an array of elements [0 .. (N - 1)] that stores indices to the original array. You then could shuffle this array using the Knuth/Fisher-Yate's algorithm, sort the first M elements, and use those first M indices to do lookups into the original array.

Resources