Skip to content

Instantly share code, notes, and snippets.

@the-winter
Created August 4, 2016 07:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the-winter/5e4c6f466b19dd8bed0c6582a3ff78f4 to your computer and use it in GitHub Desktop.
Save the-winter/5e4c6f466b19dd8bed0c6582a3ff78f4 to your computer and use it in GitHub Desktop.
CodingBat/NodingBat JavaScript answers
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'use strict'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// var assert = require('chai').assert\n",
"var _ = require('lodash')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'use strict'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"/** test function to give feedback **/\n",
"function t(func,inputs,outputs){\n",
" // this uses the \"spread\" operator http://es6-features.org/#SpreadOperator\n",
" // TODO: if (func(...inputs)_.isEqual(inputs)){\n",
" \n",
" // runs the function wih the inputs\n",
" let res = func(...inputs)\n",
" // does the results match the exected outputs?\n",
" let match = _.isEqual(res,outputs)\n",
" // print the results\n",
" if (match){\n",
" console.log(`✔ ${func.name}(${inputs}) == ${outputs}`)\n",
" } else {\n",
" // this uses es6 template literals http://es6-features.org/#StringInterpolation\n",
" console.error(`✖ ${func.name}(${inputs}) != ${outputs}`)\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ firstLast6(1,2,6) == true\n",
"✔ firstLast6(6,1,2,3) == true\n",
"✔ firstLast6(13,6,1,2,3) == false\n",
"✔ firstLast6(13,6,1,2,6) == true\n",
"✔ firstLast6(3,2,1) == false\n",
"✔ firstLast6(3,6,1) == false\n",
"✔ firstLast6(3,6) == true\n",
"✔ firstLast6(6) == true\n",
"✔ firstLast6(3) == false\n",
"✔ firstLast6(5,6) == true\n",
"✔ firstLast6(5,5) == false\n",
"✔ firstLast6(1,2,3,4,6) == true\n",
"✔ firstLast6(1,2,3,4) == false\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// array-1\n",
"// firstLast6\n",
"/**\n",
"Given an array of ints, return true if 6 appears as \n",
"either the first or last element in the array. \n",
"The array will be length 1 or more.\n",
"**/\n",
"\n",
"function firstLast6(nums) {\n",
" if (nums[0]==6) {return true}\n",
" else if (nums[nums.length-1]==6) {return true}\n",
" else {return false}\n",
"}\n",
"\n",
"t(firstLast6,[[1, 2, 6]],true)\n",
"t(firstLast6,[[6, 1, 2, 3]],true)\n",
"t(firstLast6,[[13, 6, 1, 2, 3]],false)\n",
"t(firstLast6,[[13, 6, 1, 2, 6]],true)\n",
"t(firstLast6,[[3, 2, 1]],false)\n",
"t(firstLast6,[[3, 6, 1]],false)\n",
"t(firstLast6,[[3, 6]],true)\n",
"t(firstLast6,[[6]],true)\n",
"t(firstLast6,[[3]],false)\n",
"t(firstLast6,[[5, 6]],true)\n",
"t(firstLast6,[[5, 5]],false)\n",
"t(firstLast6,[[1, 2, 3, 4, 6]],true)\n",
"t(firstLast6,[[1, 2, 3, 4]],false)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ firstLast6(1,2,6) == true\n",
"✔ firstLast6(6,1,2,3) == true\n",
"✔ firstLast6(13,6,1,2,3) == false\n",
"✔ firstLast6(13,6,1,2,6) == true\n",
"✔ firstLast6(3,2,1) == false\n",
"✔ firstLast6(3,6,1) == false\n",
"✔ firstLast6(3,6) == true\n",
"✔ firstLast6(6) == true\n",
"✔ firstLast6(3) == false\n",
"✔ firstLast6(5,6) == true\n",
"✔ firstLast6(5,5) == false\n",
"✔ firstLast6(1,2,3,4,6) == true\n",
"✔ firstLast6(1,2,3,4) == false\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"//aray-1 \n",
"//firstlast6\n",
"/**Given an array of ints, return true if 6 appears as either the first or last element in the array. \n",
"The array will be length 1 or more.**/\n",
"\n",
"function firstLast6(nums) {\n",
" var end = nums.length - 1;\n",
" \n",
" return nums[0] == 6 || nums[end] == 6;\n",
"}\n",
"\n",
"\n",
" t(firstLast6,[[1, 2, 6]],true)\n",
" t(firstLast6,[[6, 1, 2, 3]],true)\n",
" t(firstLast6,[[13, 6, 1, 2, 3]],false)\n",
" t(firstLast6,[[13, 6, 1, 2, 6]],true)\n",
" t(firstLast6,[[3, 2, 1]],false)\n",
" t(firstLast6,[[3, 6, 1]],false)\n",
" t(firstLast6,[[3, 6]],true)\n",
" t(firstLast6,[[6]],true)\n",
" t(firstLast6,[[3]],false)\n",
" t(firstLast6,[[5, 6]],true)\n",
" t(firstLast6,[[5, 5]],false)\n",
" t(firstLast6,[[1, 2, 3, 4, 6]],true)\n",
" t(firstLast6,[[1, 2, 3, 4]],false)\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ sameFirstLast(1,2,3) == false\n",
"✔ sameFirstLast(1,2,3,1) == true\n",
"✔ sameFirstLast(1,2,1) == true\n",
"✔ sameFirstLast(7) == true\n",
"✔ sameFirstLast() == false\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ sameFirstLast(1,2,3,4,5,1) == true\n",
"✔ sameFirstLast(1,2,3,4,5,13) == false\n",
"✔ sameFirstLast(13,2,3,4,5,13) == true\n",
"✔ sameFirstLast(7,7) == true\n"
]
}
],
"source": [
"//Array-1\n",
"//sameFirstLast\n",
"/**Given an array of ints, return true if the array is length 1 or more, \n",
"and the first element and the last element are equal.**/\n",
"\n",
"function sameFirstLast(nums) {\n",
" var end = nums.length - 1;\n",
" if (nums.length >= 1){\n",
" return nums[0] == nums[end];\n",
" } else {return false;}\n",
"}\n",
"\n",
"t(sameFirstLast,[[1, 2, 3]],false)\n",
" t(sameFirstLast,[[1, 2, 3, 1]],true)\n",
" t(sameFirstLast,[[1, 2, 1]],true)\n",
" t(sameFirstLast,[[7]],true)\n",
" t(sameFirstLast,[[]],false)\n",
" t(sameFirstLast,[[1, 2, 3, 4, 5, 1]],true)\n",
" t(sameFirstLast,[[1, 2, 3, 4, 5, 13]],false)\n",
" t(sameFirstLast,[[13, 2, 3, 4, 5, 13]],true)\n",
" t(sameFirstLast,[[7, 7]],true)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ commonEnd(1,2,3,7,3) == true\n",
"✔ commonEnd(1,2,3,7,3,2) == false\n",
"✔ commonEnd(1,2,3,1,3) == true\n",
"✔ commonEnd(1,2,3,1) == true\n",
"✔ commonEnd(1,2,3,2) == false\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"/** Array-1 > commonEnd \n",
"\n",
"Given 2 arrays of ints, a and b, return true if they have the same first element or they \n",
"have the same last element. Both arrays will be length 1 or more. **/\n",
"\n",
"function commonEnd(a, b) {\n",
" \n",
" if (a.length >= 1 && b.length >= 1){\n",
" return (a[0] == b[0] || a[a.length - 1] == b[b.length - 1]);\n",
" }\n",
" return false;\n",
"}\n",
"\n",
" t(commonEnd,[[1, 2, 3], [7, 3]],true)\n",
" t(commonEnd,[[1, 2, 3], [7, 3, 2]],false)\n",
" t(commonEnd,[[1, 2, 3], [1, 3]],true)\n",
" t(commonEnd,[[1, 2, 3], [1]],true)\n",
" t(commonEnd,[[1, 2, 3], [2]],false)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ sum3(1,2,3) == 6\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ sum3(5,11,2) == 18\n",
"✔ sum3(7,0,0) == 7\n",
"✔ sum3(1,2,1) == 4\n",
"✔ sum3(1,1,1) == 3\n",
"✔ sum3(2,7,2) == 11\n"
]
}
],
"source": [
"/**Array-1 > sum3 \n",
"Given an array of ints length 3, return the sum of all the elements.**/\n",
"\n",
"function sum3(nums) {\n",
" return nums[0] + nums[1] + nums[2];\n",
"}\n",
"\n",
" t(sum3,[[1, 2, 3]],6)\n",
" t(sum3,[[5, 11, 2]],18)\n",
" t(sum3,[[7, 0, 0]],7)\n",
" t(sum3,[[1, 2, 1]],4)\n",
" t(sum3,[[1, 1, 1]],3)\n",
" t(sum3,[[2, 7, 2]],11)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ rotateLeft3(1,2,3) == 2,3,1\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ rotateLeft3(5,11,9) == 11,9,5\n",
"✔ rotateLeft3(7,0,0) == 0,0,7\n",
"✔ rotateLeft3(1,2,1) == 2,1,1\n",
"✔ rotateLeft3(0,0,1) == 0,1,0\n"
]
}
],
"source": [
"/**Array-1 > rotateLeft3 \n",
"\n",
"Given an array of ints length 3, return an array with the elements \"rotated left\" so {1, 2, 3} yields {2, 3, 1}**/\n",
"\n",
"function rotateLeft3(nums) {\n",
" var newA = [];\n",
" \n",
" newA[0] = nums[1];\n",
" newA[1] = nums[2];\n",
" newA[2] = nums[0];\n",
" \n",
" return newA;\n",
"}\n",
"\n",
"\n",
" t(rotateLeft3,[[1, 2, 3]],[2, 3, 1])\n",
" t(rotateLeft3,[[5, 11, 9]],[11, 9, 5])\n",
" t(rotateLeft3,[[7, 0, 0]],[0, 0, 7])\n",
" t(rotateLeft3,[[1, 2, 1]],[2, 1, 1])\n",
" t(rotateLeft3,[[0, 0, 1]],[0, 1, 0])\n"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ reverse3(3,2,1) == 3,2,1\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ reverse3(9,11,5) == 9,11,5\n",
"✔ reverse3(0,0,7) == 0,0,7\n",
"✔ reverse3(2,1,2) == 2,1,2\n",
"✔ reverse3(1,2,1) == 1,2,1\n",
"✔ reverse3(3,11,2) == 3,11,2\n",
"✔ reverse3(5,6,0) == 5,6,0\n",
"✔ reverse3(3,2,7) == 3,2,7\n"
]
}
],
"source": [
"//Array-1 > reverse3 \n",
"/**Given an array of ints length 3, return a new array \n",
"with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}.**/\n",
"\n",
"//UNTESTED\n",
"function reverse3(nums) {\n",
" return nums.reverse();\n",
"}\n",
"\n",
" t(reverse3,[[1, 2, 3]],[3, 2, 1])\n",
" t(reverse3,[[5, 11, 9]],[9, 11, 5])\n",
" t(reverse3,[[7, 0, 0]],[0, 0, 7])\n",
" t(reverse3,[[2, 1, 2]],[2, 1, 2])\n",
" t(reverse3,[[1, 2, 1]],[1, 2, 1])\n",
" t(reverse3,[[2, 11, 3]],[3, 11, 2])\n",
" t(reverse3,[[0, 6, 5]],[5, 6, 0])\n",
" t(reverse3,[[7, 2, 3]],[3, 2, 7])"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ maxEnd3(3,3,3) == 3,3,3\n",
"✔ maxEnd3(11,11,11) == 11,11,11\n",
"✔ maxEnd3(3,3,3) == 3,3,3\n",
"✔ maxEnd3(11,11,11) == 11,11,11\n",
"✔ maxEnd3(11,11,11) == 11,11,11\n",
"✔ maxEnd3(2,2,2) == 2,2,2\n",
"✔ maxEnd3(2,2,2) == 2,2,2\n",
"✔ maxEnd3(1,1,1) == 1,1,1\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"/**Array-1 > maxEnd3 \n",
"Given an array of ints length 3, figure out which is larger between the first and last elements \n",
"in the array, and set all the other elements to be that value. Return the changed array. **/\n",
"\n",
"//UNCHECKED\n",
"function maxEnd3(nums) {\n",
" var max = nums[0] > nums[2] ? nums[0] : nums[2];\n",
"\n",
" nums[0] = max;\n",
" nums[1] = max;\n",
" nums[2] = max;\n",
"\n",
" return nums;\n",
"}\n",
"\n",
"\n",
" t(maxEnd3,[[1, 2, 3]],[3, 3, 3])\n",
" t(maxEnd3,[[11, 5, 9]],[11, 11, 11])\n",
" t(maxEnd3,[[2, 11, 3]],[3, 3, 3])\n",
" t(maxEnd3,[[11, 3, 3]],[11, 11, 11])\n",
" t(maxEnd3,[[3, 11, 11]],[11, 11, 11])\n",
" t(maxEnd3,[[2, 2, 2]],[2, 2, 2])\n",
" t(maxEnd3,[[2, 11, 2]],[2, 2, 2])\n",
" t(maxEnd3,[[0, 0, 1]],[1, 1, 1])"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ sum2(1,2,3) == 3\n",
"✔ sum2(1,1) == 2\n",
"✔ sum2(1,1,1,1) == 2\n",
"✔ sum2(1,2) == 3\n",
"✔ sum2(1) == 1\n",
"✔ sum2() == 0\n",
"✔ sum2(4,5,6) == 9\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"/**Array-1 > sum2 \n",
"\n",
"Given an array of ints, return the sum of the first 2 elements in the array. \n",
"If the array length is less than 2, just sum up the elements that exist, \n",
"returning 0 if the array is length 0. **/\n",
"\n",
"function sum2(nums) {\n",
" if (nums.length == 0){\n",
" return 0;\n",
" }\n",
" \n",
" if (nums.length < 2) {\n",
" return nums[0];\n",
" } else {\n",
" return nums[0] + nums[1]\n",
" }\n",
"}\n",
"\n",
" t(sum2,[[1, 2, 3]],3)\n",
" t(sum2,[[1, 1]],2)\n",
" t(sum2,[[1, 1, 1, 1]],2)\n",
" t(sum2,[[1, 2]],3)\n",
" t(sum2,[[1]],1)\n",
" t(sum2,[[]],0)\n",
" t(sum2,[[4, 5, 6]],9)\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ middleWay(1,2,3,4,5,6) == 2,5\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ middleWay(7,7,7,3,8,0) == 7,8\n",
"✔ middleWay(5,2,9,1,4,5) == 2,4\n",
"✔ middleWay(1,9,7,4,8,8) == 9,8\n",
"✔ middleWay(1,2,3,3,1,4) == 2,1\n",
"✔ middleWay(1,2,3,4,1,1) == 2,1\n"
]
}
],
"source": [
"/**Array-1 > middleWay \n",
"Given 2 int arrays, a and b, each length 3, return a new \n",
"array length 2 containing their middle elements.**/\n",
"\n",
"function middleWay(a, b) {\n",
" var newArr = []\n",
" \n",
" newArr.push(a[1], b[1]);\n",
" \n",
" return newArr;\n",
"}\n",
"\n",
" t(middleWay,[[1, 2, 3], [4, 5, 6]],[2, 5])\n",
" t(middleWay,[[7, 7, 7], [3, 8, 0]],[7, 8])\n",
" t(middleWay,[[5, 2, 9], [1, 4, 5]],[2, 4])\n",
" t(middleWay,[[1, 9, 7], [4, 8, 8]],[9, 8])\n",
" t(middleWay,[[1, 2, 3], [3, 1, 4]],[2, 1])\n",
" t(middleWay,[[1, 2, 3], [4, 1, 1]],[2, 1])\n"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ makeEnds(1,2,3) == 1,3\n",
"✔ makeEnds(1,2,3,4) == 1,4\n",
"✔ makeEnds(7,4,6,2) == 7,2\n",
"✔ makeEnds(1,2,2,2,2,2,2,3) == 1,3\n",
"✔ makeEnds(7,4) == 7,4\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ makeEnds(7) == 7,7\n",
"✔ makeEnds(5,2,9) == 5,9\n",
"✔ makeEnds(2,3,4,1) == 2,1\n"
]
}
],
"source": [
"/**Array-1 > makeEnds \n",
"\n",
"Given an array of ints, return a new array length 2 containing the first and last \n",
"elements from the original array. The original array will be length 1 or more.**/\n",
"\n",
"// function makeEnds(nums) {\n",
"// var newArr = [];\n",
"// newArr.push(nums.shift(), nums.pop())\n",
"// return newArr;\n",
"// }\n",
"\n",
" function makeEnds(nums) {\n",
" var nArr = [];\n",
" nArr.push(nums[0], nums[nums.length - 1]);\n",
" \n",
" return nArr;\n",
"}\n",
"\n",
" t(makeEnds,[[1, 2, 3]],[1, 3])\n",
" t(makeEnds,[[1, 2, 3, 4]],[1, 4])\n",
" t(makeEnds,[[7, 4, 6, 2]],[7, 2])\n",
" t(makeEnds,[[1, 2, 2, 2, 2, 2, 2, 3]],[1, 3])\n",
" t(makeEnds,[[7, 4]],[7, 4])\n",
" t(makeEnds,[[7]],[7, 7])\n",
" t(makeEnds,[[5, 2, 9]],[5, 9])\n",
" t(makeEnds,[[2, 3, 4, 1]],[2, 1])\n"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ has23(2,5) == true\n",
"✔ has23(4,3) == true\n",
"✔ has23(4,5) == false\n",
"✔ has23(2,2) == true\n",
"✔ has23(3,2) == true\n",
"✔ has23(3,3) == true\n",
"✔ has23(7,7) == false\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ has23(3,9) == true\n",
"✔ has23(9,5) == false\n"
]
}
],
"source": [
"/**Array-1 > has23 \n",
"\n",
"Given an int array length 2, return true if it contains a 2 or a 3.**/\n",
"\n",
"function has23(nums) {\n",
"\n",
" if (nums.indexOf(2) != -1 || nums.indexOf(3) != -1){\n",
" return true\n",
" } else {\n",
" return false\n",
" }\n",
"}\n",
"\n",
" t(has23,[[2, 5]],true)\n",
" t(has23,[[4, 3]],true)\n",
" t(has23,[[4, 5]],false)\n",
" t(has23,[[2, 2]],true)\n",
" t(has23,[[3, 2]],true)\n",
" t(has23,[[3, 3]],true)\n",
" t(has23,[[7, 7]],false)\n",
" t(has23,[[3, 9]],true)\n",
" t(has23,[[9, 5]],false)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ no23(4,5) == true\n",
"✔ no23(4,2) == false\n",
"✔ no23(3,5) == false\n",
"✔ no23(1,9) == true\n",
"✔ no23(2,9) == false\n",
"✔ no23(1,3) == false\n",
"✔ no23(1,1) == true\n",
"✔ no23(2,2) == false\n",
"✔ no23(3,3) == false\n",
"✔ no23(7,8) == true\n",
"✔ no23(8,7) == true\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"/**Array-1 > no23 \n",
"\n",
"Given an int array length 2, return true if it does not contain a 2 or 3.**/\n",
"\n",
"function no23(nums) {\n",
" if (nums.indexOf(2) == -1 && nums.indexOf(3) == -1){\n",
" return true;\n",
" } else {\n",
" return false;\n",
" }\n",
"}\n",
"\n",
" t(no23,[[4, 5]],true)\n",
" t(no23,[[4, 2]],false)\n",
" t(no23,[[3, 5]],false)\n",
" t(no23,[[1, 9]],true)\n",
" t(no23,[[2, 9]],false)\n",
" t(no23,[[1, 3]],false)\n",
" t(no23,[[1, 1]],true)\n",
" t(no23,[[2, 2]],false)\n",
" t(no23,[[3, 3]],false)\n",
" t(no23,[[7, 8]],true)\n",
" t(no23,[[8, 7]],true)\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ makeLast(4,5,6) == 0,0,0,0,0,6\n",
"✔ makeLast(1,2) == 0,0,0,2\n",
"✔ makeLast(3) == 0,3\n",
"✔ makeLast(0) == 0,0\n",
"✔ makeLast(7,7,7) == 0,0,0,0,0,7\n",
"✔ makeLast(3,1,4) == 0,0,0,0,0,4\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ makeLast(1,2,3,4) == 0,0,0,0,0,0,0,4\n",
"✔ makeLast(1,2,3,0) == 0,0,0,0,0,0,0,0\n",
"✔ makeLast(2,4) == 0,0,0,4\n"
]
}
],
"source": [
"/**Array-1 > makeLast \n",
"\n",
"Given an int array, return a new array with double the length where its last element \n",
"is the same as the original array, and all the other elements are 0. The original array will be length 1 \n",
"or more. Note: by default, a new int array contains all 0's.**/\n",
"\n",
"function makeLast(nums) {\n",
" var i = 0;\n",
" var newNums = [];\n",
" var last = nums.slice(nums.length - 1); //calling slice on array returns array with 'sliced' portion\n",
" var double = nums.length * 2;\n",
" \n",
" while (i < double - 1) {\n",
" newNums.push(0);\n",
" i++;\n",
" }\n",
"\n",
" newNums.push(last[0]);\n",
" return newNums;\n",
"}\n",
"\n",
" t(makeLast,[[4, 5, 6]],[0, 0, 0, 0, 0, 6])\n",
" t(makeLast,[[1, 2]],[0, 0, 0, 2])\n",
" t(makeLast,[[3]],[0, 3])\n",
" t(makeLast,[[0]],[0, 0])\n",
" t(makeLast,[[7, 7, 7]],[0, 0, 0, 0, 0, 7])\n",
" t(makeLast,[[3, 1, 4]],[0, 0, 0, 0, 0, 4])\n",
" t(makeLast,[[1, 2, 3, 4]],[0, 0, 0, 0, 0, 0, 0, 4])\n",
" t(makeLast,[[1, 2, 3, 0]],[0, 0, 0, 0, 0, 0, 0, 0])\n",
" t(makeLast,[[2, 4]],[0, 0, 0, 4])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ double23(2,2) == true\n",
"✔ double23(3,3) == true\n",
"✔ double23(2,3) == false\n",
"✔ double23(3,2) == false\n",
"✔ double23(4,5) == false\n",
"✔ double23(2) == false\n",
"✔ double23(3) == false\n",
"✔ double23() == false\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ double23(3,4) == false\n"
]
}
],
"source": [
"/**Array-1 > double23 \n",
"\n",
"Given an int array, return true if the array contains 2 twice, or 3 twice. The array will be length 0, 1, or 2**/\n",
"\n",
"function double23(nums) {\n",
" if (nums.length < 2) {\n",
" return false;\n",
" } \n",
" else if ((nums[0] == 2 && nums[1] == 2) || (nums[0] == 3 && nums[1] == 3)) {\n",
" return true\n",
" } else {\n",
" return false;\n",
" }\n",
"}\n",
"\n",
" t(double23,[[2, 2]],true)\n",
" t(double23,[[3, 3]],true)\n",
" t(double23,[[2, 3]],false)\n",
" t(double23,[[3, 2]],false)\n",
" t(double23,[[4, 5]],false)\n",
" t(double23,[[2]],false)\n",
" t(double23,[[3]],false)\n",
" t(double23,[[]],false)\n",
" t(double23,[[3, 4]],false)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ fix23(1,2,0) == 1,2,0\n",
"✔ fix23(2,0,5) == 2,0,5\n",
"✔ fix23(1,2,1) == 1,2,1\n",
"✔ fix23(3,2,1) == 3,2,1\n",
"✔ fix23(2,2,0) == 2,2,0\n",
"✔ fix23(2,0,3) == 2,0,3\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"/**Array-1 > fix23 \n",
"\n",
"Given an int array length 3, if there is a 2 in the array immediately followed by a 3, \n",
"set the 3 element to 0. Return the changed array.**/\n",
"\n",
"function fix23(nums) {\n",
" for (var i = 0; i < nums.length; i++){\n",
" if (nums[i] == 2 && nums[i+1] == 3) {\n",
" var tri = nums.indexOf(3);\n",
" nums[tri] = 0;\n",
" }\n",
" }\n",
" return nums;\n",
"}\n",
"\n",
" t(fix23,[[1, 2, 3]],[1, 2, 0])\n",
" t(fix23,[[2, 3, 5]],[2, 0, 5])\n",
" t(fix23,[[1, 2, 1]],[1, 2, 1])\n",
" t(fix23,[[3, 2, 1]],[3, 2, 1])\n",
" t(fix23,[[2, 2, 3]],[2, 2, 0])\n",
" t(fix23,[[2, 3, 3]],[2, 0, 3])"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ start1(1,2,3,1,3) == 2\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ start1(7,2,3,1) == 1\n",
"✔ start1(1,2,) == 1\n",
"✔ start1(,1,2) == 1\n",
"✔ start1(7,) == 0\n",
"✔ start1(7,1) == 1\n",
"✔ start1(1,1) == 2\n",
"✔ start1(7,8) == 0\n",
"✔ start1(,) == 0\n",
"✔ start1(1,3,1) == 2\n"
]
}
],
"source": [
"/**Array-1 > start1 \n",
"Start with 2 int arrays, a and b, of any length. \n",
"Return how many of the arrays have 1 as their first element.**/\n",
"\n",
"function start1(a, b) {\n",
" \n",
" var count = 0;\n",
" if (a[0] == 1) {\n",
" count += 1;\n",
" }\n",
"\n",
" if (b[0] == 1){\n",
" count += 1;\n",
" }\n",
"\n",
" return count;\n",
"}\n",
"\n",
" t(start1,[[1, 2, 3], [1, 3]],2)\n",
" t(start1,[[7, 2, 3], [1]],1)\n",
" t(start1,[[1, 2], []],1)\n",
" t(start1,[[], [1, 2]],1)\n",
" t(start1,[[7], []],0)\n",
" t(start1,[[7], [1]],1)\n",
" t(start1,[[1], [1]],2)\n",
" t(start1,[[7], [8]],0)\n",
" t(start1,[[], []],0)\n",
" t(start1,[[1, 3], [1]],2)"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ biggerTwo(1,2,3,4) == 3,4\n",
"✔ biggerTwo(3,4,1,2) == 3,4\n",
"✔ biggerTwo(1,1,1,2) == 1,2\n",
"✔ biggerTwo(2,1,1,1) == 2,1\n",
"✔ biggerTwo(2,2,1,3) == 2,2\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ biggerTwo(1,3,2,2) == 1,3\n",
"✔ biggerTwo(6,7,3,1) == 6,7\n"
]
}
],
"source": [
"/**Array-1 > biggerTwo \n",
"Start with 2 int arrays, a and b, each length 2. Consider the sum of the values \n",
"in each array. Return the array which has the largest sum. In event of a tie, return a.**/\n",
"\n",
"function biggerTwo(a, b) { \n",
" if (b[0] + b[1] > a[0] + a[1]) {\n",
" \treturn b;\n",
" } \n",
" return a;\n",
"}\n",
"\n",
" t(biggerTwo,[[1, 2], [3, 4]],[3, 4])\n",
" t(biggerTwo,[[3, 4], [1, 2]],[3, 4])\n",
" t(biggerTwo,[[1, 1], [1, 2]],[1, 2])\n",
" t(biggerTwo,[[2, 1], [1, 1]],[2, 1])\n",
" t(biggerTwo,[[2, 2], [1, 3]],[2, 2])\n",
" t(biggerTwo,[[1, 3], [2, 2]],[1, 3])\n",
" t(biggerTwo,[[6, 7], [3, 1]],[6, 7])\n"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ makeMiddle(4) == 2,3\n",
"✔ makeMiddle(4,9) == 2,3\n",
"✔ makeMiddle() == 1,2\n",
"✔ makeMiddle(7) == 2,4\n",
"✔ makeMiddle(9,1) == 4,3\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"/**Array-1 > makeMiddle \n",
"\n",
"Given an array of ints of even length, return a new array length 2 containing \n",
"the middle two elements from the original array. The original array will be length 2 or more.**/\n",
"\n",
"function makeMiddle(nums) {\n",
" var first_half = nums.splice(0, nums.length/2);\n",
"\n",
" var one = first_half.pop();\n",
" var two = nums.shift();\n",
"\n",
" return [one, two]\n",
"\n",
"}\n",
"\n",
" t(makeMiddle,[[1, 2, 3, 4]],[2, 3])\n",
" t(makeMiddle,[[7, 1, 2, 3, 4, 9]],[2, 3])\n",
" t(makeMiddle,[[1, 2]],[1, 2])\n",
" t(makeMiddle,[[5, 2, 4, 7]],[2, 4])\n",
" t(makeMiddle,[[9, 0, 4, 3, 9, 1]],[4, 3])"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ plusTwo(1,2,3,4) == 1,2,3,4\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ plusTwo(4,4,2,2) == 4,4,2,2\n",
"✔ plusTwo(9,2,3,4) == 9,2,3,4\n"
]
}
],
"source": [
"/*Array-1 > plusTwo \n",
"Given 2 int arrays, each length 2, return a new array length 4 containing all their elements.**/\n",
"\n",
"function plusTwo(a, b) {\n",
" var duoArr = new Array();\n",
" duoArr[0]= a[0];\n",
" duoArr[1]= a[1];\n",
" duoArr[2]= b[0];\n",
" duoArr[3]= b[1];\n",
"\n",
" return duoArr;\n",
"}\n",
"\n",
" t(plusTwo,[[1, 2], [3, 4]],[1, 2, 3, 4])\n",
" t(plusTwo,[[4, 4], [2, 2]],[4, 4, 2, 2])\n",
" t(plusTwo,[[9, 2], [3, 4]],[9, 2, 3, 4])"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ swapEnds(4,2,3,1) == 4,2,3,1\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ swapEnds(3,2,1) == 3,2,1\n",
"✔ swapEnds(5,6,7,9,8) == 5,6,7,9,8\n",
"✔ swapEnds(9,1,4,1,5,3) == 9,1,4,1,5,3\n",
"✔ swapEnds(2,1) == 2,1\n",
"✔ swapEnds(1) == 1\n"
]
}
],
"source": [
"/**Array-1 > swapEnds \n",
"\n",
"Given an array of ints, swap the first and last elements in the array. Return the modified array. \n",
"The array length will be at least 1.**/\n",
"\n",
"function swapEnds(nums) {\n",
" if (nums.length < 2) {\n",
" \treturn nums;\n",
" }\n",
" var toStart = nums.pop();\n",
" var toEnd = nums.shift()\n",
"\n",
"\tnums.unshift(toStart);\n",
"\tnums.push(toEnd);\n",
"\n",
"\treturn nums;\n",
"\n",
"}\n",
"\n",
" t(swapEnds,[[1, 2, 3, 4]],[4, 2, 3, 1])\n",
" t(swapEnds,[[1, 2, 3]],[3, 2, 1])\n",
" t(swapEnds,[[8, 6, 7, 9, 5]],[5, 6, 7, 9, 8])\n",
" t(swapEnds,[[3, 1, 4, 1, 5, 9]],[9, 1, 4, 1, 5, 3])\n",
" t(swapEnds,[[1, 2]],[2, 1])\n",
" t(swapEnds,[[1]],[1])\n"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ midThree(1,2,3,4,5) == 2,3,4\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ midThree(8,6,7,5,3,0,9) == 7,5,3\n",
"✔ midThree(1,2,3) == 1,2,3\n"
]
}
],
"source": [
"/**Array-1 > midThree \n",
"Given an array of ints of odd length, return a new array length 3 containing the elements \n",
"from the middle of the array. The array length will be at least 3.**/\n",
"\n",
"function midThree(nums) {\n",
" var x = Math.floor(nums.length/2); // since 5/2 is 2.5, floor gives 2\n",
" return nums.slice(x-1, x+2);\n",
"}\n",
"\n",
" t(midThree,[[1, 2, 3, 4, 5]],[2, 3, 4])\n",
" t(midThree,[[8, 6, 7, 5, 3, 0, 9]],[7, 5, 3])\n",
" t(midThree,[[1, 2, 3]],[1, 2, 3])"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ maxTriple(1,2,3) == 3\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ maxTriple(1,5,3) == 5\n",
"✔ maxTriple(5,2,3) == 5\n",
"✔ maxTriple(1,2,3,1,1) == 3\n",
"✔ maxTriple(1,7,3,1,5) == 5\n",
"✔ maxTriple(5,1,3,7,1) == 5\n",
"✔ maxTriple(5,1,7,3,7,8,1) == 5\n",
"✔ maxTriple(5,1,7,9,7,8,1) == 9\n",
"✔ maxTriple(5,1,7,3,7,8,9) == 9\n",
"✔ maxTriple(2,2,5,1,1) == 5\n"
]
}
],
"source": [
"/**Array-1 > maxTriple \n",
"\n",
"Given an array of ints of odd length, look at the first, last, and middle values in the array and \n",
"return the largest. The array length will be a least 1.**/\n",
"\n",
"function maxTriple(nums) {\n",
" var mid = Math.floor(nums.length/2);\n",
" var start = nums[0]\n",
" var end = nums[nums.length - 1];\n",
" \t\n",
" if ( nums[mid] > start && nums[mid] > end){\n",
" \treturn nums[mid];\n",
" }\n",
" else if (start > nums[mid] && start > end) {\n",
" \treturn start;\n",
" } else {\n",
" \treturn end;\n",
" }\n",
"}\n",
"\n",
"\n",
" t(maxTriple,[[1, 2, 3]],3)\n",
" t(maxTriple,[[1, 5, 3]],5)\n",
" t(maxTriple,[[5, 2, 3]],5)\n",
" t(maxTriple,[[1, 2, 3, 1, 1]],3)\n",
" t(maxTriple,[[1, 7, 3, 1, 5]],5)\n",
" t(maxTriple,[[5, 1, 3, 7, 1]],5)\n",
" t(maxTriple,[[5, 1, 7, 3, 7, 8, 1]],5)\n",
" t(maxTriple,[[5, 1, 7, 9, 7, 8, 1]],9)\n",
" t(maxTriple,[[5, 1, 7, 3, 7, 8, 9]],9)\n",
" t(maxTriple,[[2, 2, 5, 1, 1]],5)\n"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ frontPiece(1,2,3) == 1,2\n",
"✔ frontPiece(1,2) == 1,2\n",
"✔ frontPiece(1) == 1\n",
"✔ frontPiece() == \n",
"✔ frontPiece(6,5,0) == 6,5\n",
"✔ frontPiece(6,5) == 6,5\n",
"✔ frontPiece(3,1,4,1,5) == 3,1\n",
"✔ frontPiece(6) == 6\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"/**Array-1 > frontPiece \n",
"\n",
"Given an int array of any length, return a new array of its first 2 elements. \n",
"If the array is smaller than length 2, use whatever elements are present.**/\n",
"\n",
"function frontPiece(nums) {\n",
" if (nums.length < 2) {\n",
" \treturn nums;\n",
" }\n",
"\n",
" return [nums[0], nums[1]];\n",
"}\n",
"\n",
" t(frontPiece,[[1, 2, 3]],[1, 2])\n",
" t(frontPiece,[[1, 2]],[1, 2])\n",
" t(frontPiece,[[1]],[1])\n",
" t(frontPiece,[[]],[])\n",
" t(frontPiece,[[6, 5, 0]],[6, 5])\n",
" t(frontPiece,[[6, 5]],[6, 5])\n",
" t(frontPiece,[[3, 1, 4, 1, 5]],[3, 1])\n",
" t(frontPiece,[[6]],[6])\n"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ unlucky1(1,3,4,5) == true\n",
"✔ unlucky1(2,1,3,4,5) == true\n",
"✔ unlucky1(1,1,1) == false\n",
"✔ unlucky1(1,3,1) == true\n",
"✔ unlucky1(1,1,3) == true\n",
"✔ unlucky1(1,2,3) == false\n",
"✔ unlucky1(3,3,3) == false\n",
"✔ unlucky1(1,3) == true\n",
"✔ unlucky1(1,4) == false\n",
"✔ unlucky1(1) == false\n",
"✔ unlucky1() == false\n",
"✔ unlucky1(1,1,1,3,1) == false\n",
"✔ unlucky1(1,1,3,1,1) == true\n",
"✔ unlucky1(1,1,1,1,3) == true\n",
"✔ unlucky1(1,4,1,5) == false\n",
"✔ unlucky1(1,1,2,3) == false\n",
"✔ unlucky1(2,3,2,1) == false\n",
"✔ unlucky1(2,3,1,3) == true\n",
"✔ unlucky1(1,2,3,4,1,3) == true\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"/**Array-1 > unlucky1 \n",
"We'll say that a 1 immediately followed by a 3 in an array is an \"unlucky\" 1. \n",
"Return true if the given array contains an unlucky 1 in the first 2 or last 2 positions in the array.**/\n",
"\n",
"function unlucky1(nums) {\n",
"\n",
" var last2 = nums.slice(-2);\n",
"\n",
" var first3 = nums.slice(0, 3);\n",
"\n",
" var newArr = first3.concat(last2);\n",
"\n",
" for (var i = 0; i < first3.length; i++) {\n",
"\tif (first3[i] == 1 && first3[i+1] == 3)\n",
" \t\treturn true;\n",
" }\n",
" \n",
" for(var i = 0; i < last2.length; i++) {\n",
" if (last2[i] == 1 && last2[i+1] == 3)\n",
" \t\treturn true;\n",
" \n",
" }\n",
" return false;\n",
"}\n",
"\n",
" t(unlucky1,[[1, 3, 4, 5]],true)\n",
" t(unlucky1,[[2, 1, 3, 4, 5]],true)\n",
" t(unlucky1,[[1, 1, 1]],false)\n",
" t(unlucky1,[[1, 3, 1]],true)\n",
" t(unlucky1,[[1, 1, 3]],true)\n",
" t(unlucky1,[[1, 2, 3]],false)\n",
" t(unlucky1,[[3, 3, 3]],false)\n",
" t(unlucky1,[[1, 3]],true)\n",
" t(unlucky1,[[1, 4]],false)\n",
" t(unlucky1,[[1]],false)\n",
" t(unlucky1,[[]],false)\n",
" t(unlucky1,[[1, 1, 1, 3, 1]],false)\n",
" t(unlucky1,[[1, 1, 3, 1, 1]],true)\n",
" t(unlucky1,[[1, 1, 1, 1, 3]],true)\n",
" t(unlucky1,[[1, 4, 1, 5]],false)\n",
" t(unlucky1,[[1, 1, 2, 3]],false)\n",
" t(unlucky1,[[2, 3, 2, 1]],false)\n",
" t(unlucky1,[[2, 3, 1, 3]],true)\n",
" t(unlucky1,[[1, 2, 3, 4, 1, 3]],true)\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ unlucky1(1,3,4,5) == true\n",
"✔ unlucky1(2,1,3,4,5) == true\n",
"✔ unlucky1(1,1,1) == false\n",
"✔ unlucky1(1,3,1) == true\n",
"✔ unlucky1(1,1,3) == true\n",
"✔ unlucky1(1,2,3) == false\n",
"✔ unlucky1(3,3,3) == false\n",
"✔ unlucky1(1,3) == true\n",
"✔ unlucky1(1,4) == false\n",
"✔ unlucky1(1) == false\n",
"✔ unlucky1() == false\n",
"✔ unlucky1(1,1,1,3,1) == false\n",
"✔ unlucky1(1,1,3,1,1) == true\n",
"✔ unlucky1(1,1,1,1,3) == true\n",
"✔ unlucky1(1,4,1,5) == false\n",
"✔ unlucky1(1,1,2,3) == false\n",
"✔ unlucky1(2,3,2,1) == false\n",
"✔ unlucky1(2,3,1,3) == true\n",
"✔ unlucky1(1,2,3,4,1,3) == true\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"//ANOTHER WAY TO DO UNLUCKY1\n",
"\n",
"function unlucky1(nums) {\n",
"\n",
" var sections = [ // array of arrays \n",
" nums.slice(-2),\n",
" nums.slice(0,3)\n",
" ]\n",
"\n",
" for (let section of sections) {\n",
" for (var i = 0; i < section.length; i++) {\n",
" if (section[i] == 1 && section[i + 1] == 3)\n",
" return true;\n",
" }\n",
" }\n",
" return false;\n",
"}\n",
"\n",
" t(unlucky1,[[1, 3, 4, 5]],true)\n",
" t(unlucky1,[[2, 1, 3, 4, 5]],true)\n",
" t(unlucky1,[[1, 1, 1]],false)\n",
" t(unlucky1,[[1, 3, 1]],true)\n",
" t(unlucky1,[[1, 1, 3]],true)\n",
" t(unlucky1,[[1, 2, 3]],false)\n",
" t(unlucky1,[[3, 3, 3]],false)\n",
" t(unlucky1,[[1, 3]],true)\n",
" t(unlucky1,[[1, 4]],false)\n",
" t(unlucky1,[[1]],false)\n",
" t(unlucky1,[[]],false)\n",
" t(unlucky1,[[1, 1, 1, 3, 1]],false)\n",
" t(unlucky1,[[1, 1, 3, 1, 1]],true)\n",
" t(unlucky1,[[1, 1, 1, 1, 3]],true)\n",
" t(unlucky1,[[1, 4, 1, 5]],false)\n",
" t(unlucky1,[[1, 1, 2, 3]],false)\n",
" t(unlucky1,[[2, 3, 2, 1]],false)\n",
" t(unlucky1,[[2, 3, 1, 3]],true)\n",
" t(unlucky1,[[1, 2, 3, 4, 1, 3]],true)\n"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 4, 5 ]\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var arr = [1, 2, 3, 4, 5];\n",
"\n",
"var a = arr.slice(- 2);\n",
"\n",
"console.log(a)"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ make2(4,5,1,2,3) == 4,5\n",
"✔ make2(4,1,2,3) == 4,1\n",
"✔ make2(,1,2) == 1,2\n",
"✔ make2(1,2,) == 1,2\n",
"✔ make2(3,1,2,3) == 3,1\n",
"✔ make2(3,1) == 3,1\n",
"✔ make2(3,1,4,) == 3,1\n",
"✔ make2(1,1) == 1,1\n",
"✔ make2(1,2,3,7,8) == 1,2\n",
"✔ make2(7,8,1,2,3) == 7,8\n",
"✔ make2(7,1,2,3) == 7,1\n",
"✔ make2(5,4,2,3,7) == 5,4\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"/**Array-1 > make2 \n",
"\n",
"Given 2 int arrays, a and b, return a new array length 2 containing, as much as will fit, \n",
"the elements from a followed by the elements from b. The arrays may be any length, including 0, \n",
"but there will be 2 or more elements available between the 2 arrays.**/\n",
"\n",
"function make2(a, b) {\n",
" var newAr = a.concat(b);\n",
" \n",
" return [newAr[0], newAr[1]];\n",
"}\n",
"\n",
" t(make2,[[4, 5], [1, 2, 3]],[4, 5])\n",
" t(make2,[[4], [1, 2, 3]],[4, 1])\n",
" t(make2,[[], [1, 2]],[1, 2])\n",
" t(make2,[[1, 2], []],[1, 2])\n",
" t(make2,[[3], [1, 2, 3]],[3, 1])\n",
" t(make2,[[3], [1]],[3, 1])\n",
" t(make2,[[3, 1, 4], []],[3, 1])\n",
" t(make2,[[1], [1]],[1, 1])\n",
" t(make2,[[1, 2, 3], [7, 8]],[1, 2])\n",
" t(make2,[[7, 8], [1, 2, 3]],[7, 8])\n",
" t(make2,[[7], [1, 2, 3]],[7, 1])\n",
" t(make2,[[5, 4], [2, 3, 7]],[5, 4])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ front11(1,2,3,7,9,8) == 1,7\n",
"✔ front11(1,2) == 1,2\n",
"✔ front11(1,7,) == 1\n"
]
},
{
"data": {
"text/plain": [
"undefined"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"✔ front11(,2,8) == 2\n",
"✔ front11(,) == \n",
"✔ front11(3,1,4,1,9) == 3,1\n",
"✔ front11(1,4,1,9,) == 1\n"
]
}
],
"source": [
"/**Array-1 > front11 \n",
"\n",
"Given 2 int arrays, a and b, of any length, return a new array with the first element \n",
"of each array. If either array is length 0, ignore that array.**/\n",
"\n",
"function front11(a, b) {\n",
" var sections = [\n",
"\ta,\n",
"\tb\n",
" ]\n",
"\n",
" var newA = [];\n",
"\n",
" for (let section of sections) {\n",
" if (section.length == 0) {\n",
"\n",
" } else {\n",
" newA.push(section[0]);\n",
" }\n",
" }\n",
" \n",
" return newA;\n",
"}\n",
"\n",
" t(front11,[[1, 2, 3], [7, 9, 8]],[1, 7])\n",
" t(front11,[[1], [2]],[1, 2])\n",
" t(front11,[[1, 7], []],[1])\n",
" t(front11,[[], [2, 8]],[2])\n",
" t(front11,[[], []],[])\n",
" t(front11,[[3], [1, 4, 1, 9]],[3, 1])\n",
" t(front11,[[1, 4, 1, 9], []],[1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"\n",
"\n"
]
}
],
"metadata": {
"anaconda-cloud": {},
"hide_input": false,
"kernelspec": {
"display_name": "Babel (Node.js)",
"language": "babel",
"name": "babel"
},
"language_info": {
"file_extension": "js",
"mimetype": "application/javascript",
"name": "javascript",
"version": "5.9.0"
},
"toc": {
"toc_cell": false,
"toc_number_sections": true,
"toc_threshold": 6,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment