第 n 个最长字符串排序
nth Longest String Sortation
我已经编写了代码来确定字符串数组中第 n 个最长的字符串。下面我列出了 Codewars kata 中的测试用例。
说明:实现函数longest(array,n),你会得到一个字符串数组然后return第n长的该数组中的字符串。例如arr = ['Hello','World','Codewars','Katas'] n = 3;应该 return 'World' 因为 'Codewars' length = 8,'Hello' length = 5,所以这是第二长的单词,然后是 'World'(尽管也是单词长度5、'World'在数组中'Hello'之后)。当单词具有相同的长度时,按照它们在数组中存在的顺序处理它们。数组永远不会为空并且 n > 0 总是。
Test.assertEquals(longest(['Hello','World','Codewars','Katas'],3),'World');
Test.assertEquals(longest(['Hello','World','Codewars','Katas'],4),'Katas');
Test.assertEquals(longest(['aa', 'bb', 'cc', 'dd', 'eee', 'b', 'f', 'ff', 'hhh', 'gggg'],4),'aa');
Test.assertEquals(longest(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k'],1),'a');
Test.assertEquals(longest(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k','l'],1),'a');
除了数组以 'l' 结尾的最后一个例外,我已经通过了所有 codewars 测试用例。我的排序代码行似乎将 'f' 放在这个测试用例的第零位置,我不明白为什么。
function longest(arr, n) {
arrLength = [];
arr.sort(function(a, b){return b.length - a.length});
console.log(arr);
arr.forEach(function(numArray){
return arrLength.push(numArray.length);
});
return arr[n-1];
}
console.log(longest(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k'],1));
// Sorted Array: ["a", "b", "c", "d", "e", "f", "g", "h", "i", "k"]
// returns a
console.log(longest(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l'],1));
// Sorted Array: ["f", "a", "c", "d", "e", "b", "g", "h", "i", "k", "l"]
// returns f
当 'l' 添加到字符串数组的末尾时,我似乎无法弄清楚为什么我的排序函数将 "f" 放在第 0 个位置。
您使用了内置的排序函数,也许这个函数会根据您的数组改变排序算法,最终导致相同长度的字符串不具有相同的行为。甚至这可能取决于浏览器。
我建议您通过使用具有确定排序功能(快速排序,等等...)的库来更改此设置。并检查是否再次发生这种情况。
在 MSIE 上运行良好。
在 Microsoft Internet Explorer(任何版本)上进行的快速测试针对您提供的功能得出以下结果:
>> longest(['a','b','c','d','e','f','g','h','i','k'],1);
a,b,c,d,e,f,g,h,i,k
"a"
>> console.log(longest(['a','b','c','d','e','f','g','h','i','k','l'],1));
a,b,c,d,e,f,g,h,i,k,l
a
>> console.log(longest(['a','b','c','d','e','f','g','h','i','k','l',"m","n"],1));
a,b,c,d,e,f,g,h,i,k,l,m,n
a
p.s.: 所有非 MS 浏览器都存在 稳定性 sort().[=11= 的问题]
我已经编写了代码来确定字符串数组中第 n 个最长的字符串。下面我列出了 Codewars kata 中的测试用例。
说明:实现函数longest(array,n),你会得到一个字符串数组然后return第n长的该数组中的字符串。例如arr = ['Hello','World','Codewars','Katas'] n = 3;应该 return 'World' 因为 'Codewars' length = 8,'Hello' length = 5,所以这是第二长的单词,然后是 'World'(尽管也是单词长度5、'World'在数组中'Hello'之后)。当单词具有相同的长度时,按照它们在数组中存在的顺序处理它们。数组永远不会为空并且 n > 0 总是。
Test.assertEquals(longest(['Hello','World','Codewars','Katas'],3),'World');
Test.assertEquals(longest(['Hello','World','Codewars','Katas'],4),'Katas');
Test.assertEquals(longest(['aa', 'bb', 'cc', 'dd', 'eee', 'b', 'f', 'ff', 'hhh', 'gggg'],4),'aa');
Test.assertEquals(longest(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k'],1),'a');
Test.assertEquals(longest(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k','l'],1),'a');
除了数组以 'l' 结尾的最后一个例外,我已经通过了所有 codewars 测试用例。我的排序代码行似乎将 'f' 放在这个测试用例的第零位置,我不明白为什么。
function longest(arr, n) {
arrLength = [];
arr.sort(function(a, b){return b.length - a.length});
console.log(arr);
arr.forEach(function(numArray){
return arrLength.push(numArray.length);
});
return arr[n-1];
}
console.log(longest(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k'],1));
// Sorted Array: ["a", "b", "c", "d", "e", "f", "g", "h", "i", "k"]
// returns a
console.log(longest(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l'],1));
// Sorted Array: ["f", "a", "c", "d", "e", "b", "g", "h", "i", "k", "l"]
// returns f
当 'l' 添加到字符串数组的末尾时,我似乎无法弄清楚为什么我的排序函数将 "f" 放在第 0 个位置。
您使用了内置的排序函数,也许这个函数会根据您的数组改变排序算法,最终导致相同长度的字符串不具有相同的行为。甚至这可能取决于浏览器。
我建议您通过使用具有确定排序功能(快速排序,等等...)的库来更改此设置。并检查是否再次发生这种情况。
在 MSIE 上运行良好。
在 Microsoft Internet Explorer(任何版本)上进行的快速测试针对您提供的功能得出以下结果:
>> longest(['a','b','c','d','e','f','g','h','i','k'],1);
a,b,c,d,e,f,g,h,i,k
"a"
>> console.log(longest(['a','b','c','d','e','f','g','h','i','k','l'],1));
a,b,c,d,e,f,g,h,i,k,l
a
>> console.log(longest(['a','b','c','d','e','f','g','h','i','k','l',"m","n"],1));
a,b,c,d,e,f,g,h,i,k,l,m,n
a
p.s.: 所有非 MS 浏览器都存在 稳定性 sort().[=11= 的问题]