使用字符串搜索在多维数组中查找坐标
Find a coordinate in a multidimensional array using string search
尝试在没有循环的情况下给出这个问题的替代方案!仅使用 indexOf 和一些整数数学
下面的代码看起来很有前途但失败了。
谁有更好的数学技能想要解决它?
var letterVariations = [
[' ','0','1','2','3','4','5','6','7','8','9'],
['A','a','B','b','C','c','D','d','E','e',';'],
['Â','â','F','f','G','g','H','h','Ê','ê',':'],
['À','à','I','i','J','j','K','k','È','è','.'],
['L','l','Î','î','M','m','N','n','É','é','?'],
['O','o','Ï','ï','P','p','Q','q','R','r','!'],
['Ô','ô','S','s','T','t','U','u','V','v','“'],
['W','w','X','x','Y','y','Ù','ù','Z','z','”'],
['@','&','#','[','(','/',')',']','+','=','-'],
];
var string = JSON.stringify(letterVariations);
var pos = string.indexOf("u")
console.log(Math.floor((pos/10)%8),pos%10)
// fails, how to fix?
pos = string.indexOf("M")
console.log(Math.floor((pos/10)%8),pos%10)
function findPos(array, symbol) {
const string = array.toString().replace(/,/g, '');
const pos = string.indexOf(symbol)
const d = (array[0] || []).length
const x = pos % d;
const y = Math.floor(pos / d)
return { x, y }
}
const array = [
[' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', ';'],
['Â', 'â', 'F', 'f', 'G', 'g', 'H', 'h', 'Ê', 'ê', ':'],
['À', 'à', 'I', 'i', 'J', 'j', 'K', 'k', 'È', 'è', '.'],
['L', 'l', 'Î', 'î', 'M', 'm', 'N', 'n', 'É', 'é', '?'],
['O', 'o', 'Ï', 'ï', 'P', 'p', 'Q', 'q', 'R', 'r', '!'],
['Ô', 'ô', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', '“'],
['W', 'w', 'X', 'x', 'Y', 'y', 'Ù', 'ù', 'Z', 'z', '”'],
['@', '&', '#', '[', '(', '/', ')', ']', '+', '=', '-'],
];
console.log(findPos(array,' ')) //=> [0, 0]
console.log(findPos(array,'M')) //=> [4, 4]
console.log(findPos(array,'u')) //=> [6, 7]
console.log(findPos(array,'-')) //=> [8, 10]
这会产生正确的结果。不需要stringify,你可以将数组展平并使用indexOf
来获取位置:
var letterVariations = [
[' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', ';'],
['Â', 'â', 'F', 'f', 'G', 'g', 'H', 'h', 'Ê', 'ê', ':'],
['À', 'à', 'I', 'i', 'J', 'j', 'K', 'k', 'È', 'è', '.'],
['L', 'l', 'Î', 'î', 'M', 'm', 'N', 'n', 'É', 'é', '?'],
['O', 'o', 'Ï', 'ï', 'P', 'p', 'Q', 'q', 'R', 'r', '!'],
['Ô', 'ô', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', '“'],
['W', 'w', 'X', 'x', 'Y', 'y', 'Ù', 'ù', 'Z', 'z', '”'],
['@', '&', '#', '[', '(', '/', ')', ']', '+', '=', '-'],
];
var flattened = letterVariations.flat()
var findLetter = function(letter) {
var pos = flattened.indexOf(letter),
x = Math.floor((pos / 10) % 8),
y = (pos - (pos % 11)) / 11;
return {
letter: letter,
x: x,
y: y
}
}
console.log(findLetter(' ')) //=> [0, 0]
console.log(findLetter('M')) //=> [4, 4]
console.log(findLetter('u')) //=> [6, 7]
console.log(findLetter('-')) //=> [8, 10]
基于@GluePear 的回答
您甚至可以在此解决方案中使用多字符
function findPos(array, symbol) {
const string = array.flat();
const pos = string.indexOf(symbol)
const d = (array[0] || []).length
const x = pos % d;
const y = Math.floor(pos / d)
return { x, y }
}
const array = [
[' ','0','1','2','3','4','5','6','7','8','9'],
['A','a','B','b','C','c','D','d','E','e',';'],
['Â','â','F','f','G','g','H','h','Ê','ê',':'],
['À','à','I','i','J','j','K','k','È','è','.'],
['L','l','Î','î','M','m','N','n','É','é','?'],
['O','o','Ï','ï','P','p','Q','q','R','r','!'],
['Ô','ô','S','s','T','t','U','u','V','v','“'],
['W','w','X','x','Y','y','Ù','ù','Z','z','”'],
['@','&','#','[','(','/',')',']','+','=','-'],
];
console.log(findPos(array, '-'))
这是其中一个版本:
var letterVariations = [
[' ','0','1','2','3','4','5','6','7','8','9'],
['A','a','B','b','C','c','D','d','E','e',';'],
['Â','â','F','f','G','g','H','h','Ê','ê',':'],
['À','à','I','i','J','j','K','k','È','è','.'],
['L','l','Î','î','M','m','N','n','É','é','?'],
['O','o','Ï','ï','P','p','Q','q','R','r','!'],
['Ô','ô','S','s','T','t','U','u','V','v','“'],
['W','w','X','x','Y','y','Ù','ù','Z','z','”'],
['@','&','#','[','(','/',')',']','+','=','-'],
];
const findLetterIn = letterVariations => {
const width = letterVariations[0].length * 4 + 2;
const alpha = JSON.stringify(letterVariations)
return (char, pos = alpha.indexOf(char)) => pos > -1
? [Math.floor((pos - 1) / width), (((pos - 1) % width) - 2)/4]
: [-1, -1]
}
const findLetter = findLetterIn (letterVariations)
console.log(findLetter(' ')) //=> [0, 0]
console.log(findLetter('M')) //=> [4, 4]
console.log(findLetter('u')) //=> [6, 7]
console.log(findLetter('-')) //=> [8, 10]
这里width
与行宽有关
4
与u ~> "u",
有关+ 2
与在开头和结尾添加[
和]
有关(如以及 ]
之后的附加 ,
,但删除它之前的一个。)- 1
与忽略初始 [
和 - 2
有与删除前导 ,"
或者,对于第一个,前导 ["
.
您可以通过将返回数组的两个元素都加 1 来切换到基于 1 的索引。
您可以连接字符串并使用内部数组的长度作为除法或取余运算符的值。这仅适用于具有单个字符的字符串。
var letterVariations = [
[' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', ';'],
['Â', 'â', 'F', 'f', 'G', 'g', 'H', 'h', 'Ê', 'ê', ':'],
['À', 'à', 'I', 'i', 'J', 'j', 'K', 'k', 'È', 'è', '.'],
['L', 'l', 'Î', 'î', 'M', 'm', 'N', 'n', 'É', 'é', '?'],
['O', 'o', 'Ï', 'ï', 'P', 'p', 'Q', 'q', 'R', 'r', '!'],
['Ô', 'ô', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', '“'],
['W', 'w', 'X', 'x', 'Y', 'y', 'Ù', 'ù', 'Z', 'z', '”'],
['@', '&', '#', '[', '(', '/', ')', ']', '+', '=', '-']
],
string = letterVariations.map(a => a.join('')).join(''),
pos = string.indexOf("u");
console.log(Math.floor(pos / 11), pos % 11);
pos = string.indexOf("M")
console.log(Math.floor(pos / 11), pos % 11);
尝试在没有循环的情况下给出这个问题的替代方案!仅使用 indexOf 和一些整数数学
下面的代码看起来很有前途但失败了。
谁有更好的数学技能想要解决它?
var letterVariations = [
[' ','0','1','2','3','4','5','6','7','8','9'],
['A','a','B','b','C','c','D','d','E','e',';'],
['Â','â','F','f','G','g','H','h','Ê','ê',':'],
['À','à','I','i','J','j','K','k','È','è','.'],
['L','l','Î','î','M','m','N','n','É','é','?'],
['O','o','Ï','ï','P','p','Q','q','R','r','!'],
['Ô','ô','S','s','T','t','U','u','V','v','“'],
['W','w','X','x','Y','y','Ù','ù','Z','z','”'],
['@','&','#','[','(','/',')',']','+','=','-'],
];
var string = JSON.stringify(letterVariations);
var pos = string.indexOf("u")
console.log(Math.floor((pos/10)%8),pos%10)
// fails, how to fix?
pos = string.indexOf("M")
console.log(Math.floor((pos/10)%8),pos%10)
function findPos(array, symbol) {
const string = array.toString().replace(/,/g, '');
const pos = string.indexOf(symbol)
const d = (array[0] || []).length
const x = pos % d;
const y = Math.floor(pos / d)
return { x, y }
}
const array = [
[' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', ';'],
['Â', 'â', 'F', 'f', 'G', 'g', 'H', 'h', 'Ê', 'ê', ':'],
['À', 'à', 'I', 'i', 'J', 'j', 'K', 'k', 'È', 'è', '.'],
['L', 'l', 'Î', 'î', 'M', 'm', 'N', 'n', 'É', 'é', '?'],
['O', 'o', 'Ï', 'ï', 'P', 'p', 'Q', 'q', 'R', 'r', '!'],
['Ô', 'ô', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', '“'],
['W', 'w', 'X', 'x', 'Y', 'y', 'Ù', 'ù', 'Z', 'z', '”'],
['@', '&', '#', '[', '(', '/', ')', ']', '+', '=', '-'],
];
console.log(findPos(array,' ')) //=> [0, 0]
console.log(findPos(array,'M')) //=> [4, 4]
console.log(findPos(array,'u')) //=> [6, 7]
console.log(findPos(array,'-')) //=> [8, 10]
这会产生正确的结果。不需要stringify,你可以将数组展平并使用indexOf
来获取位置:
var letterVariations = [
[' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', ';'],
['Â', 'â', 'F', 'f', 'G', 'g', 'H', 'h', 'Ê', 'ê', ':'],
['À', 'à', 'I', 'i', 'J', 'j', 'K', 'k', 'È', 'è', '.'],
['L', 'l', 'Î', 'î', 'M', 'm', 'N', 'n', 'É', 'é', '?'],
['O', 'o', 'Ï', 'ï', 'P', 'p', 'Q', 'q', 'R', 'r', '!'],
['Ô', 'ô', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', '“'],
['W', 'w', 'X', 'x', 'Y', 'y', 'Ù', 'ù', 'Z', 'z', '”'],
['@', '&', '#', '[', '(', '/', ')', ']', '+', '=', '-'],
];
var flattened = letterVariations.flat()
var findLetter = function(letter) {
var pos = flattened.indexOf(letter),
x = Math.floor((pos / 10) % 8),
y = (pos - (pos % 11)) / 11;
return {
letter: letter,
x: x,
y: y
}
}
console.log(findLetter(' ')) //=> [0, 0]
console.log(findLetter('M')) //=> [4, 4]
console.log(findLetter('u')) //=> [6, 7]
console.log(findLetter('-')) //=> [8, 10]
基于@GluePear 的回答
您甚至可以在此解决方案中使用多字符
function findPos(array, symbol) {
const string = array.flat();
const pos = string.indexOf(symbol)
const d = (array[0] || []).length
const x = pos % d;
const y = Math.floor(pos / d)
return { x, y }
}
const array = [
[' ','0','1','2','3','4','5','6','7','8','9'],
['A','a','B','b','C','c','D','d','E','e',';'],
['Â','â','F','f','G','g','H','h','Ê','ê',':'],
['À','à','I','i','J','j','K','k','È','è','.'],
['L','l','Î','î','M','m','N','n','É','é','?'],
['O','o','Ï','ï','P','p','Q','q','R','r','!'],
['Ô','ô','S','s','T','t','U','u','V','v','“'],
['W','w','X','x','Y','y','Ù','ù','Z','z','”'],
['@','&','#','[','(','/',')',']','+','=','-'],
];
console.log(findPos(array, '-'))
这是其中一个版本:
var letterVariations = [
[' ','0','1','2','3','4','5','6','7','8','9'],
['A','a','B','b','C','c','D','d','E','e',';'],
['Â','â','F','f','G','g','H','h','Ê','ê',':'],
['À','à','I','i','J','j','K','k','È','è','.'],
['L','l','Î','î','M','m','N','n','É','é','?'],
['O','o','Ï','ï','P','p','Q','q','R','r','!'],
['Ô','ô','S','s','T','t','U','u','V','v','“'],
['W','w','X','x','Y','y','Ù','ù','Z','z','”'],
['@','&','#','[','(','/',')',']','+','=','-'],
];
const findLetterIn = letterVariations => {
const width = letterVariations[0].length * 4 + 2;
const alpha = JSON.stringify(letterVariations)
return (char, pos = alpha.indexOf(char)) => pos > -1
? [Math.floor((pos - 1) / width), (((pos - 1) % width) - 2)/4]
: [-1, -1]
}
const findLetter = findLetterIn (letterVariations)
console.log(findLetter(' ')) //=> [0, 0]
console.log(findLetter('M')) //=> [4, 4]
console.log(findLetter('u')) //=> [6, 7]
console.log(findLetter('-')) //=> [8, 10]
这里width
与行宽有关
4
与u ~> "u",
有关+ 2
与在开头和结尾添加[
和]
有关(如以及 ]
之后的附加 ,
,但删除它之前的一个。)- 1
与忽略初始 [
和 - 2
有与删除前导 ,"
或者,对于第一个,前导 ["
.
您可以通过将返回数组的两个元素都加 1 来切换到基于 1 的索引。
您可以连接字符串并使用内部数组的长度作为除法或取余运算符的值。这仅适用于具有单个字符的字符串。
var letterVariations = [
[' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', ';'],
['Â', 'â', 'F', 'f', 'G', 'g', 'H', 'h', 'Ê', 'ê', ':'],
['À', 'à', 'I', 'i', 'J', 'j', 'K', 'k', 'È', 'è', '.'],
['L', 'l', 'Î', 'î', 'M', 'm', 'N', 'n', 'É', 'é', '?'],
['O', 'o', 'Ï', 'ï', 'P', 'p', 'Q', 'q', 'R', 'r', '!'],
['Ô', 'ô', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', '“'],
['W', 'w', 'X', 'x', 'Y', 'y', 'Ù', 'ù', 'Z', 'z', '”'],
['@', '&', '#', '[', '(', '/', ')', ']', '+', '=', '-']
],
string = letterVariations.map(a => a.join('')).join(''),
pos = string.indexOf("u");
console.log(Math.floor(pos / 11), pos % 11);
pos = string.indexOf("M")
console.log(Math.floor(pos / 11), pos % 11);