Guest User

Untitled

a guest
Aug 13th, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.53 KB | None | 0 0
  1. using System;
  2.  
  3. namespace BadCodeChallenge17
  4. {
  5.     class Program
  6.     {
  7.         public static int outlier;
  8.         static void Main(string[] args)
  9.         {
  10.             Console.WriteLine("Enter your array:");
  11.             string arrayString = Console.ReadLine();
  12.             string[] arrayStringArray = arrayString.Split(',');
  13.             int[] intArray = new int[arrayStringArray.Length];
  14.             for(int j = 0; j < arrayStringArray.Length; j++)
  15.             {
  16.                 intArray[j] = int.Parse(arrayStringArray[j].ToString());
  17.             }
  18.             FindParityOutlier(intArray);
  19.             Console.WriteLine(outlier + " is the outlier");
  20.             Console.ReadKey();
  21.         }
  22.  
  23.         static bool isEven(int[] intArray, int index)
  24.         {
  25.             string onesPositionBit;
  26.             int intArraySize = intArray.Length;
  27.             string odd = null, even = null;
  28.             int currentInt = intArray[0];
  29.             int currentInt2 = intArray[1];
  30.             string binaryCurrentInt = Convert.ToString(currentInt, 2);
  31.             if (binaryCurrentInt.Length > 1)
  32.             {
  33.                 onesPositionBit = binaryCurrentInt.Substring(binaryCurrentInt.Length - 1);
  34.             }
  35.             else
  36.             {
  37.                 onesPositionBit = binaryCurrentInt.Substring(0, 1);
  38.             }
  39.             switch (Convert.ToChar(onesPositionBit))
  40.             {
  41.                 case '0':
  42.                     odd = "yes";
  43.                     break;
  44.                 case '1':
  45.                     even = "yes";
  46.                     break;
  47.                 default:
  48.                     odd = "no";
  49.                     even = "no";
  50.                     break;
  51.             }
  52.  
  53.             if (odd == "yes")
  54.             {
  55.                 return false;
  56.             }
  57.             else if (even != null)
  58.             {
  59.                 return true;
  60.             }
  61.             else
  62.             {
  63.                 throw new Exception();
  64.             }
  65.         }
  66.  
  67.         static void FindParityOutlier(int[] intArray)
  68.         {
  69.             if(isEven(intArray, 0) == true && isEven(intArray,1) == true)
  70.             {
  71.                 FindOddParityOutlier(intArray);
  72.             }
  73.             else if (isEven(intArray, 0) == false && isEven(intArray, 1) == false)
  74.             {
  75.                 FindEvenParityOutlier(intArray);
  76.             }
  77.             else
  78.             {
  79.                 throw new Exception();
  80.             }
  81.         }
  82.  
  83.         static void FindOddParityOutlier(int[] intArray)
  84.         {
  85.             int intArraySize = intArray.Length;
  86.             for (int k = 0; k < intArraySize; k++)
  87.             {
  88.                 int tempIntArrayValue = intArray[k];
  89.                 while(tempIntArrayValue > 0)
  90.                 {
  91.                     tempIntArrayValue = tempIntArrayValue - 2;
  92.                 }
  93.                 if(tempIntArrayValue == 0)
  94.                 {
  95.                     outlier = intArray[k];
  96.                 }
  97.             }
  98.         }
  99.  
  100.         static void FindEvenParityOutlier(int[] intArray)
  101.         {
  102.             int intArraySize = intArray.Length;
  103.             for (int k = 0; k < intArraySize; k++)
  104.             {
  105.                 int tempIntArrayValue = intArray[k];
  106.                 while (tempIntArrayValue > 0)
  107.                 {
  108.                     tempIntArrayValue = tempIntArrayValue - 2;
  109.                 }
  110.                 if (tempIntArrayValue == -1)
  111.                 {
  112.                     outlier = intArray[k];
  113.                 }
  114.             }
  115.         }
  116.  
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment