Since C# doesn't have a before,after,last,first etc. as part of its foreach. The challenge is to mimic this behavior as elegantly as possible with the following criteria:
If you can exceed the above criteria, pleases do!
I'll post my answer below, but its not elegant nor is it feasible, so I would like to see what the community can conjure up.
hard coded for loops get annoying sometimes =(
HackerRank is very good for beginners so even if you want to print your first program “Hello World!” then definitely HackerRank gives this opportunity to you. It has a pretty good UI with boilerplate code pre-written that helps beginners to start competitive coding.
All Free. No Credit Card Required.
Where can I get C Programming Questions and Answers with Explanation? IndiaBIX provides you lots of fully solved C Programming questions and answers with explanation. Fully solved examples with detailed answer description, explanation are given and it would be easy to understand.
LINQ...
.SkipWhile(predicate)
(left vague as your meaning isn't clear).TakeWhile(predicate)
(left vague as your meaning isn't clear).Last()
.First()
.Where((x,i)=>i%2==1)
.Where((x,i)=>i%2==0)
Jon Skeet wrote SmartEnumerable for this purpose. It's easily extended to provide IsOdd
and IsEven
properties.
Marc Gravell's answer is good because it's simple, but it will be less performant for cases when you first want to do something on all the odd elements and then on all the even elements, since that would have you iterate over all elements twice, while only a single time is strictly necessary.
I suppose you could write a function from an element to some enumeration and group all elements by the enumeration value they map to. Then you can easily handle each group seperately. But I'm not sure how this would perform, as I'm not sure what LINQ grouping specifically does and how much it's deferred.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With