如何解决乱码生成器
将字符串作为输入并根据提供的字典生成所有可能的有效单词(最少 3 个字符)的程序。 config:dictionary.txt(https://www.scrapmaker.com/download/data/wordlists/dictionaries/dictionary.txt) 包含每行单词列表。
示例: 输入:刀片 输出: 坏,小伙子,LED,ALE,... 秃头、铅、流血…… 废话,废话,..
在这个程序中,我正在获取一个文本文件并生成所有可能的单词,但我无法理解如何将文本文件与我的代码匹配并返回公共数组。
const filetoArr = async () => {
let arr = [];
let file = fetch("./dictionary.txt");
file = file.then(response => response.text());
arr = await file.then(line => line.split("\n"));
}
filetoArr();
function swap(chars,i,j) {
var tmp = chars[i];
chars[i] = chars[j];
chars[j] = tmp;
}
function getAnagrams(input) {
var counter = [],anagrams = [],chars = input.split(''),length = chars.length,i;
for (i = 0; i < length; i++) {
counter[i] = 0;
}
anagrams.push(input);
i = 0;
while (i < length) {
if (counter[i] < i) {
swap(chars,i % 2 === 1 ? counter[i] : 0,i);
counter[i]++;
i = 0;
anagrams.push(chars.join(''));
} else {
counter[i] = 0;
i++;
}
}
return anagrams;
}
const input = "BLADE";
let output = getAnagrams(input);
console.log(output);
function getCommon(arr1,arr2) {
var common = []; // Array to contain common elements
for(var i=0 ; i<arr1.length ; ++i) {
for(var j=0 ; j<arr2.length ; ++j) {
if(arr1[i] == arr2[j]) { // If element is in both the arrays
common.push(arr1[i]); // Push to common array
}
}
}
return common; // Return the common elements
}
getCommon(arr,output);
```
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。