![]() |
Programming Exercise #1
So, there is a lot of help around here dealing with syntax of scripting, but I don't see much discussion on best algorithms. Be this the case, I am going to every so often (when I feel like it) post some mathematically cool problem for you all to help each other solve and break some mental barriers.
Problem 1. Given 2 arrays of numbers, find all the sets of 3 numbers that both arrays have in common. Remember, the goal is to make this as efficient as possible with graal script. Edit: For clarification, this is a sample If you have the following two sets {2,3,5,6,8,10} {1,5,3,10,6,9,2} All of the sets of 3 would be {2,3,5} {2,3,6} {2,3,10} {2,5,6} {2,5,10} {2,6,10} {3,5,6} {3,5,10} {3,6,10} {5,6,10} New Edit: Let's do unique sets. If you have two sets of {1,1,2,3}, then the answer is {1,2,3}, not {1,2,3} {1,2,3} |
I have something I put together real quick, but it searches linearly, so someone could most certainly come up with something more efficient.
Also, I assumed that if both arrays are (for example): {1, 2, 3, 4, 5, 6} Then they would just have the two sets {1, 2, 3} and {4, 5, 6}. Instead of {1, 2, 3}, {2, 3, 4}, {3, 4, 5}, {4, 5, 6}. Also, there is a high chance that there is a case where the algorithm fails, as I haven't really tested it... so feel free to find a case that doesn't work :P. PHP Code:
|
interesting *waits for novo
|
I get the feeling I'm being manipulated, but...
PHP Code:
|
Hmm I never coded gscript, but whats about quicksorting both sets and find the common ones, then?
|
I never knew Googi could script. :)
|
What Googi posted doesn't make logical sense, at all. :p
|
PHP Code:
That doesn't check if all the sets are exactly different, I'll do that next I guess. You need to give examples of input and output >:( PHP Code:
|
Posted a sample to clarify for you tards.
|
PHP Code:
PHP Code:
|
Quote:
PHP Code:
|
Quote:
|
Quote:
Someone agrees with me. :3 |
Zero, that is incredibly inefficient. We want something better then the naive algorithm
|
Quote:
Quote:
|
Quote:
Quote:
|
Quote:
|
Quote:
|
Quote:
PHP Code:
Quote:
|
Quote:
|
PHP Code:
|
Quote:
Also, in your big O, did you include the search time for the "in" operator in your intersect portion? I can see a few ways to optimize that. |
Quote:
I had originally intended to do something along these lines: PHP Code:
PHP Code:
It just slipped my mind. Silly me. Also, first thing should be if (arrA.size() > 2 && arrB.size() > 2)... As for the naive algorithm, It has O(N2^3*N1!/((N1-3)!*3!)) (?), I think. |
Quote:
|
PHP Code:
|
Quote:
e: nice ninja edit. |
Using my leet skillz I have created the ultimate:
PHP Code:
|
Rather long, don't you think? XD
But I don't even have the competence to try. :( |
Quote:
Anyway, hashes would optimize finding the intersect, but graal has no support for it. However, we can dynamically name variables, so we can emulate it well enough for this situation. code snippet: PHP Code:
Also, it would be slightly faster to make an array with the max possible size first, so you're not resizing the array (and thus possibly moving it to a new location in memory constantly.) PHP Code:
I'll optimize finding the sets from the intersects later. Lets let this soak in first. |
The maximum size of the intersect should be the size of the small array as it cannot possibly have more elements in common than there are in the set.
|
Quote:
I actually read my post before yours and was about to correct that (does so) |
Also, the method you proposed for determining the intersection gives duplicates.
|
Quote:
PHP Code:
|
Quote:
PHP Code:
|
Quote:
However, im not sure which would work faster. when i get home and have access to my rc ill find out by averaging the run times :P creating temp.c set means creating and moving a lot of pointers. it might be faster to assign the intersects directly to the array instead of getting dynamic variable names because of it. Ill report back tonight |
whats the use in this? I wouldn't use this in a script... lol
Make a exercise that'll come in handy !!!! |
Quote:
|
why if you're not intending to use it? i'm asking for you to base your exercises on something that'll come in handy
|
Quote:
|
Quote:
|
All times are GMT +2. The time now is 06:55 PM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.