The Student Room Group

How do I derive the order of growth in this code?

function palindrome_distance(lst) {
if (is_null(lst)) {
return 0;
}
else if (is_null(tail(lst))) {
return 0;
}
else if (head(lst) === last(lst)) {
return palindrome_distance(tail(drop_last(lst)));
}
else {
const c1 = 1 + palindrome_distance(drop_last(lst)); const c2 = 1 + palindrome_distance(tail(lst)); return math_min(c1, c2);
}
}

// Helper function to return the last element of list `lst, // has linear running time function
last(lst) {
return is_null(tail(lst)) ? head(lst) : last(tail(lst));
}

// Helper function to return the list `lst without the last element, // has linear running time function
drop_last(lst) {
return is_null(tail(lst)) ? null : pair(head(lst), drop_last(tail(lst)));
}

What method should I use to find out what order of growth this code has?

Quick Reply

Latest

Trending

Trending