C
C#2y ago
sentrycod

❔ Efficient way to query this problem

I have a logic question. Right now, I'm doing it the worst way possible... Which is s adly the only way I know rn. Nesting for loops. I know, I know. Hence why I'm looking for a better way
33 Replies
Angius
Angius2y ago
You can use LINQ It won't be as performant, but will be more concise than nested loops SelectMany() will be of particular interest
sentrycod
sentrycodOP2y ago
Hmm, how would linq go about this if I may ask
Angius
Angius2y ago
var allInvestigations = someCase.Visits
.SelectMany(visit => visit.Investigations)
.ToArray();
var allInvestigations = someCase.Visits
.SelectMany(visit => visit.Investigations)
.ToArray();
sentrycod
sentrycodOP2y ago
I'd give this a shot thanks. You said it won't be as performant. Do you mean it'd be less performant than the for loop method?
Anton
Anton2y ago
what's wrong with nesting for loops
sentrycod
sentrycodOP2y ago
Bad time complexity
Angius
Angius2y ago
Yes, it will most probably be less performant Whether the difference matters or not, up to you
Anton
Anton2y ago
huh? what's the time complexity, you figure? name it
sentrycod
sentrycodOP2y ago
The larger the list grows, the more time it'd take to query
Angius
Angius2y ago
That will be true regardless of how you handle it
Anton
Anton2y ago
there are two lists
Relevant
Relevant2y ago
You're getting all elements regardless
Anton
Anton2y ago
no, let them think it through you have no right to worry about time complexity unless you understand it
sentrycod
sentrycodOP2y ago
So you're saying the time to carry this out won't change regardless of method used
Anton
Anton2y ago
I urge you to think it through if you want to do some operation on all pairs (visit, investigation), you'll have to go through all pairs how many pairs are there?
sentrycod
sentrycodOP2y ago
Just two
Relevant
Relevant2y ago
There are certainly ways to make it really bad if you tried hard enough. So I wouldn't say regardless of method used, but there will be a limit to how few steps is possible
Anton
Anton2y ago
the number of pairs is the same as the optimal time complexity think again if there are two visits and three investigations, how many pairs are there?
sentrycod
sentrycodOP2y ago
2 to the 3rd power?
Anton
Anton2y ago
(a,b) and (1,2,3) how many pairs (a,1), (a,2), ... list them ok let me List them (a,1), (a,2), (a,3), (b,1), (b,2), (b,3) are these all the pairs?
sentrycod
sentrycodOP2y ago
Given 2 visits and 3 investigations I guess so
Anton
Anton2y ago
see any patterns that might lead you to a formula? a formula for the number of pairs
sentrycod
sentrycodOP2y ago
3n? Where n is the number of visits?
Anton
Anton2y ago
how many choices do you have for the first element? how many choices for the second element for each of the choices for the first element? yeah, because you have n choices for the first element and 3 choices for the second element for each of the first choices 3 is what the size of what?
sentrycod
sentrycodOP2y ago
Number of objects in investigation list?
Anton
Anton2y ago
yeah call it m so what's the number of pairs, in terms of n and m?
sentrycod
sentrycodOP2y ago
3mn?
Anton
Anton2y ago
think again? Now think about what a nested for loop does. It goes through all visits (the choice for the first element) and for each of those goes through each investigation (the second choice for each of the first choices). If you think about it for a bit, you will realize it's fundamentally same number of iterations as the number of pairs. you could do an unnested loop through all pairs instead. ig like this
foreach (var (v, i) in visits.SelectMany(v => investigations.Select(i => (v, i))))
// operation
foreach (var (v, i) in visits.SelectMany(v => investigations.Select(i => (v, i))))
// operation
is this less operations than a nested for loop? oh i reread the question just now
sentrycod
sentrycodOP2y ago
Thanks in advance? Allow me to go learn more on this to fully get what you mean here
Anton
Anton2y ago
sure in your question you can't possibly do it any other way, since the investigations are inside the visits you'll have to go through all visits, whatever you do there's fundamentally no way around it
sentrycod
sentrycodOP2y ago
Ah I see. Damn. Thanks for taking your time with this man
Angius
Angius2y ago
Man, I'd just stop at "unga bunga linq" there lol
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server