C# arrays minimal value in specific range?

3 @nozim: No, it's not because it's not immediately clear what it's doing. It's clever, and it looks clever, but it's not the most readable solution and thus should be rejected. Sorry.

– Jason Dec 10 '10 at 19:40 6 @Jason: skip 2 items, take 5 items, then get the min. Whats not clear? – poindexter12 Dec 10 '10 at 19:42 1 Agreed - it's clear to anyone who's familiar with LINQ.

– Jon Skeet Dec 10 '10 at 19:44 3 @Jon Skeet: I feel the point is being missed here, especially if you disagree. Yes it's understandable that it's skipping two, taking five and finding the min. What is not clear is that it is solving the problem at hand.It takes a moment of thought (albeit small) to see that whereas the solution I previously pointed to does not.

I feel this is getting voted for because of the cleverness factor (and clever code is bad! ). – Jason Dec 10 '10 at 19:48 1 @Jason: I feel this is simpler to understand than Thomas's approach.

The use of Skip and Take make it immediately clear that the values of the elements aren't relevant to the filtering... whereas the more general Where clause forces the reader to consider carefully exactly what's going on. Note that for an array of integers, it would also be easy to get the value and index parameters the wrong way round accidentally. If you want to make it more "start" and "end" related, then Eric's approach is a good option.

– Jon Skeet Dec 10 '10 at 19:52.

I figure I may as well add my tuppence to this. As Jason objects to the fact that we're saying how many we're skipping rather than the end index, we can add a simple extension method: public static IEnumerable WithIndexBetween(this IEnumerable source, int startInclusive, int endExclusive) { // The two values can be the same, yielding no results... but they must // indicate a reasonable range if (endExclusive Take(endExclusive - startInclusive); } Then: int min = array. WithIndexBetween(2, 7).Min(); Adjust the extension method name to taste.(Naming is hard, and I'm not going to spend ages coming up with a nice one here :).

Yes, it's good because now it reads like the problem statement. Note that it reads as "takes those values in the sequence with index between two and seven, inclusive, and take the minimum" which is how Thomas' reads to me which is why I preferred it in the first place. So, I like this.

– Jason Dec 10 '10 at 20:27 I was about to update my answer with something similar, then I saw your answer... – Thomas Levesque Dec 10 '10 at 22:57 Jon, this is Awesome! I love extension methods. And Jason, I like your persistence and that too against Jon Skeet!

:) – decyclone Dec 11 '10 at 5:51.

Int arr = {0,1,2,3,4,5,6,7,8}; int start = 3; int end = 8; int min = arr. Skip(start - 1). Take(end - start).Min().

Just to add another option: int start = 3; int end = 8; var min = Enumerable. Range(start - 1,end - start). Select(idx => arrayidx).Min(); AFAIK, this is "theorically" faster if you have to take a range near to the end of the one, and your array is really really long.

That's because (again AFAIK) Skip() doesn't take into account that is an array (i.e. Can be accessed randomly in O(1)) and enumerates it anyway.

1 Yes, it's faster in the specific case of using an array (or other IList) - but of course it falls apart in the general sequence case. Of course, Skip might be fixed in a future release, too :) – Jon Skeet Dec 10 '10 at 20:06 Of course, and I really hope it will be fixed. In the meanwhile mine was just a sort of warning :) – digEmAll Dec 10 '10 at 20:10 +1 for the warning, even though it's not the solution I'd usually pick :) – Jon Skeet Dec 10 '10 at 20:17.

Int min = array. Where((value, index) => index >= 2 && index index.

Personally, I'd prefer this: public static class ArrayExtensions { public static bool ArrayAndIndexesAreValid( T array, int startInclusive, int endExclusive ) { return array! = null && array. Length > 0 && startInclusive >= 0 && startInclusive = 1 && endExclusive Slice( this T array, int startInclusive, int endExclusive ) { Contract.

Requires(ArrayAndIndexesAreValid( array, startInclusive, endExclusive) ); for (int index = startInclusive; index Requires(ArrayAndIndexesAreValid( array, startInclusive, endExclusive) ); return array. Slice(startInclusive, endExclusive).Max(); } }.

That will return int. MaxValue if the slice contains no elements, which probably isn't a good idea. It's also mixing the logic for "finding the minimum value" and "considering a slice of the array" which the LINQ-based solutions don't.

If you need to find the maximum value of an array slice, you'll be repeating all that slicing logic. – Jon Skeet Dec 10 '10 at 20:25 By not making it generic, you've also forced yourself to reimplement it if you need the minimum value from a long etc. Making this operation generic is a no-brainer IMO. Of course, it's hard to do that while you're going to return a value for an empty array slice... – Jon Skeet Dec 10 '10 at 20:27 @Jon Skeet: It throws on empty (which might not be preferred; see the second Contract.

Requires). I'll respond to your second comment in a minute. – Jason Dec 10 '10 at 20:29 @Jason: Ah, the endIndex is inclusive.

I hadn't expected that. (That's why my method's parameters make it explicit :) Generally speaking, methods expressing a min and max value (e.g.Random. Next) use an inclusive lower bound and an exclusive upper bound.

– Jon Skeet Dec 10 '10 at 20:34 @Jason: When it's so easy to separate the concerns of slicing and max/min, and also very easy to make it generic, I don't think YAGNI really applies. It's hardly over-engineering to use a generic method where it's obviously a generic algorithm. I'm still not convinced that putting the two aspects in the same method call is a great idea.

Maybe if you're doing this over and over again... but otherwise, use composition of simple operations at the client side. – Jon Skeet Dec 10 '10 at 20:50.

AFAIK, this is "theorically" faster if you have to take a range near to the end of the one, and your array is really really long. That's because (again AFAIK) Skip() doesn't take into account that is an array (i.e. Can be accessed randomly in O(1)) and enumerates it anyway.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions