C
C#17mo 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
Angius17mo 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
sentrycod17mo ago
Hmm, how would linq go about this if I may ask
Angius
Angius17mo ago
var allInvestigations = someCase.Visits
.SelectMany(visit => visit.Investigations)
.ToArray();
var allInvestigations = someCase.Visits
.SelectMany(visit => visit.Investigations)
.ToArray();
sentrycod
sentrycod17mo 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
Anton17mo ago
what's wrong with nesting for loops
sentrycod
sentrycod17mo ago
Bad time complexity
Angius
Angius17mo ago
Yes, it will most probably be less performant Whether the difference matters or not, up to you
Anton
Anton17mo ago
huh? what's the time complexity, you figure? name it
sentrycod
sentrycod17mo ago
The larger the list grows, the more time it'd take to query
Angius
Angius17mo ago
That will be true regardless of how you handle it
Anton
Anton17mo ago
there are two lists
Relevant
Relevant17mo ago
You're getting all elements regardless
Anton
Anton17mo ago
no, let them think it through you have no right to worry about time complexity unless you understand it
sentrycod
sentrycod17mo ago
So you're saying the time to carry this out won't change regardless of method used
Anton
Anton17mo 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
sentrycod17mo ago
Just two
Relevant
Relevant17mo 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
Anton17mo 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
sentrycod17mo ago
2 to the 3rd power?
Anton
Anton17mo 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
sentrycod17mo ago
Given 2 visits and 3 investigations I guess so
Anton
Anton17mo ago
see any patterns that might lead you to a formula? a formula for the number of pairs
sentrycod
sentrycod17mo ago
3n? Where n is the number of visits?
Anton
Anton17mo 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
sentrycod17mo ago
Number of objects in investigation list?
Anton
Anton17mo ago
yeah call it m so what's the number of pairs, in terms of n and m?
sentrycod
sentrycod17mo ago
3mn?
Anton
Anton17mo 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
sentrycod17mo ago
Thanks in advance? Allow me to go learn more on this to fully get what you mean here
Anton
Anton17mo 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
sentrycod17mo ago
Ah I see. Damn. Thanks for taking your time with this man
Angius
Angius17mo ago
Man, I'd just stop at "unga bunga linq" there lol
Accord
Accord17mo 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
More Posts
❔ Can't build wpf project. "TestClassifier_w04izrw3_wpftmp.csproj" -- FAILED.I'm trying to build a WPF project and I keep seeing the build error: `Done building project "TestCla❔ repo doesnt connect to github on another pcmy repo (solution) is on a usb stick, i've created it on another pc and pushed it to github, now i a❔ How to get hostname string ?i need the hostname string in MappingProfile for automapper i have no access to `HttpContext.CurrenHow to get and set position of an ui element via code wpf c#Hello i want to create TextBoxes via code and set their position but i can't found how to even get tLooking for somebody who can teach me using C# language, simple things for 2D games, going to pay💰I’m looking for somebody who can teach me the C# language so I can begin creating a 2D game, I will ❔ How to handle null in InterfaceI'm trying to learn .Net Core MVC and I'm following a course where the following code gives a warninTrying to delete registry Sub key treeHey I am trying to delete this sub key tree but I keep getting errors. my code is in screen shot. a❔ snippet helpIn unity, is always s good practice to null check component. I made this ``` <CodeSnippets xmlns="h❔ How to build ConsoleApp to Linux?Hello I know there's a way to do it, but I dont know how. I actually working on windows(10) and usin❔ FILewatcher raise multiple events when a file is createdHello everyone. I have a code that alert me when a file is created in a folder (the file is created