Jan 12, 2017

On to the third level of CodeFights: Arcade, List Forest Edge. The level on this one was quite doable, nothing hard or tricky. It's a level to learn about manipulating your arrays with your language. Want to codefight?

Create Array (300/300)

Given an integer size, return array of length size filled with 1s.

JavaScript

function createArray(size) {
    var onesies = [];
    for(var i=0; i<size; ++i)
        onesies[i] = 1;
    return onesies;
}

Array Replace (300/300)

Given an array of integers, replace all the occurrences of elemToReplace with substitutionElem.

JavaScript

function arrayReplace(inputArray, elemToReplace, substitutionElem) {
    
    for(var i=0; i<inputArray.length; ++i){
        if( inputArray[i] == elemToReplace )
            inputArray[i] = substitutionElem;
    }
    
    return inputArray;
}

First Reverse Try (300/300)

Reversing an array can be a tough task, especially for a novice programmer. Mary just started coding, so she would like to start with something basic at first. Instead of reversing the array entirely, she wants to swap just its first and last elements.

Given an array arr, swap its first and last elements and return the resulting array.

JavaScript

function firstReverseTry(arr) {
    if(arr.length == 0) //To avoid accessing arr[arr.length-1] == arr[-1]
        return arr;
    var tmp = arr[0];
    arr[0] = arr[arr.length-1];
    arr[arr.length-1] = tmp;
    return arr;
}

Concatenate Arrays (300/300)

Given two arrays of integers a and b, obtain the array formed by the elements of a followed by the elements of b.

JavaScript

function concatenateArrays(a, b) {
    //Don't have to check for nulls
    //Can reuse array a, thank you JS
    for(var i=0, a_index=a.length; i<b.length; ++i, ++a_index)
        a[a_index] = b[i];
    return a;
}

Remove Array Part (300/300)

Remove a part of a given array between given 0-based indexes l and r (inclusive).

JavaScript

function removeArrayPart(inputArray, l, r) {
    for(var i=l; i <= r ; ++i) //limit with r thanks to problem constraints
       inputArray.splice(l,1); //always use l since array is being manipulated during loop
    return inputArray;
}

Is Smooth? (300/300)

We define the middle of the array arr as follows:

  • if arr contains an odd number of elements, its middle is the element whose index number is the same when counting from the beginning of the array and from its end;
  • if arr contains an even number of elements, its middle is the sum of the two elements whose index numbers when counting from the beginning and from the end of the array differ by one.

An array is called smooth if its first and its last elements are equal to one another and to the middle. Given an array arr, determine if it is smooth or not.

JavaScript

function isSmooth(arr) {
    // Don't have to worry about arr.length == 0 thanks to constraints
    var is_even = arr.length % 2 == 0;
    var middle_pos = (arr.length - 1) / 2; // -1 b/c array goes 0 ... array.length-1
    var middle = !is_even ? 
                 arr[middle_pos] : // arr contained odd number of elements
                 arr[middle_pos-.5] + arr[middle_pos+.5]; 
                 //+-.5 to get to their round parts, could use Math.ceil()/floor() 
       
    return (arr[0] == arr[arr.length-1]) && (arr[0] == middle);
}

Replace Middle (300/300)

We define the middle of the array arr as follows:

  • if arr contains an odd number of elements, its middle is the element whose index number is the same when counting from the beginning of the array and from its end;
  • if arr contains an even number of elements, its middle is the sum of the two elements whose index numbers when counting from the beginning and from the end of the array differ by one.

Given array arr, your task is to find its middle, and, if it consists of two elements, replace those elements with the value of middle. Return the resulting array as the answer

JavaScript

function replaceMiddle(arr) {
    //Don't have to worry about arr.length == 0
    var is_even = arr.length % 2 == 0;
    var middle_pos = (arr.length - 1) / 2;
    
    if(is_even){
       var l_middle_pos = middle_pos - .5;
       var r_middle_pos = middle_pos + .5;
       var middle_value = arr[l_middle_pos] + arr[r_middle_pos];
       arr[l_middle_pos] = middle_value;
       arr.splice(r_middle_pos, 1); // Pop the right middle value
    }
    
    return arr;        
}

Make Array Consecutive 2 (300/300)

Given an array of integers, we need to find the number of "holes" that need to be filled such that it contains all the integers from some range.

JavaScript

function makeArrayConsecutive2(sequence) {
    sequence.sort(compare);
    var holes = 0;
    for(var i=0; i<sequence.length - 1; ++i){
        var next_num = sequence[i+1];
        var current_num = sequence[i] + 1;
        if( next_num != current_num ) //They are not consecutive
           holes+=next_num-current_num;
    }
    return holes;
}

// Compare as numbers so that sort() won't compare after converting them to strings
// since then 10 will go after 1 and before 2
// On hindsight the compare function could be a simple one liner: return a - b;
function compare(a, b) {
  var result = 0;  
  if (a < b) 
    result = -1;
  else if (a > b) 
    result = 1;
    
  return result;
}