From bcaf81919cb889c1209c6d1e577b037047714c05 Mon Sep 17 00:00:00 2001 From: Adrian Mejia Date: Tue, 28 Jul 2020 14:11:07 -0400 Subject: [PATCH] feat(book/arrays): add exercises --- book/content/part02/array.asc | 46 +++++++++++++++++++- book/content/part02/linked-list.asc | 4 ++ lab/exercises/01-arrays/rotate-array-left.js | 15 +++++++ lab/exercises/01-arrays/sum-arrays.js | 13 ++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 lab/exercises/01-arrays/rotate-array-left.js create mode 100644 lab/exercises/01-arrays/sum-arrays.js diff --git a/book/content/part02/array.asc b/book/content/part02/array.asc index 6c95d376..34cadf6a 100644 --- a/book/content/part02/array.asc +++ b/book/content/part02/array.asc @@ -7,7 +7,7 @@ endif::[] === Array (((Array))) (((Data Structures, Linear, Array))) -Arrays are one of the most used data structures. You probably have used it a lot but are you aware of the runtimes of `splice`, `shift`, `indexOf` and other operations? In this chapter, we are going deeper into the most common operations and their runtimes. +Arrays are one of the most used data structures. You probably have used it a lot but are you aware of the runtimes of `splice`, `shift`, `indexOf` and other operations? In this chapter, we are going deeper into the most common operations and their runtimes. ==== Array Basics @@ -274,3 +274,47 @@ To sum up, the time complexity of an array is: | splice ^| O(n) | Insert and remove from anywhere. |=== //end::table + +==== Array Exercises + +1) Implement an efficient algorithm that rotate an array `a` an `k` number of times. + +[source, javascript] +---- +/** + * Rotate an array left by k number of times. + * + * @example + * rotateLeft([1,2,3], 1); // [2,3,1] + * rotateLeft([1,2,3,4,5], 4); // [5,1,2,3,4] + * + * rotateLeft(Array(1e6).fill(1), 1e4); // + * + * @param a - The array + * @param k - The number of times the array is rotated + */ +function rotateLeft(a, k) { + // write you code and test with examples +} +---- + + +2) Implement an algorithm that takes two arrays of numbers and return a new array with the sum. + +[source, javascript] +---- +/** + * Return the sum of two arrays as a new array. + * + * @example + * sum([1,2,3], [1,1,1]); // [2,3,4] + * sum([1], [9,9,9]); // [1,0,0,0] + * + * @param {number[]} a - Array of numbers. + * @param {number[]} b - Array of numbers. + * @returns {number[]} the sum array. + */ +function sum(a, b) { + // write you code and test with examples +} +---- diff --git a/book/content/part02/linked-list.asc b/book/content/part02/linked-list.asc index 263caef3..6e3f3f98 100644 --- a/book/content/part02/linked-list.asc +++ b/book/content/part02/linked-list.asc @@ -285,3 +285,7 @@ Use a doubly linked list when: * You want to save some memory when dealing with possibly large data sets. Arrays pre-allocate a large chunk of contiguous memory on initialization. Lists are more “grow as you go”. For the next two linear data structures <> and <>, we are going to use a doubly linked list to implement them. We could use an array as well, but since inserting/deleting from the start performs better with linked-lists, we are going use that. + +==== Linked List Exercises + +1) Merge two sorted lists into one (and keep them sorted) diff --git a/lab/exercises/01-arrays/rotate-array-left.js b/lab/exercises/01-arrays/rotate-array-left.js new file mode 100644 index 00000000..88640b89 --- /dev/null +++ b/lab/exercises/01-arrays/rotate-array-left.js @@ -0,0 +1,15 @@ +/** + * Rotate an array left by k number of times. + * + * @example + * rotateLeft([1,2,3], 1); // [2,3,1] + * rotateLeft([1,2,3,4,5], 4); // [5,1,2,3,4] + * + * rotateLeft(Array(1e6).fill(1), 1e4); // + * + * @param a - The array + * @param k - The number of times the array is rotated + */ +function rotateLeft(a, k) { + // write you code and test with examples +} diff --git a/lab/exercises/01-arrays/sum-arrays.js b/lab/exercises/01-arrays/sum-arrays.js new file mode 100644 index 00000000..24d3576a --- /dev/null +++ b/lab/exercises/01-arrays/sum-arrays.js @@ -0,0 +1,13 @@ +/** + * Return the sum of two arrays as a new array. + * + * @example + * sum([1,2,3], [1,1,1]); // [2,3,4] + * sum([1], [9,9,9]); // [1,0,0,0] + * + * @param {number[]} a - Array of numbers. + * @param {number[]} b - Array of numbers. + * @returns {number[]} the sum array. + */ +function sum(a, b) { +}