Created
November 12, 2025 03:12
-
-
Save saeedvir/b449c12aa68857c559e719fd15389b29 to your computer and use it in GitHub Desktop.
Shuffle and Reconstruct String (3 way)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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); |
Author
saeedvir
commented
Nov 12, 2025
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);
Author
فرض کنید یک رشته حداقل پنج کارکتری دارید به صورت
abcde
ابتدا این رشته را به چهار قسمت تقسیم کند و در یک آرایه بریزید
بعد ایندکس های آرایه را به صورت تصادفی به هم بریزد
حالا روشی طراحی کنید که بعد از این به هم ریختن بتوانید دوباره این رشته را بازیابی کنید.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment