sn62

Finished
1//
2// This is only a SKELETON file for the 'List Ops' exercise. It's been provided as a
3// convenience to get you started writing code faster.
4//
5
61export class List {
7 constructor() {
8 throw new Error('Remove this line and implement the function');
2 /**
3 * Creates a new List instance.
4 * @param {Array} [values=[]] - Initial values for the list.
5 */
6 constructor(values = []) {
7 this.values = values;
98 }
109
1110 /**
12 * @param {List} other
13 * @return {List}
11 * Appends all items from another list to the end of this list.
12 * @param {List} other - The list to append.
13 * @return {List} A new List containing all items from both lists.
14 * Handled Edge Cases: Appending empty lists, appending to empty lists
1415 */
1516 append(other) {
16 throw new Error('Remove this line and implement the function');
17 // Edge Case: Appending empty lists
18 if (!other || !other.values) {
19 return new List(this.values);
20 }
21
22 const result = [];
23 for (let i = 0; i < this.values.length; i++) {
24 result.push(this.values[i]);
25 }
26 for (let i = 0; i < other.values.length; i++) {
27 result.push(other.values[i]);
28 }
29 return new List(result);
1730 }
1831
1932 /**
20 * @param {List} others
21 * @return {List}
33 * Concatenates a series of lists into one flattened list.
34 * @param {List} others - A list containing other lists to concatenate.
35 * @return {List} A new List with all items from all lists.
36 * Handled Edge Cases: Concatenating empty lists, concatenating lists with empty sublists
2237 */
2338 concat(others) {
24 throw new Error('Remove this line and implement the function');
39 // Edge Case: Empty others list
40 if (!others || !others.values) {
41 return new List(this.values);
42 }
43
44 const result = [];
45
46 // Add items from the current list
47 for (let i = 0; i < this.values.length; i++) {
48 result.push(this.values[i]);
49 }
50
51 // Add items from each sublist in others
52 for (let i = 0; i < others.values.length; i++) {
53 const sublist = others.values[i];
54 // Edge Case: Sublist is not a List instance
55 if (sublist instanceof List) {
56 for (let j = 0; j < sublist.values.length; j++) {
57 result.push(sublist.values[j]);
58 }
59 } else {
60 // If it's not a List, treat it as a single value
61 result.push(sublist);
62 }
63 }
64
65 return new List(result);
2566 }
2667
2768 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
69 * Filters the list based on a predicate function.
70 * @param {Function} predicate - Function that returns true for items to keep.
71 * @return {List} A new List containing only items for which predicate returned true.
72 * Handled Edge Cases: Empty list, predicate that matches no items, predicate that matches all items
3073 */
3174 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
75 // Edge Case: Empty list
76 if (this.values.length === 0) {
77 return new List([]);
78 }
79
80 const result = [];
81 for (let i = 0; i < this.values.length; i++) {
82 if (predicate(this.values[i])) {
83 result.push(this.values[i]);
84 }
85 }
86 return new List(result);
3387 }
3488
3589 /**
36 * @param {(any) => any} expression
37 * @return {List}
90 * Applies a function to each item in the list.
91 * @param {Function} expression - Function to apply to each item.
92 * @return {List} A new List containing the results of applying the function.
93 * Handled Edge Cases: Empty list
3894 */
3995 map(expression) {
40 throw new Error('Remove this line and implement the function');
96 // Edge Case: Empty list
97 if (this.values.length === 0) {
98 return new List([]);
99 }
100
101 const result = [];
102 for (let i = 0; i < this.values.length; i++) {
103 result.push(expression(this.values[i]));
104 }
105 return new List(result);
41106 }
42107
43108 /**
44 * @return {number}
109 * Returns the number of items in the list.
110 * @return {number} The length of the list.
111 * Handled Edge Cases: Empty list
45112 */
46113 length() {
47 throw new Error('Remove this line and implement the function');
114 let count = 0;
115 for (let i = 0; i < this.values.length; i++) {
116 count++;
117 }
118 return count;
48119 }
49120
50121 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
122 * Folds (reduces) the list from the left using a callback function.
123 * @param {Function} callback - Function to apply to accumulator and each item.
124 * @param {any} initial - Initial value for the accumulator.
125 * @return {any} The final accumulator value.
126 * Handled Edge Cases: Empty list
54127 */
55128 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
129 let accumulator = initial;
130
131 // Edge Case: Empty list
132 if (this.values.length === 0) {
133 return accumulator;
134 }
135
136 for (let i = 0; i < this.values.length; i++) {
137 accumulator = callback(accumulator, this.values[i]);
138 }
139
140 return accumulator;
57141 }
58142
59143 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
144 * Folds (reduces) the list from the right using a callback function.
145 * @param {Function} callback - Function to apply to accumulator and each item.
146 * @param {any} initial - Initial value for the accumulator.
147 * @return {any} The final accumulator value.
148 * Handled Edge Cases: Empty list
63149 */
64150 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
151 let accumulator = initial;
152
153 // Edge Case: Empty list
154 if (this.values.length === 0) {
155 return accumulator;
156 }
157
158 // Process from right to left
159 for (let i = this.values.length - 1; i >= 0; i--) {
160 accumulator = callback(accumulator, this.values[i]);
161 }
162
163 return accumulator;
66164 }
67165
68166 /**
69 * @return {List}
167 * Returns a new list with all items in reverse order.
168 * @return {List} A new List with items in reverse order.
169 * Handled Edge Cases: Empty list
70170 */
71171 reverse() {
72 throw new Error('Remove this line and implement the function');
172 // Edge Case: Empty list
173 if (this.values.length === 0) {
174 return new List([]);
175 }
176
177 const result = [];
178 for (let i = this.values.length - 1; i >= 0; i--) {
179 result.push(this.values[i]);
180 }
181 return new List(result);
73182 }
74183}
Test NameStatus
empty lists
Pass
list to empty list
Pass
empty list to list
Pass
non-empty lists
Pass
empty list
Pass
list of lists
Pass
list of nested lists
Pass
empty list
Pass
non empty list
Pass
empty list
Pass
non-empty list
Pass
empty list
Pass
non-empty list
Pass
empty list
Pass
direction independent function applied to non-empty list
Pass
direction dependent function applied to non-empty list
Pass
empty list
Pass
direction independent function applied to non-empty list
Pass
direction dependent function applied to non-empty list
Pass
empty list
Pass
non-empty list
Pass
list of lists is not flattened
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.