I am using
$where->equalTo('dailyshifthr.thrs', 0);
$where->equalTo('dailyshifthr.thrsA', 0);
I need these two condition in OR not AND condition. Help me.
You should use the or predicate, like:
$where->or->equalTo('dailyshifthr.thrs', 0);
$where->or->equalTo('dailyshifthr.thrsA', 0);
If you want to nest these two conditions with others, like
SELECT *
from your_table
where field_a = 1
and (dailyshifthr.thrs = 0 or dailyshifthr.thrsA = 0)
Then you must use the nest predicate:
$where->equalTo('field_a', 1);
$nest = $where->nest;
$nest->or->equalTo('dailyshifthr.thrs', 0);
$nest->or->equalTo('dailyshifthr.thrsA', 0);
Related
So I have arr = randi([0,20],20,1). I want to show: If there are numbers less than 5, fprintf('Yes\n') only once. Im using a for loop (for i = 1 : length(arr)) and indexing it.
As your description, maybe you need if statement within for loop like below
for i = 1:length(arr)
if arr(i) < 5
fprintf('Yes\n');
break
end
end
If you want to print Yes once, you can try
if any(arr < 5)
fprintf('Yes\n')
endif
If you don't want to use break, the code below might be an option
for i = 1:min(find(arr <5))
if (arr(i) < 5)
fprintf('Yes\n');
end
end
You can use a break statement upon finding the first value under 5 and printing the Yes statement.
Using a break Statement:
arr = randi([0,20],20,1);
for i = 1: length(arr)
if arr(i) < 5
fprintf("Yes\n");
break;
end
end
Extension:
By Using any() Function:
Alternatively, if you'd like to concise it down without the need for a for-loop the any() function can be used to determine if any values within the array meet a condition in this case arr < 5.
arr = randi([0,20],20,1);
if(any(arr < 5))
fprintf("Yes\n");
end
By Using a While Loop:
Check = 0;
arr = randi([0,20],20,1);
i = 1;
while (Check == 0 && i < length(arr))
if arr(i) < 5
fprintf("Yes\n");
Check = 1;
end
i = i + 1;
end
I'm trying to simplify this task to a one-line command, but I just cannot figure out how it should be done. I tried everything that came to my mind without success. My code right now is:
A_temp = A;
A_temp(A_temp > 3) = A_temp(A_temp > 3) + 1;
A_final = A_temp;
A_final(:,end+1) = 1;
This will achieve the same result as your above code:
A_final = [A+(A > 3) ones(size(A, 2), 1)];
Consider the following problem:
We are given two arrays A and B such that A and B are sorted
except A has B.length additional 0s appended to its end. For instance, A and B could be the following:
A = [2, 4, 6, 7, 0, 0, 0]
B = [1, 7, 9]
Our goal is to create one sorted list by inserting each entry of B
into A in place. For instance, running the algorithm on the above
example would leave
A = [1, 2, 4, 6, 7, 7, 9]
Is there a clever way to do this in better than O(n^2) time? The only way I could think of is to insert each element of B into A by scanning linearly and performing the appropriate number of shifts, but this leads to the O(n^2) solution.
Some pseudo-code (sorta C-ish), assuming array indexing is 0-based:
pA = A + len(A) - 1;
pC = pA; // last element in A
while (! *pA) --pA; // find the last non-zero entry in A
pB = B + len(B) - 1;
while (pA >= A) && (pB >= B)
if *pA > *pB
*pC = *pA; --pA;
else
*pC = *pB; --pB;
--pC
while (pB >= B) // still some bits in B to copy over
*pC = *pB; --pB; --pC;
Not really tested, and just written off the top of my head, but it should give you the idea... May not have the termination and boundary conditions exactly right.
You can do it in O(n).
Work from the end, moving the largest element towards the end of A. This way you avoid a lot of trouble to do with where to keep the elements while iterating. This is pretty easy to implement:
int indexA = A.Length - B.Length - 1;
int indexB = B.Length - 1;
int insertAt = A.Length;
while (indexA > 0 || indexB > 0)
{
insertAt--;
A[insertAt] = max(B[indexB], A[indexA]);
if (A[indexA] <= B[indexB])
indexB--;
else
indexA--;
}
I am quite new to coffeescript and I am wondering if any more experienced users could point suggest a refactoring for the following code:
splitCollection: =>
maxLength = Math.ceil(#collection.length / 3)
sets = Math.ceil(#collection.length / maxLength)
start = 0
for x in [1..sets]
if x != sets
#render new BusinessUnits(#collection.models.slice(start, (maxLength + start)))
else
#render new BusinessUnits(#collection.models.slice(start, (#collection.length)))
start+= maxLength
There does not appear to be a while loop in coffeescript which seems to suggest a better mechanism.
Any suggestions appreciated.
Looks like you are using Backbone.js, which includes Underscore.js, which has the groupBy function.
You could create a "bucketNumber" function:
bucketNumber = (value, index) ->
Math.floor( index / #collection.length * 3 )
Then group your collection:
sets = #collection.groupBy bucketNumber
Now, assuming ten items, sets should look something like this:
{0: [{}, {}, {}], 1: [{}, {}, {}], 2: [{}, {}, {}, {}]}
From here, it becomes rather straight-forward
for bucketNumber, bucket of sets
#render new BusinessUnits( bucket )
Here is a jsFiddle showing it in action
You don't need to keep track of your position twice, x is enough:
splitCollection: =>
setSize = Math.ceil #collection.length / 3
sets = Math.ceil #collection.length / maxLength
for x in [1..sets]
#render new BusinessUnits #collection.models[x * setSize...(x+1) * setSize]
Note that there is nothing wrong with passing slice an end greater than the array length.
If I understand your code, you want to split an array in 3 parts (the last one can have less items). In this case write the reusable abstraction for the task. Using underscore:
splitCollection: =>
group_size = Math.ceil(#collection.size() / 3)
_.each _(#collection.models).inGroupsOf(group_size), (group) ->
#render(new BusinessUnits(group))
_.inGroupsOf can be written:
_.mixin({
inGroupsOf: function(array, n) {
var output = [];
for(var index=0; index < array.length; index += n) {
output.push(array.slice(index, index+n));
}
return output;
}
});
I want to get count of a set based on different condition:
var invoices = new AccountingEntities().Transactions
var c1 = invoices.Count(i=>i.Type = 0);
var c2 = invoices.Count(i=>i.Type = 1);
var c3 = invoices.Count(i=>i.Type = 2);
How its possible to call all three queries in one DB round trip to increase performance?
Sure, just wrap up your three counts in a POCO or anonymous type:
using (var invoices = new AccountingEntities())
{
var c = (from i in invoices.Transactions
select new
{
c1 = invoices.Count(i=>i.Type = 0),
c2 = invoices.Count(i=>i.Type = 1),
c3 = invoices.Count(i=>i.Type = 2)
}).Single();
}
Also, dispose your context, as I show.
To aggregate arbitrary subqueries, use a dummy single-row result set from which you nest the desired subqueries. Assuming db represents your DbContext, the code to count invoice types will look like this:
var counts = (
from unused in db.Invoices
select new {
Count1 = db.Invoices.Count(i => i.Type == 0),
Count2 = db.Invoices.Count(i => i.Type == 1),
Count3 = db.Invoices.Count(i => i.Type == 2)
}).First();
If the want to generically get a count of all types, use grouping:
var counts =
from i in db.Invoices
group i by i.Type into g
select new { Type = g.Key, Count = g.Count() };