If I had a program to generate 1 billion numbers and output it to a text file. What is the best way to check if that file containing enough number? For example my output text file should be like below
1
2
....
1001
1002
1003
...
1000000
The auditor program should check if the file contains 1 billion rows and those number must be in ascending order
CodePudding user response:
store number in fixed length, may be 10 char separator.
number counter = file length / 11
CodePudding user response:
The normal approach would be just to read through the file and check this. The following pseudo-code(1) would be one approach:
expected = 1
for each line in file:
if line not equal to expected:
exit with failure indication
add one to expected
exit with success indication
This allows you to indicate failure immediately that a bad line is found, the only case where you have to process the entire file is if it's valid.
That algorithm is, of course, if your numbers need to be consecutive as well as ascending, which appears to be the case.
There are potential optimisations that could be used to pre-check things (such as the file being of a minimum size that will allow it to contain all the numbers). But, until you've got the functionality done correctly, I'd be holding off doing those.
(1) Keeping in mind that there are real-world issues you'll need to contend with when implementing in a real language. This includes things such as opening the file, reading each line into a variable, and converting between strings and integers.
I haven't covered that in detail since this question is tagged algorithm
.