-
Notifications
You must be signed in to change notification settings - Fork 2
/
infoFromArray.js
147 lines (134 loc) · 3.94 KB
/
infoFromArray.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// --------------------------------------------------------------------------
// -- infoFromArray.js
// -- initial author: Renick Bell ([email protected])
// -- initial creation date: Wed Jun 28 10:08:48 AM CST 2023
// -- contributors: Yiler Huang ([email protected]); Steve Wang ([email protected])
// -- license: GPL 3.0
// --------------------------------------------------------------------------
function findClosest(arr, item, comparatorFn) {
let mid = arr.indexOf(item);
let left = mid - 1;
let right = mid + 1;
while (left > 0 || right < arr.length) {
if (left < 0) {
} else if (comparatorFn(arr[left], item) === true) {
return { item: arr[left], index: left };
} else {
left -= 1;
}
if (right > arr.length) {
} else if (comparatorFn(arr[right], item) === true) {
return { item: arr[right], index: right };
} else {
right += 1;
}
}
return false;
}
function findClosestSmaller(arr, item) {
return this.findClosest(arr, item, (a, b) => {
return a < b;
}).item;
}
function findClosestLarger(arr, item) {
return this.findClosest(arr, item, (a, b) => {
return a > b;
}).item;
}
//Find most frequent item that appears in array: change to find most frequen item
function findMostFrequentItem(array) {
let itemCounts = new Map();
let maxItem = array[0];
let maxCount = 0;
for (let i = 0; i < array.length; i++) {
let item = array[i];
if (itemCounts.has(item)) {
itemCounts.set(item, itemCounts.get(item) + 1);
} else {
itemCounts.set(item, 1);
}
let currentCount = itemCounts.get(item);
if (currentCount > maxCount) {
maxItem = item;
maxCount = currentCount;
}
}
return maxItem;
}
function sum(inputArray) {
return inputArray.reduce((acc, cur) => {
return acc + cur;
}, 0);
}
function runningSum(startingVal, inputArray) {
let currentSum = startingVal;
let output = [];
for (let i = 0; i < inputArray.length; i++) {
let newSum = inputArray[i] + currentSum;
output.push(newSum);
currentSum = newSum;
}
return output;
}
function mean(inputArray) {
return this.sum(inputArray) / inputArray.length;
}
function includesOneOf(inputArray, things) {
let bools = things.map((t) => inputArray.includes(t));
if (bools.includes(true)) {
return true;
} else {
return false;
}
}
function matchesOneOf(candidates, thing) {
return this.includesOneOf([thing], candidates);
}
//Generated by chatgpt:
// Generic function to find index values based on a comparator function
function getIndexValues(inputArray, comparatorFn) {
let currentIndexValues = [];
for (let i = 0; i < inputArray.length; i++) {
if (currentIndexValues.length === 0) {
currentIndexValues.push([i, inputArray[i]]);
} else {
const comparisonResult = comparatorFn(
inputArray[i],
currentIndexValues[0][1]
);
if (comparisonResult < 0) {
currentIndexValues = [[i, inputArray[i]]];
} else if (comparisonResult === 0) {
currentIndexValues.push([i, inputArray[i]]);
}
}
}
return currentIndexValues;
}
//Generated by chatgpt:
// Specific functions for finding the minimum and maximum index values
function getMinIndex(inputArray) {
return this.getIndexValues(inputArray, (a, b) => {
return a - b;
});
}
//Generated by chatgpt:
function getMaxIndex(inputArray) {
return this.getIndexValues(inputArray, (a, b) => {
return b - a;
});
}
module.exports = {
findClosest,
findClosestSmaller,
findClosestLarger,
findMostFrequentItem,
sum,
runningSum,
mean,
includesOneOf,
matchesOneOf,
getIndexValues,
getMinIndex,
getMaxIndex,
};