❔ 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
You can use LINQ
It won't be as performant, but will be more concise than nested loops
SelectMany()
will be of particular interestHmm, how would linq go about this if I may ask
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?
what's wrong with nesting for loops
Bad time complexity
Yes, it will most probably be less performant
Whether the difference matters or not, up to you
huh?
what's the time complexity, you figure?
name it
The larger the list grows, the more time it'd take to query
That will be true regardless of how you handle it
there are two lists
You're getting all elements regardless
no, let them think it through
you have no right to worry about time complexity unless you understand it
So you're saying the time to carry this out won't change regardless of method used
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?
Just two
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
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?
2 to the 3rd power?
(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?
Given 2 visits and 3 investigations I guess so
see any patterns that might lead you to a formula?
a formula for the number of pairs
3n? Where n is the number of visits?
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?
Number of objects in investigation list?
yeah
call it
m
so what's the number of pairs, in terms of n
and m
?3mn?
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
is this less operations than a nested for loop?
oh i reread the question just now
Thanks in advance? Allow me to go learn more on this to fully get what you mean here
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
Ah I see. Damn. Thanks for taking your time with this man
Man, I'd just stop at "unga bunga linq" there lol
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.