Advertisement
yahorrr

Untitled

Sep 21st, 2022
1,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using System;
  2.  
  3. namespace FilterByPalindromicTask
  4. {
  5.     /// <summary>
  6.     /// Provides static method for working with integers array.
  7.     /// </summary>
  8.     public static class ArrayExtension
  9.     {
  10.         /// <summary>
  11.         /// Returns new array that contains only palindromic numbers from source array.
  12.         /// </summary>
  13.         /// <param name="source">Source array.</param>
  14.         /// <returns>Array of elements that are palindromic numbers.</returns>
  15.         /// <exception cref="ArgumentNullException">Throw when array is null.</exception>
  16.         /// <exception cref="ArgumentException">Throw when array is empty.</exception>
  17.         /// <example>
  18.         /// {12345, 1111111112, 987654, 56, 1111111, -1111, 1, 1233321, 70, 15, 123454321}  => { 1111111, 123321, 123454321 }
  19.         /// {56, -1111111112, 987654, 56, 890, -1111, 543, 1233}  => {  }.
  20.         /// </example>
  21.         public static int[] FilterByPalindromic(int[]? source)
  22.         {
  23.             if (source == null)
  24.             {
  25.                 throw new ArgumentNullException(nameof(source), "array is null");
  26.             }
  27.  
  28.             if (source.Length == 0)
  29.             {
  30.                 throw new ArgumentException("array length is null", nameof(source));
  31.             }
  32.  
  33.             int length = source.Length;
  34.             int[] result = new int[length];
  35.             int k = 0;
  36.  
  37.             for (int i = 0; i < length; i++)
  38.             {
  39.                 if (IsPalindromicNumber(source[i]))
  40.                 {
  41.                     result[k] = source[i];
  42.                     k++;
  43.                 }
  44.             }
  45.  
  46.             Array.Resize(ref result, k);
  47.  
  48.             return result;
  49.         }
  50.  
  51.         private static bool IsPalindromicNumber(int number)
  52.         {
  53.             if (number < 0)
  54.             {
  55.                 return false;
  56.             }
  57.  
  58.             byte length = IntLength(number);
  59.  
  60.             return IsPalindromicNumberCore((int)Math.Pow(10, length - 1));
  61.  
  62.             bool IsPalindromicNumberCore(int pow)
  63.             {
  64.                 if (number < 10)
  65.                 {
  66.                     return true;
  67.                 }
  68.  
  69.                 if (number / pow != number % 10)
  70.                 {
  71.                     return false;
  72.                 }
  73.  
  74.                 number = (number % pow) / 10;
  75.  
  76.                 return IsPalindromicNumberCore(pow / 100);
  77.             }
  78.         }
  79.  
  80.         private static byte IntLength(int number) =>
  81.             number switch
  82.             {
  83.                 < 10 => 1,
  84.                 < 100 => 2,
  85.                 < 1000 => 3,
  86.                 < 10000 => 4,
  87.                 < 100000 => 5,
  88.                 < 1000000 => 6,
  89.                 < 10000000 => 7,
  90.                 < 100000000 => 8,
  91.                 < 1000000000 => 9,
  92.                 _ => 10
  93.             };
  94.     }
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement