#coding interview question
1) Write a program for run-length encoding
for eg: AAABBCCCCC
is written as 3A2B5C
String RLE(String input)
{
if String.IsNullOrEmpty(input) return input;
int count = 0;
int prevChar = '/0';
StringBuilder output = "";
for (int i =0; i < input.Length; i++)
{
if (input[i] != prevChar){
if (prevChar != '/0') {
output += count.toString();
output += prevChar;
}
count = 1;
prevChar = input[i];
}else{
count+=1;
} // end if
} // end for
output += count.toString();
output += prevChar;
return output;
}
2) And in place encoding is as follows:
<work-in-progress>
Void RLE(ref StringBuilder input)
{
if String.IsNullOrEmpty(input.toString ()) return;
int count = 0;
int prev = -1; // previous index
int prevCount = 0;
Char prevChar = '/0';
String readOnlyCopy = input.SubString(0,1); // just the first bRead to get started
int countLen = count.ToString().Length;
int i = 0; // index to span reading the length of the string
bool readAhead = false;
while ( i < input.Length && readOnlyCopy.Length > 0)
{
if (prev == -1 || input[i] != input[prev] ){
if (prev != -1) {
prevChar = input [prev];
prevCount = count;
countLen = count.toString().Length; // actually we need fixed size packing say 4 bytes for number and one byte for char but the processing is similar
if (countLen >= count){
readOnlyCopy += input.subString (i+1, countLen); // to include count and char
readAhead = true;
}
Int w = prev;
If (prev==-1) w++;
If (countLen > input.Length - i ) input.resize (input.Length×2);
For (int j=0; j < countLen; j++){
Input[w]=count.toString ()[j];
w++;
}
Input [w] = prevChar;
// i = i + countLen;
readOnlyCopy.TrimLeft(0, countLen);
}
count = 1;
prev = i;
prevChar = input [prev];
Countlen=1;
}else{
count+=1;
} // end if
i++;
if (readahead == false)
readOnlyCopy += input.SubString(i, 1); // next bRead
else
readahead=false;
} // end while
// one last while lookahead
int w = prev;
if (w + count.toString().Length + 2 > input.Length) input.resize(input.Length *2 );
countLen = count.toString().Length;
for (int j = 0; j < countLen; j++){
Input[w] = count.toString()[j];
w++;
}
Input[w] = prevChar;
I = i + countLen;
w++;
Input[w] = '/0';
return ;
}
1) Write a program for run-length encoding
for eg: AAABBCCCCC
is written as 3A2B5C
String RLE(String input)
{
if String.IsNullOrEmpty(input) return input;
int count = 0;
int prevChar = '/0';
StringBuilder output = "";
for (int i =0; i < input.Length; i++)
{
if (input[i] != prevChar){
if (prevChar != '/0') {
output += count.toString();
output += prevChar;
}
count = 1;
prevChar = input[i];
}else{
count+=1;
} // end if
} // end for
output += count.toString();
output += prevChar;
return output;
}
2) And in place encoding is as follows:
<work-in-progress>
Void RLE(ref StringBuilder input)
{
if String.IsNullOrEmpty(input.toString ()) return;
int count = 0;
int prev = -1; // previous index
int prevCount = 0;
Char prevChar = '/0';
String readOnlyCopy = input.SubString(0,1); // just the first bRead to get started
int countLen = count.ToString().Length;
int i = 0; // index to span reading the length of the string
bool readAhead = false;
while ( i < input.Length && readOnlyCopy.Length > 0)
{
if (prev == -1 || input[i] != input[prev] ){
if (prev != -1) {
prevChar = input [prev];
prevCount = count;
countLen = count.toString().Length; // actually we need fixed size packing say 4 bytes for number and one byte for char but the processing is similar
if (countLen >= count){
readOnlyCopy += input.subString (i+1, countLen); // to include count and char
readAhead = true;
}
Int w = prev;
If (prev==-1) w++;
If (countLen > input.Length - i ) input.resize (input.Length×2);
For (int j=0; j < countLen; j++){
Input[w]=count.toString ()[j];
w++;
}
Input [w] = prevChar;
// i = i + countLen;
readOnlyCopy.TrimLeft(0, countLen);
}
count = 1;
prev = i;
prevChar = input [prev];
Countlen=1;
}else{
count+=1;
} // end if
i++;
if (readahead == false)
readOnlyCopy += input.SubString(i, 1); // next bRead
else
readahead=false;
} // end while
// one last while lookahead
int w = prev;
if (w + count.toString().Length + 2 > input.Length) input.resize(input.Length *2 );
countLen = count.toString().Length;
for (int j = 0; j < countLen; j++){
Input[w] = count.toString()[j];
w++;
}
Input[w] = prevChar;
I = i + countLen;
w++;
Input[w] = '/0';
return ;
}
Are you trying to earn money from your websites by using popup advertisments?
ReplyDeleteIn case you are, did you try using Propeller Ads?