Can someone pls help me with a little algorithm script

Joined
Nov 28, 2024
Messages
1
Reaction score
0
hey there community,


just signed up because i’m trying all day to create a script in HTML/JS with ChatGPT. It works quite a bit already but can’t tune it the way I need it.

here is what I’m trying to create, a text generator which follows these rules:

-the string (Z) persists of a 3 random lowercase letter combination (X) and a domain ending (D) (e.g. “eoo . de”)
-(X) contains a double letter (e. g. “oo”)
-(X) only persists of 3 lowercase letters
-(X) can’t persist of 3 equal letters (e. g. “ooo . de” is not correct)
-(X) has a minimum of 1 letter which (D) persists of (e. g. domain .de: “eoo . de” is correct, “coo . de” is not correct)
-no duplicates in string (Z), only unique combinations
-domains (D) can be: "ac", "ad", "ae", "af", "ag", "ai", "al", "am", "ao", "aq", "ar", "as", "at", "au", "aw", "ax", "az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bl", "bm", "bn", "bo", "bq", "br", "bs", "bt", "bv", "bw", "by", "bz", "ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm", "cn", "co", "cr", "cu", "cv", "cw", "cx", "cy", "cz", "de", "dj", "dk", "dm", "do", "dz", "ec", "ee", "eg", "eh", "er", "es", "et", "eu", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gb", "gd", "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu", "id", "ie", "il", "im", "in", "io", "iq", "ir", "is", "it", "je", "jm", "jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "me", "mf", "mg", "mh", "mk", "ml", "mm", "mn", "mo", "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na", "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om", "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "ps", "pt", "pw", "py", "qa", "re", "ro", "rs", "ru", "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so", "sr", "ss", "st", "sv", "sx", "sy", "sz", "tc", "td", "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn", "to", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "uk", "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn", "vu", "wf", "ws", "ye", "yt", "za", "zm", "zw"

-the generator should show checkboxes for each (D) for the user to choose which (D) are used in generation of (Z), if none are selected, generate for all (D)
-the result of generation should be displayed in a table, 1 column for each chosen (D) (try the code from chatgpt to see),
-the ammount of max. possible commbinations should be displayed in the table for each (D)
-there should be a textbox where the user can choose how many (Z) are generated. default should be 10, when entered “0” it should generate all possible combinations
-add a “magic mode” check box, the generator then will only use the letters of (D) to generate (X)

thank you very much for your help. I know this is a script nobody needs, but I’m looking forward to read the code and understand how others are coding.
code from chatgpt:
Code:
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Zeichenfolgen-Generator</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            line-height: 1.6;
            margin: 20px;
        }
        button {
            margin: 10px 0;
            padding: 10px 15px;
            font-size: 16px;
            cursor: pointer;
        }
        .results {
            margin-top: 20px;
        }
        .checkbox-group {
            margin-bottom: 10px;
            display: table;
            width: 100%;
        }
        .checkbox-group label {
            display: inline-block;
            margin: 1px 2px;
            text-align: left;
            width: auto;
        }
        .checkbox-group .checkbox-row {
            display: table-row;
        }
        .checkbox-group .checkbox-cell {
            display: table-cell;
            padding: 0px;
        }
        #selectAll {
            margin-bottom: 10px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            padding: 10px;
            text-align: left;
            border: 1px solid #ddd;
        }
    </style>
</head>
<body>
    <h1>Zeichenfolgen-Generator</h1>
    <p>
        Wählen Sie die Domain-Endungen aus und klicken Sie auf "Generiere Zeichenfolgen", um alle möglichen Zeichenfolgen zu erstellen.
    </p>

    <!-- Checkboxen für Domain-Endungen -->
    <div class="checkbox-group">
        <div class="checkbox-row">
            <label><input type="checkbox" id="selectAll" onclick="toggleSelectAll()"> Alle auswählen</label>
        </div>
        <div id="domainCheckBoxes"></div>
    </div>

    <button onclick="generateStrings()">Generiere Zeichenfolgen</button>

    <!-- Tabelle zur Anzeige der Ergebnisse -->
    <div class="results" id="results"></div>

    <script>
        // Liste der Domain-Endungen (D)
        const domainEndings = [
            "ac", "ad", "ae", "af", "ag", "ai", "al", "am", "ao", "aq", "ar", "as", "at", "au", "aw", "ax", "az", "ba",
            "bb", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bl", "bm", "bn", "bo", "bq", "br", "bs", "bt", "bv", "bw",
            "by", "bz", "ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm", "cn", "co", "cr", "cu", "cv", "cw",
            "cx", "cy", "cz", "de", "dj", "dk", "dm", "do", "dz", "ec", "ee", "eg", "eh", "er", "es", "et", "eu", "fi",
            "fj", "fk", "fm", "fo", "fr", "ga", "gb", "gd", "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", "gq",
            "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu", "id", "ie", "il", "im", "in", "io",
            "iq", "ir", "is", "it", "je", "jm", "jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky",
            "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "me", "mf", "mg",
            "mh", "mk", "ml", "mm", "mn", "mo", "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na",
            "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om", "pa", "pe", "pf", "pg", "ph", "pk",
            "pl", "pm", "pn", "pr", "ps", "pt", "pw", "py", "qa", "re", "ro", "rs", "ru", "rw", "sa", "sb", "sc", "sd",
            "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so", "sr", "ss", "st", "sv", "sx", "sy", "sz", "tc",
            "td", "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn", "to", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "uk",
            "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn", "vu", "wf", "ws", "ye", "yt", "za", "zm", "zw"
        ];

        // Funktion, um die Checkboxen für die Domain-Endungen zu generieren
        function generateDomainCheckboxes() {
            const checkboxesDiv = document.getElementById("domainCheckBoxes");
            let rowDiv = document.createElement('div');
            rowDiv.classList.add('checkbox-row');
            let counter = 0;

            domainEndings.forEach(domain => {
                if (counter === 20) {
                    checkboxesDiv.appendChild(rowDiv);
                    rowDiv = document.createElement('div');
                    rowDiv.classList.add('checkbox-row');
                    counter = 0;
                }

                const checkboxLabel = document.createElement("label");
                checkboxLabel.innerHTML = `<input type="checkbox" class="domain-checkbox" value="${domain}"> .${domain}</label>`;
                const checkboxCell = document.createElement('div');
                checkboxCell.classList.add('checkbox-cell');
                checkboxCell.appendChild(checkboxLabel);
                rowDiv.appendChild(checkboxCell);
                counter++;
            });

            if (rowDiv.innerHTML) {
                checkboxesDiv.appendChild(rowDiv);
            }
        }

        // Funktion, um eine gültige Kombination zu erzeugen
        function generateCombination(domain) {
            const letters = "abcdefghijklmnopqrstuvwxyz";
            const combinations = new Set();

            // Durch alle Buchstabenpaare iterieren, um Kombinationen zu erstellen
            for (let i = 0; i < letters.length; i++) {
                for (let j = 0; j < letters.length; j++) {
                    if (letters[i] === letters[j]) continue; // Keine drei gleichen Buchstaben
                    for (let k = 0; k < letters.length; k++) {
                        let combination;

                        // Doppelbuchstabe am Anfang oder Ende
                        if (letters[i] === letters[j]) {
                            combination = letters[i] + letters[i] + letters[k]; // Doppelbuchstabe am Anfang
                        } else if (letters[j] === letters[k]) {
                            combination = letters[i] + letters[j] + letters[j]; // Doppelbuchstabe am Ende
                        } else {
                            continue; // Keine gültige Kombination ohne Doppelbuchstaben
                        }

                        // Sicherstellen, dass mindestens ein Buchstabe der Domain-Endung enthalten ist
                        if (domain.split('').some(letter => combination.includes(letter))) {
                            combinations.add(combination);
                        }
                    }
                }
            }

            return Array.from(combinations);
        }

        // Funktion, um alle Zeichenfolgen zu generieren und sie in der Tabelle anzuzeigen
        function generateStrings() {
            const selectedDomains = getSelectedDomains();
            const results = {};

            selectedDomains.forEach((domain) => {
                results[domain] = generateCombination(domain);
            });

            // Ergebnisse in Tabelle umwandeln und anzeigen
            const resultsDiv = document.getElementById("results");
            let tableHTML = "<table><thead><tr>";

            // Spaltenüberschriften erstellen
            selectedDomains.forEach((domain) => {
                tableHTML += `<th>${domain}</th>`;
            });
            tableHTML += "</tr></thead><tbody>";

            // Anzahl der Zeilen festlegen
            const maxRows = Math.max(...selectedDomains.map(domain => results[domain].length));
            for (let i = 0; i < maxRows; i++) {
                tableHTML += "<tr>";
                selectedDomains.forEach((domain) => {
                    tableHTML += `<td>${results[domain][i] ? results[domain][i] + '.' + domain : ''}</td>`;
                });
                tableHTML += "</tr>";
            }

            tableHTML += "</tbody></table>";
            resultsDiv.innerHTML = tableHTML;
        }

        // Funktion, um alle selektierten Domain-Endungen zu erhalten
        function getSelectedDomains() {
            const checkboxes = document.querySelectorAll('.domain-checkbox');
            const selectedDomains = [];
            checkboxes.forEach((checkbox) => {
                if (checkbox.checked) {
                    selectedDomains.push(checkbox.value);
                }
            });

            return selectedDomains.length === 0 ? domainEndings : selectedDomains;
        }

        // Funktion, um alle Domain-Endungen auszuwählen oder abzuwählen
        function toggleSelectAll() {
            const isChecked = document.getElementById("selectAll").checked;
            const checkboxes = document.querySelectorAll('.domain-checkbox');
            checkboxes.forEach((checkbox) => {
                checkbox.checked = isChecked;
            });
        }

        // Initialisieren
        generateDomainCheckboxes();
    </script>
</body>
</html>
 
Joined
Sep 21, 2022
Messages
182
Reaction score
25
If the domain has two different letters, there are 294 possible X.

If the domain is a double, there are 150 possible X.

It doesn't matter what the letters are, the count is the same.

Because the numbers are so low, if I was doing it, I would generate them all, shuffle the list, then take as many as I needed from the top.

You don't need three nested loops, you only need one loop.

a and b come from D, c needs the loop.
Code:
domain .ab
----------

aab   6 patterns
aba
baa   magic
bba
bab
abb

aac  bbc   12 patterns
aca  bcb
caa  cbb   c != a
acc  bcc   c != b
cac  cbc
cca  ccb   24 choices for c

domain .aa
----------

aac   6 patterns
aca
caa   25 choices for c
acc
cac
cca
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,029
Messages
2,570,346
Members
46,983
Latest member
stevesmith27

Latest Threads

Top