Skip to content

Instantly share code, notes, and snippets.

View raivatshah's full-sized avatar
💻
Hacking...

Raivat Shah raivatshah

💻
Hacking...
View GitHub Profile
// 1A
function compute_e(lst) {
function helper_acc(xs, n, current){
if(is_empty_list(xs)) {
return current;
} else {
if (n % 2 === 0){
return helper_acc(tail(xs), n + 1, current + head(xs)); //if index is even (so number is at odd i), just add it to the sum
} else {
return helper_acc(tail(xs), n + 1, current + head(xs) * 3); //if index is odd (so number is at even i), multiply by 3 and then add to sum
@raivatshah
raivatshah / avenger questions.js
Last active November 14, 2018 09:39
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).