Skip to content

Instantly share code, notes, and snippets.

@raivatshah
Last active November 14, 2018 09:39
Show Gist options
  • Save raivatshah/31c11193e04de4092b946f5f11c32469 to your computer and use it in GitHub Desktop.
Save raivatshah/31c11193e04de4092b946f5f11c32469 to your computer and use it in GitHub Desktop.
Questions from Avenger Tiffany of CS1101S at NUS SoC
//Obtain sum of array using for-loops
/* Thought process: this is the same as our sum for lists but in an imperative style of programming. So the idea is the same as recursion
Array indexes start at 0 and end at n - 1, where n is the number of elements in the array. Therefore, we start at 0 and end at n-1, where we move 1 element each time
*/
function sumArray(arr) {
let sum = 0; //initialize counter at 0 because we haven't added anything yet (we usually start counting with 0 in CS).
for(let i = 0; i < array_length(arr); i = i + 1) {
sum = sum + arr[i]; //at each interation of the loop, update the sum by adding the element stored at index i in the array arr
}
return sum; //return the updated sum variable
}
//Test Case:
const a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
sumArray(a); //returns 55. Working fine
//Question 1 extra - implementing sum using while loop
function sumArray2(arr) {
let sum = 0; //initialize sum to be 0
let i = 0; //initialize i to start at element 1 of the array
while (i < array_length(arr)) {
sum = sum + arr[i]; //update sum for each iteration of the loop
i = i + 1;//update i to move to the next element in the array
}
return sum; //return the updated sum
}
//Question 2 - Implementing Map
/* Reasoning: This is also the same as recursion in the imperative style of programming
at each iteration of the loop, we need to apply the function supplied in map to the ith index
in the array. We end this loop when we are done with applying function to all elements
of the array.
*/
//Using For Loop
function mapArray(f, arr) {
for(let i = 0; i < array_length(arr); i = i + 1) {
arr[i] = f(arr[i]);//at each iteration, apply f to element in index i
}
return arr;//return updated/mutated array.
}
//Using While Loop
function mapArray_while(f, arr) {
let i = 0;//initialize counter outside the loop
while(i < array_length(arr)){
arr[i] = f(arr[i]);//apply f at each iteration
i = i + 1;//move to next item in the array
}
return arr;//return updated array
}
//Test Case:
const b = [];
mapArray(x => x + 1, b);
const c = [1, 2, 3, 4, 5, 6, 7];
mapArray(x => 2*x, c);
//Question 3 - Implement accumulate using for loop
/*
This question is also very simple. We are just doing recursion with loops. Since
accumulate is fold-right, we start at the last element in the array and move leftward
towards the first element. At each iteration, we want to apply binary op to our current and
the ith element of the array
*/
function accumulateArray(op, init, arr) {
let current = init; //initialize current to be initial value specified in args
for(let i = array_length(arr) - 1; i >= 0; i = i - 1){
current = op(arr[i], current);//apply binary function to all elements to the right
}
return current; //return updated current.
}
//Test Case:
function add(x, y){
return x + y;
}//why do we need this?
accumulateArray(add, 0, [1, 2, 3, 4, 5]);//returns 15
//Question 4 - Reverse Array
/* Also a simple scenario. We want to reverse the array [1, 2, 3, 4] such that it
becomes [4, 3, 2, 1].
Recursion in lists: if we had a reversed array of n-1 elments, we can
simply place the nth element in the tail using append.
In arrays using loops: we can build a new array by fetching elements from original
array in reverse order:
*/
function reverseArray(arr) {
let reversed_array = [];
for(let i = 0; i < array_length(arr); i = i + 1){
reversed_array[array_length(arr) - (i+1)] = arr[i];
}
arr = reversed_array; //make the original array point to the reversed array
return arr; //return the reversed array.
}
//Test Cases
const d = [1, 2, 3, 4, 5, 6, 7, 8, 9];
reverseArray(d);
//Question 5 - Filter Array
/* We have to filter elements from an array based on a specified predicate supplied in
the argument of this function. To do this, can create a new array that stores the values
that pass our predicate into a new array. At the end, we point our original array back
to new array (as we have to mutate the original array)
Since we have to obtain shorter answer, we cannot simply change new_array[i] = arr[i],
when the element satisfies a condition. We have to have another counter variable
that indexes for the new array.
*/
function filterArray(pred, arr){
let new_array = [];//initialize our new array to store the results
let index_counter = 0;//initialize our counter for the new array
for(let i = 0; i < array_length(arr); i = i + 1) {
if(pred(arr[i])) {
new_array[index_counter] = arr[i];//if the value satisfies pred, add it
index_counter = index_counter + 1; //update counter only when a value is added
} else {
//do nothing
}
}
//mutate
for(let i = 0; i < array_length(new_array); i = i + 1){
arr[i] = new_array[i];
}
for(let i = array_lenght(new_array); i < array_length(arr); i = i + 1){
arr[i] = 0;
}
return arr; //return updated original array
}
//Test Case:
filterArray(x => x % 2 === 0, [1, 2, 3, 4, 5, 6, 7, 8]); //returns [2, 4, 6, 8]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment