Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Reverse String and Binary Search of floating point values
//Tableau Interview Homework //Rextester.Program.Main is the entry point for your code. Don't change it. using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { Console.WriteLine("Thanks for the opportunity!"); Console.WriteLine(); Console.WriteLine("Reverse string - tests:"); Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~"); Console.WriteLine("Time complexity: O(n/2) and Space complexity: O(n), where n is the number of characters in the string. Space complexity would be O(1) if the input is given as a char[]."); Console.WriteLine(); Assert("I am sam." == ReverseString(".mas ma I"), "reverses a small sentence"); Assert("ab" == ReverseString("ba"), "reverses a 2 letter string"); Assert("a" == ReverseString("a"), "reverses a 1 letter string"); Assert(string.Empty == ReverseString(null), "returns string.Empty for null"); Assert(string.Empty == ReverseString(string.Empty), "returns string.Empty for string.Empty"); Assert("அஆஇஈஉ" == ReverseString("உஈஇஆஅ"), "handles unicode characters"); Console.WriteLine(); Console.WriteLine("Binary search of floating point values - tests:"); Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); Console.WriteLine("Time complexity: O(log n) and Space complexity: O(1), where n is the number of elements in the input array."); Console.WriteLine(); double[] input = new double[] {-0.989, -0.989999, -0.990001, 0.999999, 1.0}; Array.Sort(input); //sort to save yourself from the test being wrong Assert(2 == FindClosestMatchIndex(input, -0.990001), "finds the straight match"); Assert(1 == FindClosestMatchIndex(input, -0.989989), "finds when greater is the closest"); Assert(3 == FindClosestMatchIndex(input, 0.9999991),"finds when lower is the closest"); Assert(0 == FindClosestMatchIndex(input, -9999999),"finds when the first is the closest by being greater"); Assert(0 == FindClosestMatchIndex(input, -0.991),"finds when the first is the closest by being lesser"); Assert(4 == FindClosestMatchIndex(input, 0.9999999),"finds when the last is the closest by being greater"); Assert(4 == FindClosestMatchIndex(input, 99999999),"find swhen the last is the closest by being lesser"); Assert(-1 == FindClosestMatchIndex(new double[0], 99999999),"returns -1 when the array is empty"); Console.WriteLine(); Console.WriteLine("Binary search of LARGE, PRECISE, DUPLICATE floating point values - tests:"); Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); Console.WriteLine(); double[] precise = new double[] {5.0 * Math.Pow(10, -324), 5.0 * Math.Pow(10, -324), 1.6 * Math.Pow(10,308), 1.7 * Math.Pow(10,308), 1.7 * Math.Pow(10,308)}; Array.Sort(input); //sort to save yourself from the test being wrong Assert(2 == FindClosestMatchIndex(precise, 1.6 * Math.Pow(10,308)), "finds the straight match among large, precise numbers"); Assert(1 == FindClosestMatchIndex(precise, 5.0 * Math.Pow(10, -324)), "finds the highest index one when duplicate exists - test #1"); Assert(4 == FindClosestMatchIndex(precise, 1.7 * Math.Pow(10,308)),"finds the highest index one when duplicate exists - test #2"); } public static void Assert(bool result, string message) { Console.WriteLine("Assert:{0}. {1}", result ? "Pass":"Fail", message); } public static string ReverseString(string text) { if (string.IsNullOrEmpty(text)) return string.Empty; char[] textArray = text.ToCharArray(); for(int i = 0, j = textArray.Length-1; i < j; i++, j--) { char temp = textArray[i]; textArray[i] = textArray[j]; textArray[j] = temp; } return new string(textArray); } //binary search public static int FindClosestMatchIndex(double[] sortedArray, double number) { if (sortedArray.Length == 0) return -1; int left=0; int right = sortedArray.Length - 1; //find the closest range while ((right - left) > 1) { int mid = left + (right - left)/2; if (sortedArray[mid] > number) right = mid; else left = mid; } double diff1 = number - sortedArray[left]; double diff2 = sortedArray[right] - number; int closestIndex; //closest is the one with the lesser difference if (diff1 < diff2) closestIndex = left; else closestIndex = right; return closestIndex; } } }
run
|
edit
|
history
|
help
0
array print 2nd half first and 1 half later
Sum Matrix Column
Fórum Parallel Threads ( Without Fun )
QLSV
งานสอบ
Date Comparison
ashok
jjj2
Invoke method via reflection
Bases5