Relevant and Efficient parsing of data from raw files is of utmost importance to generate valuable information.
Here, we’ll see how to parse numbers from a file that are strung together without any delimiters.
We’ll first generate a file containing a large set of consecutive numbers starting from 1. These numbers are present continuously without any delimiters.
The first task is to parse the huge string to store every number as a member of a List of Strings. We’ll go through the concepts of number-lookup, usage of the stream-buffer and declaration of a string containing a repeated character.
Next, we’ll also efficiently parse a file containing numbers without delimiters that aren’t necessarily consecutive. For this, we’ll have certain preconditions set up.
Please check out the 5 video links where I go through this simulated Interview in more detail.
C# Experiments: Raining Numbers (Part 1)
The code typed-in during the interview series is as follows for your reference:-
//0123456789101112... 20000
//0,1,2,3,4,5,6,7,8,9,10,11,...,19999,20000
//using (var sw = new StreamWriter(@"C:\Users\Sandeep\Desktop\numbers.txt"))
//{
// for (int i = 0; i <= 20000; i++)
// sw.Write(i);
//}
var rainingNumbers = new List<string>();
var digitBuffer = new Char[10];
int size = 1;
string tempStr = null;
//var numberSizeLookUp = new List<string> { "9", "99", "999", "9999", "99999", "999999", "9999999" };
//using (var sr = new StreamReader(@"C:\Users\Sandeep\Desktop\numbers.txt"))
//{
// while (!sr.EndOfStream)
// {
// sr.Read(digitBuffer, 0, size);
// //tempStr = new String(digitBuffer, 0, size);
// rainingNumbers.Add(new String(digitBuffer, 0, size));
// //if (tempStr.Equals(new String('9', size)))
// if (rainingNumbers.Last().Equals(numberSizeLookUp[size - 1]))
// size++;
// }
//}
//811179013456798098009981
//using (var sw = new StreamWriter(@"C:\Users\Sandeep\Desktop\tricky numbers.txt"))
//{
// sw.Write("811179013456798099009981");
//}
using (var sr = new StreamReader(@"C:\Users\Sandeep\Desktop\tricky numbers.txt"))
{
while (!sr.EndOfStream)
{
sr.Read(digitBuffer, 0, size);
tempStr = new String(digitBuffer, 0, size);
if (rainingNumbers.Count != 0)
{
if (Convert.ToDouble(tempStr) <= Convert.ToDouble(rainingNumbers.Last()))
{
sr.Read(digitBuffer, 0, 1);
tempStr = String.Concat(tempStr, new String(digitBuffer, 0, 1));
rainingNumbers.Add(tempStr);
size++;
}
else
rainingNumbers.Add(tempStr);
}
else
rainingNumbers.Add(tempStr);
}
}
//0,1,2,3,4,5,6,7,8,9,10,11,...,19999,20000
//using (var sw = new StreamWriter(@"C:\Users\Sandeep\Desktop\numbers.txt"))
//{
// for (int i = 0; i <= 20000; i++)
// sw.Write(i);
//}
var rainingNumbers = new List<string>();
var digitBuffer = new Char[10];
int size = 1;
string tempStr = null;
//var numberSizeLookUp = new List<string> { "9", "99", "999", "9999", "99999", "999999", "9999999" };
//using (var sr = new StreamReader(@"C:\Users\Sandeep\Desktop\numbers.txt"))
//{
// while (!sr.EndOfStream)
// {
// sr.Read(digitBuffer, 0, size);
// //tempStr = new String(digitBuffer, 0, size);
// rainingNumbers.Add(new String(digitBuffer, 0, size));
// //if (tempStr.Equals(new String('9', size)))
// if (rainingNumbers.Last().Equals(numberSizeLookUp[size - 1]))
// size++;
// }
//}
//811179013456798098009981
//using (var sw = new StreamWriter(@"C:\Users\Sandeep\Desktop\tricky numbers.txt"))
//{
// sw.Write("811179013456798099009981");
//}
using (var sr = new StreamReader(@"C:\Users\Sandeep\Desktop\tricky numbers.txt"))
{
while (!sr.EndOfStream)
{
sr.Read(digitBuffer, 0, size);
tempStr = new String(digitBuffer, 0, size);
if (rainingNumbers.Count != 0)
{
if (Convert.ToDouble(tempStr) <= Convert.ToDouble(rainingNumbers.Last()))
{
sr.Read(digitBuffer, 0, 1);
tempStr = String.Concat(tempStr, new String(digitBuffer, 0, 1));
rainingNumbers.Add(tempStr);
size++;
}
else
rainingNumbers.Add(tempStr);
}
else
rainingNumbers.Add(tempStr);
}
}
Thank you for reading this post and watching the videos. Please Subscribe, Comment and Rate the channel if you liked the video.
Goto C# Experiments to
access more of such content! Thanks again!
No comments:
Post a Comment