Sort only the odd elements of an array.

huangapple go评论108阅读模式
英文:

Sort only the odd elements of an array

问题

I want to sort only the odd elements of an array, leaving the even elements unchanged.

I have created the below using LINQ queries, but get an error on this line:

  1. .Select(x => x.value % 2 == 1 ? x.value : dictionary[x.index])

The error is due to there not being an item in the dictionary with key '2'. I.e. the dictionary[x.index] term in the if statement is being evaluated when the criteria for the if statement is false (i.e. when x.value is even).

  1. public class Kata
  2. {
  3. public static int[] SortArray(int[] array)
  4. {
  5. var dictionary = array
  6. .Select((value, index) => new { value, index })
  7. .Where(x => x.value % 2 == 1)
  8. .OrderBy(x => x.value)
  9. .ToDictionary(x => x.index, x => x.value);
  10. var output = array
  11. .Select((value, index) => new { value, index })
  12. .Select(x => x.value % 2 == 1 ? x.value : dictionary[x.index])
  13. .ToArray();
  14. return new int[1];
  15. }
  16. }
  17. public class Program
  18. {
  19. static void Main(string[] args)
  20. {
  21. Console.WriteLine(Kata.SortArray(new int[] { 5, 3, 2, 8, 1, 4 }));
  22. }
  23. }
英文:

I want to sort only the odd elements of an array, leaving the even elements unchanged.

I have created the below using LINQ queries, but get an error on this line:

  1. .Select(x => x.value % 2 == 1 ? x.value : dictionary[x.index])

The error is due to there not being an item in the dictionary with key '2'. I.e. the dictionary[x.index] term in the if statement is being evaluated when the criteria for the if statement is false (i.e. when x.value is even).

  1. public class Kata
  2. {
  3. public static int[] SortArray(int[] array)
  4. {
  5. var dictionary = array
  6. .Select((value, index) => new { value, index })
  7. .Where(x => x.value % 2 == 1)
  8. .OrderBy(x => x.value)
  9. .ToDictionary(x => x.index, x => x.value);
  10. var output = array
  11. .Select((value, index) => new { value, index })
  12. .Select(x => x.value % 2 == 1 ? x.value : dictionary[x.index])
  13. .ToArray();
  14. return new int[1];
  15. }
  16. }
  17. public class Program
  18. {
  19. static void Main(string[] args)
  20. {
  21. Console.WriteLine(Kata.SortArray(new int[] { 5, 3, 2, 8, 1, 4 }));
  22. }
  23. }

答案1

得分: 1

你可以提取奇数项,对它们进行排序,最后将它们放回:

  1. int[] array = new int[] { 5, 3, 2, 8, 1, 4};
  2. // 提取并排序奇数项
  3. using var odd = array
  4. .Where(item => item % 2 != 0)
  5. .OrderBy(item => item)
  6. .GetEnumerator();
  7. for (int i = 0; i < array.Length; ++i)
  8. if (array[i] % 2 != 0 && odd.MoveNext())
  9. array[i] = odd.Current;

请注意,检查奇数应该是 item % 2 != 0,而不是 item % 2 == 1,因为余数 % 在负数 item 上返回 -1

英文:

You can extract odd items, sort them, and finally put them back:

  1. int[] array = new int[] { 5, 3, 2, 8, 1, 4};
  2. // odd items extracted and sorted
  3. using var odd = array
  4. .Where(item =&gt; item % 2 != 0)
  5. .OrderBy(item =&gt; item)
  6. .GetEnumerator();
  7. for (int i = 0; i &lt; array.Length; ++i)
  8. if (array[i] % 2 != 0 &amp;&amp; odd.MoveNext())
  9. array[i] = odd.Current;

Fiddle

Please, note that checking for odd should be item % 2 != 0, not item % 2 == 1, since remainder % returns -1 on negative item.

huangapple
  • 本文由 发表于 2023年5月15日 05:23:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定