My question is the same as Algorithm to detect overlapping periods.
But in my case, a period can be unbouded (No End Date, ie. NULL).
I can't find an elegant way do to that.
For unbounded end dates, you could also do something like:
a.end = a.end == NULL ? MAXDATE : a.end;
b.end = b.end == NULL ? MAXDATE : b.end;
bool overlap = a.start < b.end && b.start < a.end;
Or this could work:
bool overlap = (a.start < b.end || b.end == NULL) && (b.start < a.end || a.end == NULL);
Related
Given two horizontal lines on a 1 dimension plane. I want to check if they overlap at any point.
Below I show some examples of overlap. Please note that, intervals like [1,2] and [2,3] have borders "touching" but they don't overlap each other.
[1,2] is basically a line going from 1 on the x axis, to 2 on the x axis.
My question is, what are the exhaustive set of checks for such a condition. These are the ones I came up with, assuming that the first line is called, a, and the second line is called b.
b.s <= a.s && b.e > a.s
b.s <= a.s && b.e >= a.e
b.s < a.e && b.e >= a.e
b.s > a.s && b.e < a.s
Is it really this complicated? Isn't there an easier way to figure out if two lines overlap/one line contains a part of the other?
Aliter
By looking at the negation condition
public boolean isOverlap(Interval i1, Interval i2)
{
// if(i2.start <= i1.start && i2.end > i1.start)
// return true;
//
// if(i2.start <= i1.start && i2.end >= i1.end)
// return true;
//
// if(i2.start < i1.end && i2.end >= i1.end)
// return true;
//
// if(i2.start >= i1.start && i2.end <= i1.end)
// return true;
//
// return false;
if(i2.start <= i1.start && i2.end <= i1.start)
return false;
if(i2.start >= i1.end && i2.end >= i1.end)
return false;
return true;
}
An alternative method is to consider them on the same axis and check for disconnectedness.
Then you can say "what is the left most point?"
I'll use .l for "left" and .r for "right."
leftmost = a.l < b.l ? a : b
rightmost = leftmost == a ? b : a
Now you know the left most one and the right most one. In order for them to be connected the right most one must have a left part in between the the left and right of the leftmost one.
Assuming every line must have at least length 1 then you can simply do:
connected = rightmost.l < leftmost.r
I've written a program to find shortest path in a N*N grid recursively.
def dfs(x,y,Map,p):
N = len(Map)
p += [[x,y]]
if Map[x][y] == 'E':
return p
for i in [[x-1,y],[x+1,y],[x,y-1],[x,y+1]]:
if N > i[0] >= 0 and N > i[1] >= 0 :
if (Map[i[0]][i[1]] == 'P' or Map[i[0]][i[1]] == 'E') and i not in p:
dfs(i[0], i[1], Map,p)
return []
When Map[x][y] = 'E' the recursion don't stop and return p. But it goes till the end. How to correct it and return the path(p).
By the looks of it, the code is prone to loop indefinitely. This is due to lack of checks whether you've entered a node before and moving in all (4) directions from a given node.
To solve it simply, add another array NxN of Boolean values answering the question: visited?. Then update the code to something along the lines:
def dfs(x,y,Map,visited,p):
visited[x,y] = true;
N = len(Map)
(...)
if (Map[i[0]][i[1]] == 'P' or Map[i[0]][i[1]] == 'E')
and i not in p
and visited[i[0], i[1]] == false:
dfs(i[0], i[1], Map,visited,p)
Wondering why I can't use the following code.
enum player_state { fast, slow, focus, bust, out};
if (score >= 100)
player_state = fast;
else if (score > 50)
player_state = slow;
else if ((score <= 50) && (score > 1))
player_state = focus;
else if ((score == 1) || (score < 0))
player_state = bust;
else
player_state = out;
I get an error at the assignment's (=).
any tips? I thought I could do something like this, if not exactly this.
An enum is a type declaration. You have to make a variable that uses that type.
For instance
enum player_states { fast, slow, focus, bust, out};
player_states playerState = fast;
So I have this function:
public static BaseList GetVersesByChapterVerseRanges(string translation, BaseList criteriaList)
{
BaseList returnValue = new BaseList();;
if(criteriaList.Count() > 0)
{
List<VerseMaster.BusinessLayer.Vers> queryList = new List<Vers>();
int bookId = criteriaList[0].Book.BookId;
int StartChapter = criteriaList[0].StartChapter;
int EndChapter = criteriaList[0].EndChapter;
int StartVerse = criteriaList[0].StartChapterStartVerse;
int EndVerse = criteriaList[0].EndChapterEndVerse;
var searchQuery = (from v in VerseMaster.BusinessLayer.Controller.Controller.VerseMasterEntities.Verses where v.Translation.ToLower().StartsWith(translation.ToLower()) && v.BookId == bookId && v.ChapterNumber >= StartChapter && v.ChapterNumber <= EndChapter && (v.ChapterNumber == StartChapter ? (v.VerseNumber >= StartVerse) : true) && (v.ChapterNumber == EndChapter ? (v.VerseNumber <= EndVerse) : true) orderby v.ChapterNumber, v.VerseNumber ascending select v);
for(int i = 1; i < criteriaList.Count(); i++)
{
bookId = criteriaList[i].Book.BookId;
StartChapter = criteriaList[i].StartChapter;
EndChapter = criteriaList[i].EndChapter;
StartVerse = criteriaList[i].StartChapterStartVerse;
EndVerse = criteriaList[i].EndChapterEndVerse;
VerseMaster.BusinessLayer.DataObjects.Helper.VerseSearchCriteria criteria = criteriaList[i];
searchQuery = (System.Linq.IOrderedQueryable<VerseMaster.BusinessLayer.Vers>)searchQuery.Concat(from v in VerseMaster.BusinessLayer.Controller.Controller.VerseMasterEntities.Verses where v.Translation.ToLower().StartsWith(translation.ToLower()) && v.BookId == bookId && v.ChapterNumber >= StartChapter && v.ChapterNumber <= EndChapter && (v.ChapterNumber == StartChapter ? (v.VerseNumber >= StartVerse) : true) && (v.ChapterNumber == EndChapter ? (v.VerseNumber <= EndVerse) : true) orderby v.ChapterNumber, v.VerseNumber ascending select v);
}
string traceString = ((System.Data.Objects.ObjectQuery)searchQuery).ToTraceString();
foreach (var entVerse in searchQuery)
{
VerseMaster.BusinessLayer.DataObjects.IVerse verse = new VerseMaster.BusinessLayer.DataObjects.Verse();
LoadVerseFromEntityVerse(ref verse, entVerse);
Console.Write(String.Format("Chapter={0} Verse={1} Text={2}\n", entVerse.ChapterNumber, entVerse.VerseNumber, entVerse.Verse));
//returnValue.Add((VerseMaster.BusinessLayer.DataObjects.Verse)verse);
}
}
return returnValue;
}
So for some reason I had to convert each of the parameters that go into the linq query into standard ints because of an error, not sure if its related. So the generated sql is:
SELECT
[UnionAll2].[C1] AS [C1],
[UnionAll2].[C2] AS [C2],
[UnionAll2].[C3] AS [C3],
[UnionAll2].[C4] AS [C4],
[UnionAll2].[C5] AS [C5],
[UnionAll2].[C6] AS [C6]
FROM (SELECT
[UnionAll1].[VerseId] AS [C1],
[UnionAll1].[ChapterNumber] AS [C2],
[UnionAll1].[VerseNumber] AS [C3],
[UnionAll1].[Verse] AS [C4],
[UnionAll1].[Translation] AS [C5],
[UnionAll1].[BookId] AS [C6]
FROM (SELECT
[Extent1].[VerseId] AS [VerseId],
[Extent1].[ChapterNumber] AS [ChapterNumber],
[Extent1].[VerseNumber] AS [VerseNumber],
[Extent1].[Verse] AS [Verse],
[Extent1].[Translation] AS [Translation],
[Extent1].[BookId] AS [BookId]
FROM [dbo].[Verses] AS [Extent1]
WHERE (( CAST(CHARINDEX(LOWER(#p__linq__0), LOWER([Extent1].[Translation])) AS int)) = 1) AND ([Extent1].[BookId] = #p__linq__1) AND ([Extent1].[ChapterNumber] >= #p__linq__2) AND ([Extent1].[ChapterNumber] <= #p__linq__3) AND ((CASE WHEN ([Extent1].[ChapterNumber] = #p__linq__4) THEN CASE WHEN ([Extent1].[VerseNumber] >= #p__linq__5) THEN cast(1 as bit) WHEN ( NOT ([Extent1].[VerseNumber] >= #p__linq__5)) THEN cast(0 as bit) END ELSE cast(1 as bit) END) = 1) AND ((CASE WHEN ([Extent1].[ChapterNumber] = #p__linq__6) THEN CASE WHEN ([Extent1].[VerseNumber] <= #p__linq__7) THEN cast(1 as bit) WHEN ( NOT ([Extent1].[VerseNumber] <= #p__linq__7)) THEN cast(0 as bit) END ELSE cast(1 as bit) END) = 1)
UNION ALL
SELECT
[Extent2].[VerseId] AS [VerseId],
[Extent2].[ChapterNumber] AS [ChapterNumber],
[Extent2].[VerseNumber] AS [VerseNumber],
[Extent2].[Verse] AS [Verse],
[Extent2].[Translation] AS [Translation],
[Extent2].[BookId] AS [BookId]
FROM [dbo].[Verses] AS [Extent2]
WHERE (( CAST(CHARINDEX(LOWER(#p__linq__8), LOWER([Extent2].[Translation])) AS int)) = 1) AND ([Extent2].[BookId] = #p__linq__9) AND ([Extent2].[ChapterNumber] >= #p__linq__10) AND ([Extent2].[ChapterNumber] <= #p__linq__11) AND ((CASE WHEN ([Extent2].[ChapterNumber] = #p__linq__12) THEN CASE WHEN ([Extent2].[VerseNumber] >= #p__linq__13) THEN cast(1 as bit) WHEN ( NOT ([Extent2].[VerseNumber] >= #p__linq__13)) THEN cast(0 as bit) END ELSE cast(1 as bit) END) = 1) AND ((CASE WHEN ([Extent2].[ChapterNumber] = #p__linq__14) THEN CASE WHEN ([Extent2].[VerseNumber] <= #p__linq__15) THEN cast(1 as bit) WHEN ( NOT ([Extent2].[VerseNumber] <= #p__linq__15)) THEN cast(0 as bit) END ELSE cast(1 as bit) END) = 1)) AS [UnionAll1]
UNION ALL
SELECT
[Extent3].[VerseId] AS [VerseId],
[Extent3].[ChapterNumber] AS [ChapterNumber],
[Extent3].[VerseNumber] AS [VerseNumber],
[Extent3].[Verse] AS [Verse],
[Extent3].[Translation] AS [Translation],
[Extent3].[BookId] AS [BookId]
FROM [dbo].[Verses] AS [Extent3]
WHERE (( CAST(CHARINDEX(LOWER(#p__linq__16), LOWER([Extent3].[Translation])) AS int)) = 1) AND ([Extent3].[BookId] = #p__linq__17) AND ([Extent3].[ChapterNumber] >= #p__linq__18) AND ([Extent3].[ChapterNumber] <= #p__linq__19) AND ((CASE WHEN ([Extent3].[ChapterNumber] = #p__linq__20) THEN CASE WHEN ([Extent3].[VerseNumber] >= #p__linq__21) THEN cast(1 as bit) WHEN ( NOT ([Extent3].[VerseNumber] >= #p__linq__21)) THEN cast(0 as bit) END ELSE cast(1 as bit) END) = 1) AND ((CASE WHEN ([Extent3].[ChapterNumber] = #p__linq__22) THEN CASE WHEN ([Extent3].[VerseNumber] <= #p__linq__23) THEN cast(1 as bit) WHEN ( NOT ([Extent3].[VerseNumber] <= #p__linq__23)) THEN cast(0 as bit) END ELSE cast(1 as bit) END) = 1)) AS [UnionAll2]
So the problem is that for some reason its treating each of the parameters I give it as if I sent 3 sets of the same parameter and concatenated so its returning the same set of verses 3 times. I'm not sure why its doing this as I am not an expert of linq or the entity framework, any suggestions?
This is a very tricky example of the modified closure issue. In the for loop you generate a query part that uses the variables bookId etc. At the time that your query is executed it uses the values that these variables have at that moment, which are the values after the last iteration of the loop. (The tricky part is that usually the loop variable is captured, but here it is about variables that were declared outside the loop scope).
It would be different if you declared the variables within the for loop, because each query part would capture its own instance of the variable (which is called a closure).
The good news: you can do this and at the same time clean up your code!
if(criteriaList.Count() > 0)
{
IQueryable<Verse> baseQuery = null;
for(int i = 0; i < criteriaList.Count(); i++) // starting i = 0 now !
{
int bookId = criteriaList[0].Book.BookId;
int StartChapter = criteriaList[0].StartChapter;
int EndChapter = criteriaList[0].EndChapter;
int StartVerse = criteriaList[0].StartChapterStartVerse;
int EndVerse = criteriaList[0].EndChapterEndVerse;
searchQuery = (IOrderedQueryable<Vers>)searchQuery.Concat(
from v in Controller.Controller.VerseMasterEntities.Verses
where v.Translation.ToLower().StartsWith(translation.ToLower())
&& v.BookId == bookId
&& v.ChapterNumber >= StartChapter
&& v.ChapterNumber <= EndChapter
&& (v.ChapterNumber == StartChapter ? (v.VerseNumber >= StartVerse) : true)
&& (v.ChapterNumber == EndChapter ? (v.VerseNumber <= EndVerse) : true)
select v);
if (i == 0)
baseQuery = searchQuery;
else
baseQuery = baseQuery.Concat(searchQuery);
}
...
(do the ordering later)
The following LINQ to Entities query slows down substantially when the commented line below is included in the query. Is there a better way to phrase this?
The 'OR' clause should only take into account the following lines:
((o.Type_ID == (int) RecordEnums.RecordType.Lead) && (o.Item_ID == l1.Lead_ID))
|| ((o.Type_ID == (int)RecordEnums.RecordType.Opportunity)(o.Item_ID == o1.Opportunity_ID))
Full Query
return withItemsPending
? (from l1 in db.Leads
from o1 in db.Opportunities.Where(x => (x.Lead_ID == l1.Lead_ID) && (x.Company_ID == companyId)).DefaultIfEmpty()
from l2
in
db.Tasks.Where(
o =>
((o.IsCompleted ?? false) == false) &&
(o.TaskType_ID == typeId) &&
((o.Type_ID == (int) RecordEnums.RecordType.Lead) && (o.Item_ID == l1.Lead_ID))
//|| ((o.Type_ID == (int)RecordEnums.RecordType.Opportunity) && (o.Item_ID == o1.Opportunity_ID))
&&
(o.Due_Date > EntityFunctions.AddDays(DateTime.Now, -1)))
where (l1.Company_ID == companyId)
select l1)
: (from l1 in db.Leads where (0 == 1) select l1);
}
Here's the offending query:
SELECT Extent1.Lead_ID
FROM Leads AS Extent1 LEFT OUTER JOIN
Opportunities AS Extent2 ON Extent2.Lead_ID = Extent1.Lead_ID AND Extent2.Company_ID = 118 INNER JOIN
Tasks AS Extent3 ON 0 = (CASE WHEN ([Extent3].[IsCompleted] IS NULL) THEN CAST(0 AS bit) ELSE [Extent3].[IsCompleted] END) AND Extent3.TaskType_ID = 1 AND
5 = Extent3.Type_ID AND Extent3.Item_ID = Extent1.Lead_ID OR
4 = Extent3.Type_ID AND Extent3.Item_ID = Extent2.Opportunity_ID AND Extent3.Due_Date > DATEADD(day, - 1, SysDateTime())
WHERE (Extent1.Company_ID = 118)
If what you want is "give me the items that are not completed AND are due later than yesterday AND are EITHER Lead OR Opportunity", you need to add extra brackets around the 2 statements you want to OR. What you are currently saying is "give me the items that are either not completed AND Lead OR Opportunity AND due later than yesterday".
The code will then be like this:
return withItemsPending
? (from l1 in db.Leads
from o1 in db.Opportunities.Where(x => (x.Lead_ID == l1.Lead_ID) && (x.Company_ID == companyId)).DefaultIfEmpty()
from l2
in
db.Tasks.Where(
o =>
((o.IsCompleted ?? false) == false) &&
(o.TaskType_ID == typeId) &&
(((o.Type_ID == (int) RecordEnums.RecordType.Lead) && (o.Item_ID == l1.Lead_ID))
|| ((o.Type_ID == (int)RecordEnums.RecordType.Opportunity) && (o.Item_ID == o1.Opportunity_ID)))
&&
(o.Due_Date > EntityFunctions.AddDays(DateTime.Now, -1)))
where (l1.Company_ID == companyId)
select l1)
: (from l1 in db.Leads where (0 == 1) select l1);
}