Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace BadCodeChallenge17
- {
- class Program
- {
- public static int outlier;
- static void Main(string[] args)
- {
- Console.WriteLine("Enter your array:");
- string arrayString = Console.ReadLine();
- string[] arrayStringArray = arrayString.Split(',');
- int[] intArray = new int[arrayStringArray.Length];
- for(int j = 0; j < arrayStringArray.Length; j++)
- {
- intArray[j] = int.Parse(arrayStringArray[j].ToString());
- }
- FindParityOutlier(intArray);
- Console.WriteLine(outlier + " is the outlier");
- Console.ReadKey();
- }
- static bool isEven(int[] intArray, int index)
- {
- string onesPositionBit;
- int intArraySize = intArray.Length;
- string odd = null, even = null;
- int currentInt = intArray[0];
- int currentInt2 = intArray[1];
- string binaryCurrentInt = Convert.ToString(currentInt, 2);
- if (binaryCurrentInt.Length > 1)
- {
- onesPositionBit = binaryCurrentInt.Substring(binaryCurrentInt.Length - 1);
- }
- else
- {
- onesPositionBit = binaryCurrentInt.Substring(0, 1);
- }
- switch (Convert.ToChar(onesPositionBit))
- {
- case '0':
- odd = "yes";
- break;
- case '1':
- even = "yes";
- break;
- default:
- odd = "no";
- even = "no";
- break;
- }
- if (odd == "yes")
- {
- return false;
- }
- else if (even != null)
- {
- return true;
- }
- else
- {
- throw new Exception();
- }
- }
- static void FindParityOutlier(int[] intArray)
- {
- if(isEven(intArray, 0) == true && isEven(intArray,1) == true)
- {
- FindOddParityOutlier(intArray);
- }
- else if (isEven(intArray, 0) == false && isEven(intArray, 1) == false)
- {
- FindEvenParityOutlier(intArray);
- }
- else
- {
- throw new Exception();
- }
- }
- static void FindOddParityOutlier(int[] intArray)
- {
- int intArraySize = intArray.Length;
- for (int k = 0; k < intArraySize; k++)
- {
- int tempIntArrayValue = intArray[k];
- while(tempIntArrayValue > 0)
- {
- tempIntArrayValue = tempIntArrayValue - 2;
- }
- if(tempIntArrayValue == 0)
- {
- outlier = intArray[k];
- }
- }
- }
- static void FindEvenParityOutlier(int[] intArray)
- {
- int intArraySize = intArray.Length;
- for (int k = 0; k < intArraySize; k++)
- {
- int tempIntArrayValue = intArray[k];
- while (tempIntArrayValue > 0)
- {
- tempIntArrayValue = tempIntArrayValue - 2;
- }
- if (tempIntArrayValue == -1)
- {
- outlier = intArray[k];
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment