Created
June 27, 2016 19:57
-
-
Save jianminchen/c8501eabdb774710c100a1aa06049d8e to your computer and use it in GitHub Desktop.
Aor B HackerRank workout example - with bug fix - reverse the Hexadecimal string to begin compare from least significant bit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System | |
; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace AtoB | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//string fromValue = "80"; | |
//string orValue = "70"; | |
string output = ""; // should be "00" | |
//int no = updateHexaDecimal_Remove1s(fromValue, orValue, ref output); | |
int no2 = updateHexaDecimal_Remove1s("F1", "A70", ref output); // should be no2 = 2, output = "70" | |
} | |
/* | |
* HexaDecimal Char update | |
* For example, A = 1000 | |
* A | B = C, | |
* C = 0111 | |
* Then, 4th bit 1 in A should be set to 1. | |
* number is 1, A' = 0000 | |
* | |
* Another example, A = 1100, | |
* A | B = C, | |
* C = 0111, | |
* then, 4th bit 1 in A should be set to 0 | |
* function return is 1, A' = 0100 | |
* | |
* 1. count how many bits to update (only bit with value 1) | |
* 2. return A's new value A' | |
* 3. Work on bits from left to right, lowest significant bit first | |
* 4. assume the value > 0, and value < 10^ 50000 | |
* | |
* Work on HexaDecimal string, | |
* For example, D8, work on '8' first, 4 bits 1000, from right to left; | |
* and then, work on 'D', 1101 - 13 | |
* | |
* edge case: | |
* from = "8D", | |
* orValue = "E", | |
* orValue is short in length. | |
* | |
* This function design is questionable - need to work on later, make it as simple as possible | |
* | |
* Need to reverse the string to do comparison | |
* A | B = C | |
* A = "F1", B = "70" | |
* we need to reverse A and B | |
* 1F | |
* 07 | |
* and then, start to compare one char at a time, since A and B may have different length. | |
*/ | |
public static int updateHexaDecimal_Remove1s(string fromValue, string orValue, ref string output) | |
{ | |
int count = 0; | |
string fromReversed = Reverse(fromValue); | |
string orValueReversed = Reverse(orValue); | |
StringBuilder sb_Out = new StringBuilder(); | |
for (int i = 0; i < fromReversed.Length; i++) | |
{ | |
char[] twoToCompare = { fromReversed[i], orValueReversed[i] }; // need to double check - matching | |
int nFrom = hexaDecimalCharToInt(twoToCompare[0]); | |
int nOrValue = (i < orValueReversed.Length) ? hexaDecimalCharToInt(twoToCompare[1]) : 0; | |
string s1 = ""; | |
count += getBinaryStrInReverseOrder(nFrom, nOrValue, ref s1); | |
sb_Out.Append(convertBinaryStringToHexaDecimal(Reverse(s1))); | |
} | |
output = Reverse(sb_Out.ToString()); // bug001 - need to reverse the binary string | |
return count; | |
} | |
/* | |
* Make it well defined function - | |
* work on more! | |
* | |
*/ | |
public static int getBinaryStrInReverseOrder(int value, int targetValue, ref string output) | |
{ | |
if (value == 0) | |
{ | |
output = "0"; | |
return 0; | |
} | |
StringBuilder sb = new StringBuilder(); | |
int count = 0; | |
while (value > 0) | |
{ | |
int bitNo = value & 1; | |
int bitTarget = targetValue & 1; | |
if ((bitNo == 1 && bitTarget == 0)) | |
{ | |
count++; | |
bitNo = 0; | |
} | |
char c = (char)(bitNo + '0'); | |
sb.Append(c); | |
// right shift 1 bit | |
value = value >> 1; | |
targetValue = targetValue >> 1; | |
} | |
output = sb.ToString(); | |
return count; | |
} | |
/* | |
* input is string of binary number, at most 4 bits, | |
* for example, "1100" -> 12 -> 'C' | |
*/ | |
public static char convertBinaryStringToHexaDecimal(string s) | |
{ | |
int sum = 0; | |
foreach (char c in s) | |
{ | |
int value = (c - '0'); | |
sum = value + sum * 2; | |
} | |
if (sum >= 0 && sum <= 9) | |
return (char)(sum + '0'); | |
else | |
{ | |
string charStr = "ABCDEF"; | |
char[] charArr = charStr.ToCharArray(); | |
return charArr[sum - 10]; | |
} | |
} | |
/* | |
* Convert HexaDecimal char to integer | |
*/ | |
public static int hexaDecimalCharToInt(char c) | |
{ | |
int number = c - '0'; | |
if (number >= 0 && number <= 9) | |
return number; | |
else | |
{ | |
string charStr = "ABCDEF"; | |
char[] charArr = charStr.ToCharArray(); | |
int value = Array.IndexOf(charArr, c); | |
return 10 + value; | |
} | |
} | |
/* | |
* http://stackoverflow.com/questions/228038/best-way-to-reverse-a-string | |
*/ | |
public static string Reverse(string s) | |
{ | |
char[] charArray = s.ToCharArray(); | |
Array.Reverse(charArray); | |
return new string(charArray); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment