gpt4 book ai didi

javascript - 如何将数组中的已排序数组合并为第三个已排序数组

转载 作者:行者123 更新时间:2023-12-03 05:54:41 24 4
gpt4 key购买 nike

标题可能会令人困惑,请原谅我,因为我还是一个初学者。这是学校硬件作业的一部分,虽然我并不是直接寻找答案,但我需要朝着正确的方向插入,或者任何帮助。那么问题来了...

我需要根据类中的代码创建一个简单的程序,将多个排序数组合并为一个排序数组以输出到屏幕。这些数组被硬编码为“测试用例”以取消注释和验证。导师指导如下

Using the merge program developed in class (see attached) as a base, create a version that will merge 0 (zero) to n arrays of numbers. Hints: Sort the arrays before merging Use the shift method to remove the first item in an array (arrayName.shift()) To create an array of arrays ---> var x = [ ]; var y = [ ]; var z = [ x, y ];

编辑:我已经通过电子邮件向讲师发送了基于 @cpugourou 给出的答案的解决方案,他的答复是他不会接受该解决方案。作业的重点是手动合并这些数组。

下面是我们在类里面开发的原始“合并程序”

"use strict";   // reduces chance for error

/*
there are 2 arrays here and this program will merge them.
Your job is to merge zero or more arrays. Test cases include:
1. arrays with no elements: x = [], y = [], z = [], ...
2. 2 arrays - one with elements and one without: x = [], y = [200, 39,1]
3. 4 arrays: x = [5, 1, 0], y = [], z = [78, 3], w = [4, 34]
*/
var x = [ 12, 5, 1], y = [ 13, 2, 3], z = []; // x and y are input and, after processing, z has the merged arrays
var ix = 0, iy = 0, iz = 0; // indexes to the next element

// The following sorts the arrays in ascending sequence
x.sort(function(a, b){return a - b});
y.sort(function(a, b){return a - b});

while (ix < x.length || iy < y.length){ // while arrays x or y have elements

if (ix < x.length && iy < y.length){ // if both have elements, choose the lowest element

if (x[ix] <= y[iy]){ // is the current element in array x less than the current element in y?
z[iz] = x[ix]; // choose x
iz++; // point to the next space in z
ix++; // ditto x
} else{ // choose y
z[iz] = y[iy];
iz++; // next
iy++; // next
}

} else if (ix < x.length){ // if only one has elements is it x?
for (ix; ix < x.length; ix++){ // if so, move the rest of x to z
z[iz] = x[ix]; // copy current element in x
iz++; // next z ... no need to increment x because the for loop does it
}

} else if (iy < y.length){ // if only y has elements
for (iy; iy < y.length; iy++){ // move the rest of y to z
z[iz] = y[iy]; // copy
iz++; // increment z's pointer
}
}
}

// display the resulting merged elements in the web page
var result = "<ul>";
for (var i in z){
result += "<li>" + z[i] + "</li>"
}
result += "</ul>"
document.getElementById("placeAnswerHere").innerHTML=result;

/*
Things to think about:
. This code is hard wired to merge only 2 arrays
. You will need to create an array containing all arrays to be merged. e.g. q = [x, y, z, w, and more]
. Your loop will need to find the smallest element in any of the arrays in q and move it to z (the resultant array)
. There is a method shift() to strip the first element from an array (e.g. x.shift(); removes the first element in array x)
. This is like making a pile of cafeteria trays from a variable numbers of tray stacks.
. Remember to sort all arrays first
*/

所以我的新问题是,如何在不编写一堆意大利面条代码的情况下完成此任务?

最佳答案

我能想到的最快和最短的(添加了重复奖励):

var x = [5, 1, 0], y = [2, 5, 0], z = [78, 3, 1], w = [4, 34];

function sortNumber(a,b) {
return a - b;
}
function uniq(a) {
return Array.from(new Set(a));
}

var d = uniq(x.concat(y,z,w).sort(sortNumber));
/* [0, 1, 2, 3, 4, 5, 34, 78] */

关于javascript - 如何将数组中的已排序数组合并为第三个已排序数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39988177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com