Skip to content

Instantly share code, notes, and snippets.

@saeedvir
Created November 12, 2025 03:12
Show Gist options
  • Select an option

  • Save saeedvir/b449c12aa68857c559e719fd15389b29 to your computer and use it in GitHub Desktop.

Select an option

Save saeedvir/b449c12aa68857c559e719fd15389b29 to your computer and use it in GitHub Desktop.
Shuffle and Reconstruct String (3 way)
// method 1
const mystring = "abcde";
// Split into 4 sections (since string length is 5, one section will be empty)
function splitIntoFour(str) {
const sectionSize = Math.ceil(str.length / 4);
const sections = [];
for (let i = 0; i < 4; i++) {
const start = i * sectionSize;
const end = start + sectionSize;
sections.push({
content: str.substring(start, end),
originalIndex: i
});
}
return sections;
}
// Shuffle the sections
function shuffleSections(sections) {
for (let i = sections.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[sections[i], sections[j]] = [sections[j], sections[i]];
}
return sections;
}
// Reconstruct original string
function reconstructString(shuffledSections) {
// Sort by original index to get back original order
shuffledSections.sort((a, b) => a.originalIndex - b.originalIndex);
return shuffledSections.map(section => section.content).join('');
}
// Usage
const sections = splitIntoFour(mystring);
console.log("Original sections:", sections.map(s => s.content));
const shuffled = shuffleSections([...sections]); // Create a copy to shuffle
console.log("Shuffled sections:", shuffled.map(s => s.content));
const reconstructed = reconstructString(shuffled);
console.log("Reconstructed string:", reconstructed);
@saeedvir
Copy link
Author

// method 2

const mystring = "abcde";

function splitAndShuffle(str) {
    const sectionSize = Math.ceil(str.length / 4);
    const sections = [];
    const indices = [0, 1, 2, 3];
    
    // Split into sections
    for (let i = 0; i < 4; i++) {
        const start = i * sectionSize;
        const end = start + sectionSize;
        sections.push(str.substring(start, end));
    }
    
    // Shuffle indices to remember the mapping
    for (let i = indices.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [indices[i], indices[j]] = [indices[j], indices[i]];
    }
    
    // Apply shuffle to sections
    const shuffledSections = indices.map(idx => sections[idx]);
    
    return {
        shuffled: shuffledSections,
        mapping: indices, // Store how to reverse the shuffle
        original: sections
    };
}

function reconstructFromShuffle(shuffleResult) {
    const { shuffled, mapping } = shuffleResult;
    const reconstructed = new Array(4);
    
    // Reverse the shuffle using the mapping
    mapping.forEach((originalIdx, shuffledIdx) => {
        reconstructed[originalIdx] = shuffled[shuffledIdx];
    });
    
    return reconstructed.join('');
}

// Usage
const result = splitAndShuffle(mystring);
console.log("Original:", mystring);
console.log("Shuffled:", result.shuffled);
console.log("Reconstructed:", reconstructFromShuffle(result));

@saeedvir
Copy link
Author

// method 3

const mystring = "abcde";

function splitString(str) {
    const sectionSize = Math.ceil(str.length / 4);
    const sections = [];
    
    for (let i = 0; i < 4; i++) {
        const start = i * sectionSize;
        const end = start + sectionSize;
        sections.push(str.substring(start, end));
    }
    return sections;
}

function shuffleWithTracking(array) {
    const tracked = array.map((item, index) => ({ item, index }));
    const shuffled = [...tracked];
    
    // Fisher-Yates shuffle
    for (let i = shuffled.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
    }
    
    return {
        shuffledArray: shuffled.map(obj => obj.item),
        originalIndices: shuffled.map(obj => obj.index)
    };
}

function reconstruct(shuffledResult) {
    const { shuffledArray, originalIndices } = shuffledResult;
    const reconstructed = new Array(shuffledArray.length);
    
    originalIndices.forEach((originalIdx, currentIdx) => {
        reconstructed[originalIdx] = shuffledArray[currentIdx];
    });
    
    return reconstructed.join('');
}

// Usage
const sections = splitString(mystring);
console.log("Original sections:", sections);

const shuffledResult = shuffleWithTracking(sections);
console.log("Shuffled sections:", shuffledResult.shuffledArray);

const original = reconstruct(shuffledResult);
console.log("Reconstructed:", original);

@saeedvir
Copy link
Author

فرض کنید یک رشته حداقل پنج کارکتری دارید به صورت

abcde
ابتدا این رشته را به چهار قسمت تقسیم کند و در یک آرایه بریزید

بعد ایندکس های آرایه را به صورت تصادفی به هم بریزد

حالا روشی طراحی کنید که بعد از این به هم ریختن بتوانید دوباره این رشته را بازیابی کنید.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment