create a webpage that generates binary (0,1) De Brujin sequences of a given, user…
- User
create a webpage that generates binary (0,1) De Brujin sequences of a given, user inputed, length. Use only html, javascript etc. - all code in one page ready to copy and paste.
- Assistant
Sure! Below is a complete HTML page that generates binary De Bruijn sequences of a user-inputted length. You can copy and paste this code into a
.htmlfile and open it in a web browser to use the sequence generator. It uses only HTML and JavaScript, all within one page.<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Binary De Bruijn Sequence Generator</title> <style> body { font-family: Arial, sans-serif; margin: 40px; } input, button, textarea { font-size: 16px; } textarea { width: 100%; height: 100px; margin-top: 10px; } </style> </head> <body> <h1>Binary De Bruijn Sequence Generator</h1> <p>Enter the order (length) <strong>n</strong> of the De Bruijn sequence:</p> <input type="number" id="orderInput" min="1" value="3"> <button onclick="generateSequence()">Generate</button> <h2>Generated De Bruijn Sequence:</h2> <textarea id="sequenceOutput" readonly></textarea> <script> // Function to generate De Bruijn sequence of order n over k symbols function deBruijn(k, n) { var a = [], sequence = []; for (var i = 0; i < k * n; i++) a[i] = 0; function db(t, p) { if (t > n) { if (n % p === 0) { for (var i = 1; i <= p; i++) sequence.push(a[i]); } } else { a[t] = a[t - p]; db(t + 1, p); for (var j = a[t - p] + 1; j < k; j++) { a[t] = j; db(t + 1, t); } } } db(1, 1); return sequence.join(''); } // Function to handle sequence generation and display function generateSequence() { var n = parseInt(document.getElementById('orderInput').value); if (isNaN(n) || n <= 0) { alert('Please enter a positive integer for the order.'); return; } var sequence = deBruijn(2, n); document.getElementById('sequenceOutput').value = sequence; } </script> </body> </html>Instructions:
- Copy the entire code above.
- Paste it into a text editor (like Notepad, TextEdit, or any code editor).
- Save the file with an
.htmlextension, for example,debruijn_generator.html. - Open the saved
.htmlfile in a web browser (like Chrome, Firefox, or Edge).
How to Use the Generator:
- Enter the desired order (length)
nof the De Bruijn sequence into the input field. - Click the "Generate" button.
- The generated binary De Bruijn sequence will appear in the text area below.
Example:
- Order
n: 3 - Generated Sequence:
00010111
This sequence is a De Bruijn sequence of order 3 over the binary alphabet
{0,1}. It contains every possible substring of length 3 exactly once.Explanation:
The JavaScript function
deBruijn(k, n)generates a De Bruijn sequence for an alphabet of sizekand subsequences of lengthn. In this case, we setk = 2for the binary alphabet{0,1}.The function uses a recursive approach to build the sequence, ensuring that every possible substring of length
nappears exactly once in the generated sequence.Feel free to use and modify this code according to your needs!