This Django application calculates the length of the longest substring without repeating characters, based on the logic implemented in an algorithm derived from a LeetCode problem. The solution to the problem can also be found in my LeetCode repository.
- Algorithm Integration: The logic for calculating the longest substring without repeating characters is implemented in
algorithm.py
and integrated into the Django app through theviews.py
file. - User Input: A simple HTML form allows users to input a string and calculate the result.
- Database Storage: Results are saved in the database for record-keeping and debugging purposes.
Initially, the app did not return the correct substring length. Here's the step-by-step debugging process that helped resolve the issue:
-
Algorithm Verification:
- The logic in
algorithm.py
was independently verified using unit tests to ensure correctness.
- The logic in
-
Django Debugging:
- The
DEBUG
setting insettings.py
was set toTRUE
:DEBUG = True
- Debugging messages were added in
views.py
to trace request methods and ensure the correct flow of data:print("Received Request: ", request.method)
- The
-
Root Cause Analysis:
- After testing, the issue was identified in the
HTML
template. The problem was in the line:<p>Longest Substring Length: {{ result.length_of_longest_substring }}</p>
- The variable name did not match the field in the
models.py
file.
- The variable name did not match the field in the
- After testing, the issue was identified in the
-
Fix:
- The incorrect variable name was corrected to:
<p>Longest Substring Length: {{ result.length_of_longest_substring }}</p>
- The incorrect variable name was corrected to:
algorithm.py
: Contains the logic for calculating the longest substring without repeating characters.views.py
: Integrates the algorithm with Django views and handles user requests.models.py
: Defines theSubstringResult
model for storing input strings and their corresponding results.templates/substring/index.html
: The HTML template for the user interface.
- Clone the repository:
git clone https://github.com/Mike014/LongestSubstringApp.git cd LongestSubstringApp
- Create and activate a virtual environment:
python3 -m venv venv
source venv/bin/activate
- Install dependencies:
pip install -r requirements.txt
- Run migrations:
python manage.py migrate
- Start the server:
python manage.py runserver
- The algorithm is based on my solution for the LeetCode problem linked above.
- Debugging was essential to ensure the application logic worked seamlessly from the algorithm to the user interface.