Note that there are some explanatory texts on larger screens.

plurals
  1. PORotate an array using LINQ syntax
    primarykey
    data
    text
    <p>I am solving this problem of rotating an array and got algorithm and code working</p> <pre><code> int[] Rotate(int[] ar,int k) { if (k &lt;= 0 || k &gt; ar.Length - 1) return ar; Reverse(ar, 0, k - 1); Reverse(ar, k, ar.Length - 1); Reverse(ar, 0, ar.Length - 1); return ar; } void Reverse(int[] ar,int start, int end) { while (start &lt; end) { int temp = ar[start]; ar[start] = ar[end]; ar[end] = temp; start++; end--; } } </code></pre> <p>Now I want to do this in LINQ and I got the below code, I think this can be done much better. </p> <pre><code> int[] Rotate(int[] ar,int k) { if (k &lt;= 0 || k &gt; ar.Length - 1) return ar; int[] ar1=ar.Take(k-1).Reverse().ToArray(); int[] ar2=ar.Skip(k - 1).Take(ar.Length - k+1).Reverse().ToArray(); int[] ar3 = ar1.Concat(ar2).Reverse().ToArray(); return ar3; } </code></pre> <p>This is a well known algorithm from Programming pearls - <a href="http://books.google.com/books?id=kse_7qbWbjsC&amp;lpg=PA14&amp;ots=DfzTzQCSar&amp;dq=rotate%20an%20array%20programming%20pearls&amp;pg=PA14#v=onepage&amp;q&amp;f=false" rel="nofollow noreferrer">http://books.google.com/books?id=kse_7qbWbjsC&amp;lpg=PA14&amp;ots=DfzTzQCSar&amp;dq=rotate%20an%20array%20programming%20pearls&amp;pg=PA14#v=onepage&amp;q&amp;f=false</a></p> <p>And in general how to develop my LINQ skills, if I am given a programming problem, right now I am only thinking in for loops or foreach loops, how to think in terms of linq operators. I am reading C# 4.0 nutshell, other than practicing any advice? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload