|
Hồng Chấn Phát wrote: I have been developer for 5 years, I am not a student, so it's not homework ... Can you show what you have done ?
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
I don't know if there is a general solution, but using swaps I have come up with:
N = 2 requires 0 swaps (obviously)
N = 4 requires 1 swap
N = 6 requires 3 swaps
N = 4 requires 4 swaps
N = 10 requires 6 swaps
...
Basically, moving the N/2 first elements of the pairs to the beginning requires N/2-1 swaps. The N/2 second elements are "nearly" in order, requiring only a few additional swaps.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
using System.Linq;
private int[] ints = new int[]{0,1,2,3,4,5,6,7,8,9,10};
int[] sints = ints.Where(val => (val%2 == 0)).Concat(ints.Where(val => (val%2 == 1))).ToArray(); I'm not claiming that is optimal in terms of speed of code execution; but it's faster in terms of my valuable time writing code !
If I could not use Linq:
private string[] strs = new string[]{"a1","b1","a2","b2","a3","b3","a4","b4"};
private string firstOddValue = strs[1];
List<string> strList = new List<string>{strs[0]};
for (int i = 1; i < strs.Length; i++)
{
if (i%2 == 1)
{
strList.Add(strs[i]);
}
else
{
strList.Insert(strList.IndexOf(firstOddValue), strs[i]);
}
}
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
Hi
I have a dataset and I want to perform Association Rules on it.
In other for it to predict accurately, the algorithm should have the ability to look at each attribute and decipher their association which mostly is a result of activities between each attribute in the form of multiplication, addition, subtraction, re-appearance of common patterns etc
Common association algorithms follow a basic pattern deciphering method...'comparing the re-occurrence of item sets'....
The association algorithm I have in mind should have a dynamic god capability....it should be able to determine the relationship between the attributes by going deeper such as finding out if the multiplication of the data 'value' of an instance in one attribute with another data value of an instance in another attribute has a higher rate of predictive accuracy....my workflow is in WEKA and so a compatible algorithm is needed.
I will love to interact with any coder who believes such a task can be accomplished.
cheers
|
|
|
|
|
Have you done something or it is just an idea ?
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
Its an idea for mining out patterns that is evident in football data.
|
|
|
|
|
Its an idea for mining out patterns that is evident in football data.
|
|
|
|
|
Its an idea for mining out patterns that is evident in football data.
|
|
|
|
|
How do I solve the recurrence T(n) = T (n - 1) + c substitution method?
|
|
|
|
|
Looks like a partial definition of a linear suite.
What do you want to solve ?
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
I have thought of an algorithm that would really aide the work I am doing but cannot think of a name for it to search if there is an online algorithm created for it that I could use.
Algorithm example + explanation: For the work I am doing I have lots of different amounts of money and a total figure. I know that the sum of some of these amounts will equal the total figure but I have no clue which amounts they are and am not too sure how many of these figures would reach the total. I would input the amounts of money and the total figure and the algorithm would have to evaluate every single combination of these amounts of money until it reached the total amount and would output which amounts of money were used to create that total.
I am not asking anyone to create this algorithm I am just asking whether there is a website with this algorithm on it that I could use or if this algorithm is really easy to create + therefore I could create it for myself.
Any comments would be helpful + please ask if you have any queries for clarity! Many thanks!
|
|
|
|
|
this is the "coin change problem".
but, it's not an exceptionally hard problem. and it's probably worth trying to think it through on your own.
|
|
|
|
|
You have a set of data, and you search which subset will match a criteria.
This is a classical problem, I am not sure there is a name to this problem, or as mentioned in previous answer, it is related to the coin change problem.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
Hi.
I was recently interested in literature regarding what a good hash table load factor is for rehashing. Wikipedia mentions 0,7, 0,75 and 2/3, but with no explanation as to why performance degrades at these numbers.
The problem intrigued me, and I came to the conclusion that, mathematically speaking, the load factor threshold is at exactly log 2 (~0,7), which I found rather beautiful in it's own way.
I am posting this in case somebody else finds it interesting as I could not find any reference to a similar solution. Please also reply if you find any errors.
---------------------------------------
Chaining can be avoided and branch prediction exploited by predicting if a bucket is empty or not. A bucket is probably empty if the probability of it being empty exceeds 0,5.
Let s represent the size and n the number of keys added. Using the binomial theorem, the probability of a bucket being empty is:
P(0) = C(n, 0) * (1/s)^0 * (1 - 1/s)^(n - 0).
Thus, a bucket is probably empty if there are less than
log(2)/log(s/(s - 1)) keys.
As s reaches infinity and if the number of keys added is such that P(0) = 0,5, then n/s approaches log(2) rapidly:
lim (log(2)/log(s/(s - 1)))/s as s -> infinity = log(2) ~ 0,7.
|
|
|
|
|
|
Split the larger triangle into two right-angled triangles, and apply Pythagoras:
<br />
L1² = x² + y²<br />
L2² = (X1 - x)² + y² = X1² - 2·X1·x + x² + y²<br />
<br />
L2² - L1² = X1² - 2·X1·x<br />
2·X1·x = X1² - L2² + L1²<br />
<br />
x = (X1² - L2² + L1²) / 2·X1<br />
<br />
y = ± √(L1² - x²)<br />
NB: there will be two solutions - one with a positive value for y , and one with a negative value.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Use Pythagoras.
Let Pen tip = P. We know L1, L2, X1.
Define line segments R1 -> P = L1, R1 -> x = A, (x,0) -> P = B.
A2 + B2 = L12
We know the length of A is X, since it's (0,0) -> (x,0), and we know length of B is y, since it's (x,0) to (x,y)
x2 + y2 = L12
Let R2 -> P = L2, x -> R2 = C
B2 + C2 = L22
A + C = X1 => C = X1 - A
So we have
x2 + y2 = L12
y2 + (X1 - x)2 = L22
Solve these and you get x and y
cheers
Chris Maunder
|
|
|
|
|
Im inputing three numbers of users choice
number 1
number 2
final number
what i want to find out is , how many combinations of number 1 and number 2 would result in final number = number1 *x + number2*y = final number.
I am using nested loops
int i;
int j;
long long int pocet=0;
long long int one;
long long int two;
long long int last;
long long int one_max;
long long int two_max;
long long int add=1;
long long int reduc=1;
if((one%2==0) && (two%2==0)){
if(one>two){
add=two/2;
reduc=one/2;
}
}
one_max=last/one;
two_max=last/two;
for(i=0;i<=one_max;i+=add){
for(j=two_max;j>=0;j-=reduc){
long long int tmp_one=(one*i);
long long int tmp_two=(two*j);
if(tmp_one+tmp_two==last){
two_max=j;
pocet++;
}
}
}
if i input these numbers
one = 10;
two = 13;
final =100;
the only possible combination of first two numbers to result in final number is
10 * 10 + 13 * 0
modified 7-Nov-15 11:37am.
|
|
|
|
|
First of all, I fear your code don't work or is not complete.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
this is not full code , its just half pseudo code for illustrating my problem , i can provide full code if it helps
|
|
|
|
|
If it's pseudo code, ho wdo you know it runs slow?
|
|
|
|
|
i have updated my question , i wrote actual code which does works slow
|
|
|
|
|
You should consider this
long long int MyFunc (long long int one, long long int two, long long int last) {
int i;
int j;
long long int pocet=0; long long int one_max;
long long int two_max;
long long int add=1; long long int reduc=1;
if((one%2==0) && (two%2==0)){
if(one>two){
add=two/2;
reduc=one/2;
}
}
one_max=last/one;
two_max=last/two;
for(i=0;i<=one_max;i+=add){
for(j=two_max;j>=0;j-=reduc){
long long int tmp_one=(one*i);
long long int tmp_two=(two*j);
if(tmp_one+tmp_two==last){
two_max=j;
pocet++;
}
}
}
return pocet;
}
as the source code to provide. Because we can't use your source code as is. In the form of a function, we can do test without changing a single line in this code.
First remark: using int for i and j may be short and should be replaced with long long int to match other variables.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
Here is code with first batch of changes. the code is changed to a function for practical reasons.
long long int MyFunc (long long int one, long long int two, long long int last) {
long long int i;
long long int j;
long long int pocet=0;
for(i=0;i*one <=last;i++){
if ((last - one*i )% two == 0) { j= (last - one*i) / two;
pocet++;
}
}
return pocet;
}
I just stick to the problem here.
I just take advantage of mathematics to change the equation as i is known. This should already improve the runtime.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
thanks for reply , i have also improved my code , but both , my and yours fail to be fast and calculate the number of combinatiosn when big numbers such as
5098109
25279297
36821491225502191
are inputed
|
|
|
|