daily pastebin goal
94%
SHARE
TWEET

Competency test

a guest Apr 16th, 2018 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace ExaminationTest
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     using System.Collections.Generic;
  6.  
  7.     /// <summary>
  8.     /// Developer Entry Exercises
  9.     /// </summary>
  10.     public class ExaminationTest
  11.     {
  12.  
  13.         /// <summary>
  14.         /// Returns whether or not the value is contained in the array
  15.         /// </summary>
  16.         public static bool ArrayContainsValue(int[] array, int value)
  17.         {
  18.             bool found = false;
  19.             if (array != null && value != null)
  20.             {
  21.                 for (int i = 0; i < array.Length; i++)
  22.                 {
  23.                     if (array[i] == value)
  24.                     {
  25.                         found = true;
  26.                         break;
  27.                     }
  28.                 }
  29.             }
  30.             return found;
  31.         }
  32.  
  33.         /// <summary>
  34.         /// Returns an array of integers that are contained in all three arrays.
  35.         /// </summary>
  36.         public static int[] CommonNumbersInArrays(int[] array1, int[] array2, int[] array3)
  37.         {
  38.             List<int> common = new List<int>();
  39.             List<int> checkedNums = new List<int>();
  40.  
  41.             if (array1 != null && array2 != null && array3 != null)
  42.             {
  43.                 for (int i = 0; i < array1.Length; i++)
  44.                 {
  45.                     bool[] foundInArray = new bool[] { false, false };
  46.  
  47.                     //skip any already checked values
  48.                     if (checkedNums.Contains(array1[i])) continue;
  49.  
  50.                     //check 2nd array against 1st
  51.                     for (int j = 0; j < array2.Length; j++)
  52.                     {
  53.                         if (array2[j] == array1[i])
  54.                         {
  55.                             foundInArray[0] = true;
  56.                             break;
  57.                         }
  58.                     }
  59.  
  60.                     //check 3rd array against 1st
  61.                     for (int j = 0; j < array3.Length; j++)
  62.                     {
  63.                         if (array3[j] == array1[i])
  64.                         {
  65.                             foundInArray[1] = true;
  66.                             break;
  67.                         }
  68.                     }
  69.  
  70.                     //if found in both arrays add it to the array
  71.                     if (foundInArray[0] && foundInArray[1]) common.Add(array1[i]);
  72.  
  73.                     //add the number we've checked to the array to save on computation
  74.                     checkedNums.Add(array1[i]);
  75.                 }
  76.             }
  77.  
  78.             return common.ToArray();
  79.         }
  80.  
  81.         /// <summary>
  82.         /// Returns whether or not the passed in word is a palindrome if all punctuation is excluded (i.e. just the letters are a palindrome)
  83.         /// </summary>
  84.         /// <remarks>A palindrome reads the same forward or reversed</remarks>
  85.         public static bool SentenceIsPalindrome(string word)
  86.         {
  87.             //normalise string
  88.             word = word.ToLower();
  89.  
  90.             //strip punctuation
  91.             for (int i = 0; i < word.Length; i++)
  92.             {
  93.                 if (char.IsPunctuation(word[i]) || word[i] == ' ')
  94.                 {
  95.                     word = word.Remove(i, 1);
  96.                     //move index back one as we've removed a character! Argh!
  97.                     i--;
  98.                 }
  99.             }
  100.  
  101.             char[] charArray = word.ToCharArray();
  102.             Array.Reverse(charArray);
  103.             string reversed = new string(charArray);
  104.  
  105.             //evaluate word against the reversed variant
  106.             return word.Equals(reversed);
  107.         }
  108.  
  109.         /// <summary>
  110.         /// Returns whether or not the passed in number is a palindrome (excluding the full stop)
  111.         /// </summary>
  112.         /// <remarks>A palindrome reads the same forward or reversed</remarks>
  113.         public static bool NumberIsPalindrome(Decimal number)
  114.         {
  115.             return SentenceIsPalindrome(number.ToString());
  116.         }
  117.  
  118.         /// <summary>
  119.         /// Returns the nth number in the Fibonacci Sequence if the first 5 numbers in the sequence are 1, 1, 2, 3, 5, where n is the index passed into this method
  120.         /// </summary>
  121.         /// <remarks>By definition, the first two numbers in the Fibonacci sequence are 1 and 1, and each subsequent number is the sum of the previous two.</remarks>
  122.         public static int FibonacciSequence(int index)
  123.         {
  124.             if (index == null || index < 0)
  125.                 throw new ArgumentException("Invalid index passed to Fibonacci Calculator. Ensure your index is 0 or greater.");
  126.  
  127.             int[] fibonacci = new int[2] { 0, 1 };
  128.  
  129.             for (int i = 0; i < index; i++)
  130.             {
  131.                 int temp = fibonacci[0];
  132.                 fibonacci[0] = fibonacci[1];
  133.                 fibonacci[1] = temp + fibonacci[1];
  134.             }
  135.  
  136.             return fibonacci[0];
  137.         }
  138.  
  139.     }
  140. }
RAW Paste Data
Top