Как равномерно распределить одинаковые строки в списке?

Поддержка
  • @Ilgiz said in Как равномерно распределить одинаковые строки в списке?:

    @Fox есть товары на маркетплейсе у каждого свой id

    Id в каком виде? Дай пример.

  • @sergerdn Судя по результату это то что предложил Fox, в этом случае строки кучкуются - три и два, потом три и один, не совсем подходит(

  • @sergerdn id просто цифры - 128546335

  • @Ilgiz said in Как равномерно распределить одинаковые строки в списке?:

    @sergerdn id просто цифры - 128546335

    Нужен ли каждый раз разный результат после запуска алгоритма распределения или подойдет статичный?

  • @sergerdn статичный подойдет, но входные данные (сами id, количество этих id) могут изменяться при новом запуске скрипта.

  • @Ilgiz said in Как равномерно распределить одинаковые строки в списке?:

    @sergerdn статичный подойдет, но входные данные (сами id, количество этих id) могут изменяться при новом запуске скрипта.

    Дай входные данные и выходные(желаемый результат), чтобы я не составлял их сам. Пожалуйста, оформи данные в виде массива Javascript.

  • @sergerdn
    Входные:
    let ids = [
    '1572238509:watercolor painting',
    '1572238509:watercolor painting',
    '1572238509:watercolor painting',
    '1572238509:watercolor painting',
    '1572238509:watercolor painting',
    '1572238509:watercolor painting',
    '1572238509:watercolor painting',
    '1572238509:watercolor painting',
    '1572238509:watercolor painting',
    '1572238509:watercolor painting',
    '1572224807:oil painting',
    '1572224807:oil painting',
    '1572224807:oil painting',
    '1572224807:oil painting',
    '1572224807:oil painting',
    '1530765154:botanical wall art'
    '1530765154:botanical wall art'
    '1530765154:botanical wall art'
    '1530765154:botanical wall art'
    '1530765154:botanical wall art'
    '1530765154:botanical wall art'
    '1530765154:botanical wall art'
    '1530765154:botanical wall art'
    '1530765154:botanical wall art'
    '1530765154:botanical wall art'
    '1530765154:botanical wall art'
    '1530765154:botanical wall art'
    '1530765154:botanical wall art'
    '1530765154:botanical wall art'
    '1530765154:botanical wall art'
    ];

    Выходные типа того:
    let ids = [
    '1530765154:botanical wall art'
    '1572224807:oil painting',
    '1530765154:botanical wall art'
    '1572238509:watercolor painting',
    '1530765154:botanical wall art'
    '1572238509:watercolor painting',
    '1530765154:botanical wall art'
    '1572238509:watercolor painting',
    '1572224807:oil painting',
    '1530765154:botanical wall art'
    '1572238509:watercolor painting',
    '1530765154:botanical wall art'
    '1572238509:watercolor painting',
    '1530765154:botanical wall art'
    '1572224807:oil painting',
    '1530765154:botanical wall art'
    '1572238509:watercolor painting',
    '1530765154:botanical wall art'
    '1572224807:oil painting',
    '1530765154:botanical wall art'
    '1572238509:watercolor painting',
    '1530765154:botanical wall art'
    '1572238509:watercolor painting',
    '1530765154:botanical wall art'
    '1572238509:watercolor painting',
    '1530765154:botanical wall art'
    '1572224807:oil painting',
    '1530765154:botanical wall art'
    '1572238509:watercolor painting',
    '1530765154:botanical wall art'
    ];

  • @Ilgiz

    1530765154:botanical wall art
    1572238509:watercolor painting
    1530765154:botanical wall art
    1572238509:watercolor painting
    1530765154:botanical wall art
    1572238509:watercolor painting
    1530765154:botanical wall art
    1572224807:oil painting
    1530765154:botanical wall art
    1572224807:oil painting
    1530765154:botanical wall art
    1572238509:watercolor painting
    1530765154:botanical wall art
    1572224807:oil painting
    1530765154:botanical wall art
    1572238509:watercolor painting
    1530765154:botanical wall art
    1572224807:oil painting
    1530765154:botanical wall art
    1572238509:watercolor painting
    1530765154:botanical wall art
    1572238509:watercolor painting
    1530765154:botanical wall art
    1572238509:watercolor painting
    1530765154:botanical wall art
    
    function isSameId(item1, item2) {
        return item1.split(':')[0] === item2.split(':')[0];
    }
    
    function shuffleArray(array) {
        let currentIndex = array.length,
            temporaryValue, randomIndex;
    
        // While there remain elements to shuffle...
        while (0 !== currentIndex) {
            // Pick a remaining element...
            randomIndex = Math.floor(Math.random() * currentIndex);
            currentIndex -= 1;
    
            // And swap it with the current element.
            temporaryValue = array[currentIndex];
            array[currentIndex] = array[randomIndex];
            array[randomIndex] = temporaryValue;
        }
    
        return array;
    }
    
    function distributeItems(items) {
        let attempts = 0;
        let maxAttempts = items.length * 10;
    
        // First, shuffle the array to get a random order
        let shuffledItems = shuffleArray(items.slice());
    
        for (let i = 0; i < shuffledItems.length - 1; i++) {
            if (isSameId(shuffledItems[i], shuffledItems[i + 1])) {
                let swapIndex = i + 2;
    
                while (swapIndex < shuffledItems.length && isSameId(shuffledItems[i], shuffledItems[swapIndex])) {
                    swapIndex++;
                }
    
                if (swapIndex < shuffledItems.length) {
                    // Swap the consecutive item with another non-consecutive item
                    let temp = shuffledItems[i + 1];
                    shuffledItems[i + 1] = shuffledItems[swapIndex];
                    shuffledItems[swapIndex] = temp;
                } else {
                    // If we've reached the end, we need to shuffle and start over
                    shuffledItems = shuffleArray(items.slice());
                    i = -1; // Start over from the first element after shuffling
                    attempts++;
                    if (attempts > maxAttempts) {
                        console.warn('Max attempts reached, distribution may not be perfect.');
                        break;
                    }
                }
            }
        }
    
        return shuffledItems;
    }
    
    let ids = [
        '1572238509:watercolor painting',
        '1572238509:watercolor painting',
        '1572238509:watercolor painting',
        '1572238509:watercolor painting',
        '1572238509:watercolor painting',
        '1572238509:watercolor painting',
        '1572238509:watercolor painting',
        '1572238509:watercolor painting',
        '1572238509:watercolor painting',
        '1572238509:watercolor painting',
        '1572224807:oil painting',
        '1572224807:oil painting',
        '1572224807:oil painting',
        '1572224807:oil painting',
        '1572224807:oil painting',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art'
    ];
    
    let evenlyDistributedArray = distributeItems(ids);
    
    console.log(evenlyDistributedArray.join('\n'));
    
  • @sergerdn said in Как равномерно распределить одинаковые строки в списке?:

    @Ilgiz

    1530765154:botanical wall art
    1572238509:watercolor painting
    1530765154:botanical wall art
    1572238509:watercolor painting
    1530765154:botanical wall art
    1572238509:watercolor painting
    1530765154:botanical wall art
    1572224807:oil painting
    1530765154:botanical wall art
    1572224807:oil painting
    1530765154:botanical wall art
    1572238509:watercolor painting
    1530765154:botanical wall art
    1572224807:oil painting
    1530765154:botanical wall art
    1572238509:watercolor painting
    1530765154:botanical wall art
    1572224807:oil painting
    1530765154:botanical wall art
    1572238509:watercolor painting
    1530765154:botanical wall art
    1572238509:watercolor painting
    1530765154:botanical wall art
    1572238509:watercolor painting
    1530765154:botanical wall art
    
    function isSameId(item1, item2) {
        return item1.split(':')[0] === item2.split(':')[0];
    }
    
    function shuffleArray(array) {
        let currentIndex = array.length,
            temporaryValue, randomIndex;
    
        // While there remain elements to shuffle...
        while (0 !== currentIndex) {
            // Pick a remaining element...
            randomIndex = Math.floor(Math.random() * currentIndex);
            currentIndex -= 1;
    
            // And swap it with the current element.
            temporaryValue = array[currentIndex];
            array[currentIndex] = array[randomIndex];
            array[randomIndex] = temporaryValue;
        }
    
        return array;
    }
    
    function distributeItems(items) {
        let attempts = 0;
        let maxAttempts = items.length * 10;
    
        // First, shuffle the array to get a random order
        let shuffledItems = shuffleArray(items.slice());
    
        for (let i = 0; i < shuffledItems.length - 1; i++) {
            if (isSameId(shuffledItems[i], shuffledItems[i + 1])) {
                let swapIndex = i + 2;
    
                while (swapIndex < shuffledItems.length && isSameId(shuffledItems[i], shuffledItems[swapIndex])) {
                    swapIndex++;
                }
    
                if (swapIndex < shuffledItems.length) {
                    // Swap the consecutive item with another non-consecutive item
                    let temp = shuffledItems[i + 1];
                    shuffledItems[i + 1] = shuffledItems[swapIndex];
                    shuffledItems[swapIndex] = temp;
                } else {
                    // If we've reached the end, we need to shuffle and start over
                    shuffledItems = shuffleArray(items.slice());
                    i = -1; // Start over from the first element after shuffling
                    attempts++;
                    if (attempts > maxAttempts) {
                        console.warn('Max attempts reached, distribution may not be perfect.');
                        break;
                    }
                }
            }
        }
    
        return shuffledItems;
    }
    
    let ids = [
        '1572238509:watercolor painting',
        '1572238509:watercolor painting',
        '1572238509:watercolor painting',
        '1572238509:watercolor painting',
        '1572238509:watercolor painting',
        '1572238509:watercolor painting',
        '1572238509:watercolor painting',
        '1572238509:watercolor painting',
        '1572238509:watercolor painting',
        '1572238509:watercolor painting',
        '1572224807:oil painting',
        '1572224807:oil painting',
        '1572224807:oil painting',
        '1572224807:oil painting',
        '1572224807:oil painting',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art',
        '1530765154:botanical wall art'
    ];
    
    let evenlyDistributedArray = distributeItems(ids);
    
    console.log(evenlyDistributedArray.join('\n'));
    

    Какую нейронку используете? :D

  • @Fox said in Как равномерно распределить одинаковые строки в списке?:

    Какую нейронку используете? :D

    Думаю, я не использую ничего уникального, а все то же самое, что и остальные.
    Вероятно, просто руки у меня немного другие😃

  • @sergerdn Спасибо, попробую разобраться как это в бас впихнуть)

  • @Ilgiz said in Как равномерно распределить одинаковые строки в списке?:

    @sergerdn Спасибо, попробую разобраться как это в бас впихнуть)

    Да не вопрос.

    distibuted_items.xml

    Script started with 1 threads and will finish after 1 success execution or 1 fail execution or when resource will finish.
    You can change these params in record mode.
    
    [792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
    [792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
    [792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
    [792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
    [792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
    [792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
    [792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
    [792848449] [03:55:57] Thread #1 : 1572224807:oil painting
    [792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
    [792848449] [03:55:57] Thread #1 : 1572224807:oil painting
    [792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
    [792848449] [03:55:57] Thread #1 : 1572224807:oil painting
    [792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
    [792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
    [792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
    [792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
    [792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
    [792848449] [03:55:57] Thread #1 : 1572224807:oil painting
    [792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
    [792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
    [792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
    [792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
    [792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
    [792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
    [792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
    [792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
    [792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
    [792848449] [03:55:57] Thread #1 : 1572224807:oil painting
    [792848449] [03:55:57] Thread #1 : 1530765154:botanical wall art
    [792848449] [03:55:57] Thread #1 : 1572238509:watercolor painting
    [03:55:57] Thread #1 : Thread succeeded with message "Ok"
    [03:55:57] Script finished correctly
    
  • @sergerdn Большое спасибо за помощь, то что нужно!