I have an array which is used to compute a score in a game:
a = {1,7,5,1,2,6,2,3,4,5,5,6,7,7,7}
All numbers should be simply added, except when a number appears several times, for instance 7 (which appears 4 times) it should be added as such:
1*7 + 2*7 + 3*7 + 4*7
So, altogether, array "a" should give this score:
score = (1*1 + 2*1) + (1*2 + 2*2) + (1*3) + (1*4) + (1*5 + 2*5 + 3*5) + (1*6 + 2*6) + (1*7 + 2*7 + 3*7 + 4*7)
I wouldn't know where to start doing this. What's a good method for this kind of calculation?
Any help is appreciated.
You can keep track of the current multiplier for each number in another table:
function calculateScore(a)
local multipliers = {}
local score = 0
for i,number in ipairs(a) do
local multiplier = multipliers[number] or 1
multipliers[number] = multiplier + 1
score = score + number * multiplier
end
return score
end
local a = {1,7,5,1,2,6,2,3,4,5,5,6,7,7,7}
local score = calculateScore(a)
Related
I've a function that I'm trying to analyze which its output is 7:
Given this block of code:
int func_1(struct node* node)
{
if (node == NULL)
return 0;
else
return func_1(node->left) + 1 + func_1(node->right);
}
And this Binary Search Tree:
The return value is 7.
I know recursion and it's kinda simple here, I tried to follow up and I can not understand how it returned 7. I calculated that it just goes left, left, then one time right, and that's it. which will return 3. And even if it goes 3 times right, after the root, it will still return 6 and not 7.
Can you guys help me out please?
Semantically it takes the number of left nodes + 1 (the current node) + the number of right nodes.
with func_1(x) I mean calling the function on that specific node.
So the complete calculation is
func_1(8) + 1 + func_1(14)
(func_1(7) + 1 + func_1(9)) + 1 + func_1(14)
(1 + 1 + 1) + 1 + (0 + 1 + func_1(17)
3 + 1 + (0 + 1 + (0 + 1 + func_1(18))
3 + 1 + (1 + (1 + (0 + 1 + 0)
results in 7
This principle is used very often in recursion:
first do the calculation for the 'current' item (the current node), in this case the number of nodes for the 'node itself' is 1.
than add the calculation of the other items in a recursive way, in this case the number of nodes left of the current node, and the number of nodes right of the current node. For ordering reasons in this case the +1 for the current node (number of nodes) is put in the middle.
Take a look at the leaf node 7.
When func_1 is called with the value of the node 7, the if statement will branch into the else part, since the pointer to this node is valid.
Then func_1 will be called twice once for the left child and once for the right child. In both cases the functions return 0, since left and right child are NULL. The function will return 1:
return func_1(node->left) + 1 + func_1(node->right);
equivalent to:
return func_1(NULL) + 1 + func_1(NULL);
becomes:
return 0 + 1 + 0;
Look at this statement
return func_1(node->left) + 1 + func_1(node->right);
^^^^^
if a node is not equal to NULL it counts itself plus the number of nodes in the left and right sub trees relative to this node.
So you will get a result that is equal to the number of nodes that are not equal to NULL.
First time posting so forgive me if this has been asked before.
I am not new to writing SQL but I am also no expert.
I need a better way to write this:
CASE
WHEN OrderQuantity + Week1Quantity >= CriteriaAmount THEN 1
WHEN OrderQuantity + Week1Quantity + Week2Quantity >= CriteriaAmount THEN 2
WHEN OrderQuantity + Week1Quantity + Week2Quantity + Week3Quantity >= CriteriaAmount THEN 3
WHEN OrderQuantity + Week1Quantity + Week2Quantity + Week3Quantity + Week4Quantity >= CriteriaAmount THEN 4
WHEN OrderQuantity + Week1Quantity + Week2Quantity + Week3Quantity + Week4Quantity + Week5Quantity >= CriteriaAmount THEN 5
WHEN OrderQuantity + Week1Quantity + Week2Quantity + Week3Quantity + Week4Quantity + Week5Quantity + Week6Quantity >= CriteriaAmount THEN 6
WHEN OrderQuantity + Week1Quantity + Week2Quantity + Week3Quantity + Week4Quantity + Week5Quantity + Week6Quantity + Week7Quantity > CriteriaAmount THEN 7
WHEN OrderQuantity + Week1Quantity + Week2Quantity + Week3Quantity + Week4Quantity + Week5Quantity + Week6Quantity + Week7Quantity + Week8Quantity >= CriteriaAmount THEN 8
ELSE 999
END
It's purpose is to find the first stage at which the target is hit, using numeric values from each column as a running total.
Table like so:
Order QuantityWeek 1Week 2Week 3(Suggested Target)
.........5.................5...........5...........5..................15.............
........10...............10.........10.........10.................500...........
In the above table row 1 will give an answer of 2 as there are 2 weeks to add to reach the target.
Row 2 will land on the ELSE statement giving 999 which will later be converted to an error.
I would like a better way to write this as I have 52 more columns to get through you can imagine this is a white-space-hog!
Thanks,
Adam
The design of table is very poor. After a few years it will have hundreds of columns. The only way to deal with this, if you cannot change the table design, is to use dynamic sql with exec.
The sane, and maintainable, approach is to change the table design to have one row for each item number, week number and item amount. Then you can use standard TSQL verbs such as sum and group by.
I have two functions written that have simple assignment statements with very simple expressions. The expressions are the same for both functions, however, they involve different variable types: One function uses an array of structs, the other just uses a typedef'd struct.
When running the functions, the second function fails to divide by 256, and I get very high values that are not "normalized". I have to uncomment the second line in the second function (valueB = valueB / 256) to get it to work.
The first function, however, works perfectly.
Heres the statement in Function One:
value = ((p[0].value * p2Area)+(p[1].value * p3Area)+(p[2].value * p0Area)+(p[3].value * p1Area) / 256);
Heres the statement in Function Two:
valueB = ((dataPoints.p0B * p2Area)+(dataPoints.p1B * p3Area)+(dataPoints.p2B * p0Area)+(dataPoints.p3B * p1Area) / 256);
//valueB = valueB / 256;
Why would this happen?
Also, I pass the functions the same numbers and it doesn't seem to help.
This is on MacOSX 10.6.8, inside Xcode 3.2.6
Are you absolutely sure the first one works properly? You have
value = ((p[0].value * p2Area)+(p[1].value * p3Area)+(p[2].value * p0Area)+(p[3].value * p1Area) / 256);
I think you want:
value = (((p[0].value * p2Area)+(p[1].value * p3Area)+(p[2].value * p0Area)+(p[3].value * p1Area)) / 256);
Similar thing with the second. I think it should be:
value = (((p[0].value * p2Area)+(p[1].value * p3Area)+(p[2].value * p0Area)+(p[3].value * p1Area)) / 256);
In both cases I think you want to divide the sum of the products by 256. Not just the last one. My change only involves placing an extra set of parentheses around the sum of the product subexpressions and dividing the entire thing by 256
In all languages there is an order by which mathematical (and all other operators are completed). It just so happens that * and / are higher in precedence than + and - in C/C++ You may refer to this link for more details.
To simplify what happened to you, I will create this simple equation:
2 + 4 + 6 + 4 / 2
Since division occurs first (and there are no parentheses to alter the order) it gets computed as:
2 + 4 + 6 + (4 / 2) = 14
Not:
(2 + 4 + 6 + 4) / 2 = 8
So my change to your code was the same as putting parentheses around 2 + 4 + 6 + 4 / 2 giving (2 + 4 + 6 + 4) / 2 and forcing the division to be done last after all the additions are completed.
I am trying to create a column that is the total value of column A,B,C,D,E.
select,[TVIncome] a
,[XIncome] b
,[ZIncome] c
,[DINCOME] d
,[OIncome] e
sum(a,b,c,d,e) as total
The error I get when doing the above sum is :"The sum function requires 1 argument(s)."
The total above does not work. Also, if I just include the the proper names, XIncome and the rest,it still does not work. How do I do it?
SUM() adds each value in a column, to give a single value
+ adds each value in two columns together to give a column
It sounds like you are after a + b + c + d + e AS total,
or possibly SUM(a + b + c + d + e) AS total if you are after 1 value
Assuming you want a sum for those columns for each row
SELECT (TVIncome + XIncome + ZIncome + DIncome + OIncome) as TotalIncome
FROM Table
If you want a sum of sums, which will equal one row:
SELECT SUM(TVIncome + XIncome + ZIncome + DIncome + OIncome) as TotalIncome
FROM Table
Please find the code below which shows some operations based on recursion.I would love some one to please explain me how this recursion works?
#include <stdio.h>
int func(int);
main()
{
int ret = 0;
ret = func(6);
printf("The val is %d\n",ret);
}
int func(int m)
{
if((m==0)||(m==1))
{
return 1;
}
else
{
return (func(m-1)+func(m-2));
}
}
When executed,the value of val is 13.Please someone explain how does this unwind operations happens in stack
You don't need to involve a stack or any unwinding (excuse me for involving myself, though).
Just substitute the call with the content of the function, and keep doing that until you no longer recurse:
ret = func(6) =
func(5) + func(4) =
func(4) + func(3) + func(3) + func(2) =
func(2) + func(3) + func(1) + func(2) + func(1) + func(2) + func(0) + func(1) =
func(0) + func(1) + func(1) + func(2) + 1 + func(0) + func(1) + 1 + func(1) + func(0) + 1 + 1 =
1 + 1 + 1 + func(0) + func(1) + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 =
3 + 1 + 1 + 8 =
3 + 2 + 8 =
13
It's a bit difficult with the typography, but that's what happens and the answer seems to match what you got, too.
Recursion is nothing more than calling the a function from within that particular function. A lot of mathematical algorithms or (tree) search algorithms use this technique for their desired result.
Recursive function calls need to 'escape' their repeating 'self calling' otherwise the application would become unresponsive. In your example, this is done by the if((m==0)||(m==1)) check. If the check is true, the function just returns 1 (and escapes the recursion).
The recursive code you showed calculates the Fibonacci sequence, which is a typical recursive algorithm, as it requires the values of 2 previous calculations. Step 0 and 1 return 1. These 2 values are added for step 2 (resulting in 1+1=2). The next step results in 1+2=3. And so on. As you see this can only be calculated from the start (and thus requires the recursion to do so)
Your program tries to print the nth(or n+1th) number of a Fibonacci series. Here the base case is when m =1 or m=0
The worst thing about recursion here is a value is calculated twice for example func(4), func(3) and func(2) as evident from here.