This project processes a list of integers from an input file and generates an output file containing unique integers. The integers in the output are sorted in increasing order. The project includes handling various edge cases, including invalid input, empty lines, non-integer values, and multiple integers on the same line.
The project follows strict memory and runtime efficiency requirements and avoids the use of built-in libraries for operations like sorting and list management.
/dsa/hw01/
│
├── code/
│ └── src/
│ └── UniqueInt.py # The main Python file containing the implementation
│
├── sample_inputs/ # Folder containing sample input files
│ └── small_sample_input_02.txt # Example input file
│
├── sample_results/ # Folder to store output files
│ └── small_sample_input_02.txt_results.txt # Example output file for the corresponding input
│
└── README.md # This README file
Read a list of integers from an input file. Eliminate duplicates. Handle special cases like whitespaces, multiple integers on a line, and invalid entries. Write the unique integers to an output file, sorted in increasing order.
The program can handle the following variations in the input:Whitespace: Lines with leading or trailing whitespaces around integers. Empty lines: Lines with no content or whitespaces only. Multiple integers on one line: These lines are skipped. Non-integer inputs: Lines with non-integer values such as alphabetic characters or symbols are skipped. Out-of-range integers: Only integers between -1023 and 1023 (inclusive) are processed.
UniqueInt Class:readNextItemFromFile(line): This method reads a line from the input file and extracts a valid integer if available. Invalid entries or lines with multiple integers are skipped. processFile(inputFilePath, outputFilePath): Reads the entire input file, checks for unique integers, sorts them manually, and writes them to the output file. run(inputFilePath, outputFilePath): Runs the processing while measuring execution time. Boolean Array for Tracking: A boolean array (seen) of size 2047 is used to track whether an integer has been encountered before. The array indexes represent numbers from -1023 to 1023.
Manual Sorting: A simple comparison-based sorting algorithm is used to avoid built-in sort functions.
1. Clone the Repository: Provide instructions for cloning the project if applicable.- Running the Code:
You can just navigate to the code/src/ directory. Run the Python script using the following command
./UniqueInt.py
Example usage: ./UniqueInt.py /dsa/hw01/sample_inputs/sample_input_02.txt /dsa/hw01/sample_results/sample_input_02_results.txt
- Output: The output file will be located in the sample_results/ folder with the same name as the input file, followed by _results.txt.
14
5
-9
62
-1
-9
-9
-9
-1
5
14
62