Skip to main content

CoderByte Array Addition I

I started playing around with coderbyte a place to test your programming skills. I was stumped for a bit on this question Array Addition I:

"Using the JavaScript language, have the function ArrayAdditionI(arr) take the array of numbers stored in arr and return the string true if any combination of numbers in the array can be added up to equal the largest number in the array, otherwise return the string false. For example: if arrcontains [4, 6, 23, 10, 1, 3] the output should return true because 4 + 6 + 10 + 3 = 23. The array will not be empty, will not contain all the same elements, and may contain negative numbers. 

Use the Parameter Testing feature in the box below to test your code with different arguments."

But eventually hammered it out. I saw some people using multiple for loops as well as nested loops. I took a different approach and just did math with the exception of one for loop.

Here is my code example with assertions if you anyone is interested.



var array_addition = function (arr) {

  // code goes here
  var sorted_arr = arr.sort(function(a, b) { return a - b; }),
      largest = sorted_arr.pop(),
      sum = 0,
      i;

  sorted_arr.forEach(function (number) {
   sum += number;
  });

  //base condition is the total < largest
  if (sum < largest) {
   return false;
  }

  if ( largest === sum ) {
    //condition 1: everything in the array adds up to largest
    return true;
  } else if (sorted_arr.indexOf(Math.abs(largest - sum)) > 0) {
    //condition 2: largest - total gives you the difference in array
    return true;
  } else {
    //condition 3: traversal logic. Find any number in the array that adds up to the largest doing subtraction math
    for (i = 0; i < sorted_arr.length; i++) {
      return sorted_arr.indexOf(largest - sorted_arr[i]) > 0;
    }
  }

  return false;
}


var assert = function (actual, expected) {
  if (actual === expected) {
    console.log('assertion passed');
  } else {
    console.log('assertion failed: expected: ' + expected + " actual: " + actual );
  }
}


assert(true, array_addition([3,5,-1,8,12]));
assert(true, array_addition([11,10,-2,1,1,1]));
assert(false, array_addition([5,7,16,1,2]));
assert(false, array_addition([10,9,16,7,2]));

Comments

Popular posts from this blog

Libgdx: Create Item Database using JSON files

This blog post describes how to create an Item database using libgdx JSON parser and storing it in a Collections object to retrieve from.

0.) Create the JSON fileSave this as "weaponDb.json". This JSON object is used to create a list of Items in which we can grab items from.
[ { "id": 1, "name": "Dumb Basic Sword", }, { "id": 2, "name": "Bad Ass Flaming Sword", }, { "id": 3, "name": "Sword of Kick Ass", } ]1.) Create the testA simple JUnit test to test our database
import com.mygdx.items.Item; import com.mygdx.items.ItemLoader; import org.junit.Test; import java.io.IOException; import java.util.ArrayList; /*Simple Test Case*/ public class TestDb { @Test public void testDb() throws IOException { ItemLoader itemLoader = new ItemLoader(); ArrayList database = itemLoader.load("weaponDb.json&…

Inkscape: Kawaii Friends

Did this one for work.
Download svg file here: kawaii_friends

New Angry Birds(Maybe)

So I'm a huge fan of angry birds and while playing the game I thought hey I want to make some birds to break things. So here are some of the birds I created (with the exception of the first one).

Let me know what you guys think.
If you would like to study the drawing feel free to download the Inkscape SVG file.
Right click then hit save link as: newbirds.svg
All rights reserve to the original artists.