forked from csesoc/Handbook-Parser-Assessment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy.js
123 lines (108 loc) · 3.74 KB
/
easy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var assert = require("assert")
// Given an array of numbers, return a new array so that positive and negative
// numbers alternate. You can assume that 0 is a positive number. Within the
// positive and negative numbers, you must keep their relative order. You are
// guaranteed the number of positive and negative numbers will not differ by more
// than 1.
// =====Example 1
// Input: [1, -3, -8, -5, 10]
// Output: [-3, 1, -8, 10, -5]
// Explanation: We have alternated positive and negative numbers. Notice that the
// negative numbers appear in the same relative order (-3, -8, -5) and the positive
// numbers appear in the same order as well (1, 10).
// =====Example 2
// Input: [3, 0, 0, -5, -2]
// Output: [3, -5, 0, -2, 0]
// Explanation: We have alternated positive and negative numbers. Notice they appear
// in the same relative order.
// =====Example 3
// Input: [0, -3, 3, -1, 1, -1]
// Output #1: [0, -3, 3, -1, 1, -1]
// Output #2: [-3, 0, -1, 3, -1, 1]
// Explanation: There are 2 possible answers which satisfy the problem's constraints.
// We can start with either positive or negative
// =====Example 4
// Input numArray: []
// Output numArray: []
// Explanation: Empty array...
const altNumbers = (numArray) => {
// Separate numArray to postive and negative arrays
const posArray = []
const negArray = []
for (let j = 0; j < numArray.length; j++) {
if (numArray[j] < 0) {
negArray.push(numArray[j])
} else {
posArray.push(numArray[j])
}
}
// 3 cases depended on the differ of positive and negative
const newArray = [];
if (posArray.length > negArray.length) {
for (let j = 0; j < negArray.length; j++) {
newArray.push(posArray[j]);
newArray.push(negArray[j]);
}
newArray.push(posArray[posArray.length - 1]);
} else if (posArray.length < negArray.length) {
for (let j = 0; j < posArray.length; j++) {
newArray.push(negArray[j]);
newArray.push(posArray[j]);
}
newArray.push(negArray[negArray.length - 1]);
} else {
for (let j = 0; j < posArray.length; j++) {
newArray.push(negArray[j]);
newArray.push(posArray[j]);
}
}
return newArray;
}
module.exports = { altNumbers } // Do not modify this line
// ====================TESTS====================
// Some tests to help you check your progress. Simply run your code with
// node easy.js
// If successful, no output should appear. If unsuccessful, you should see
// assertion errors being thrown.
let array1 = [1, -3, -8, -5, 10]
array1 = altNumbers(array1)
const answer1 = [-3, 1, -8, 10, -5]
for (let i = 0; i < array1.length; i++) {
assert(array1[i] === answer1[i])
}
let array2 = [3, 0, 0, -5, -2]
array2 = altNumbers(array2)
const answer2 = [3, -5, 0, -2, 0]
for (let i = 0; i < array2.length; i++) {
assert(array2[i] === answer2[i])
}
let array3 = [0, -3, 3, -1, 1, -1]
array3 = altNumbers(array3)
const answer3a = [0, -3, 3, -1, 1, -1]
const answer3b = [-3, 0, -1, 3, -1, 1]
if (array3[0] === 0) {
for (let i = 0; i < array3.length; i++) {
assert(array3[i] === answer3a[i])
}
} else if (array3[0] == -3) {
for (let i = 0; i < array3.length; i++) {
assert(array3[i] === answer3b[i])
}
} else {
assert(false)
}
let array4 = []
array4 = altNumbers(array4)
assert(array4.length === 0)
let array5 = [3,2,1,-1,-2,-3,-4]
array5 = altNumbers(array5)
const answer5 = [-1, 3, -2, 2, -3, 1, -4]
for (let i = 0; i < array5.length; i++) {
assert(array5[i] === answer5[i])
}
let array6 = [5,-1,-2,-3,-4,0,3]
array6 = altNumbers(array6)
const answer6 = [-1, 5, -2, 0, -3, 3, -4]
for (let i = 0; i < array6.length; i++) {
assert(array6[i] === answer6[i])
}