Skip to content

Instantly share code, notes, and snippets.

@mmloveaa
mmloveaa / 3-28
Last active April 3, 2016 06:55
3-28
Complete the function arrayDepth that will examine an array and determine the depth of the most deeply nested array inside. An array with no nested arrays inside has a depth of 1. The array passed to your function can contain any data types.
Input Format:
The function will have one argument:
arr - an array which may contain any data types, including more similar arrays.
Output Format:
The function will return a number representing the deepest level of nesting in the given array.
Any array that doesn't have any nested arrays inside, including an empty array, has a depth of 1.
@mmloveaa
mmloveaa / Binary to Decimal
Last active April 3, 2016 07:03
Binary to Decimal
String.prototype.binaryToDecimal = function() {
var array = Array.prototype.slice.call (this)
var revArray = array.reverse()
var answer = 0;
revArray = forEach(function(elem, index) {
var coeff = Number(elem)
var value = coeff * Math.power(2,index)
answer += value
})
@mmloveaa
mmloveaa / 3-25
Last active April 3, 2016 06:43
3-25
The complement of a number is defined as the bit-wise inversion: starting from the highest-order 1-bit, change every 1 to 0 and every 0 to 1.
For example, the number N = 5 is represented as 101 in binary. The binary complement of N is 010, which is 2 in decimal notation. Similarly, the complement of N = 50 is 13. Complete the function getIntegerComplement().
This function accepts N as parameter and should return the complement of N. Your task is to complete the body of the function provided so that it returns the correct output.
Constraints :
0 ≤ N ≤ 100,000.
Sample #1:
@mmloveaa
mmloveaa / 3-24 solution
Last active April 3, 2016 06:42
3-24 solution
function closestNumbers(arr) {
arr.sort((a,b) => a-b );
var smallestGap = arr.reduce(function(gap, num , i) {
var thisGap = arr[i+1] - num;
return thisGap < gap ? thisGap : gap;
// num - arr[i+1]
}, infinity);
@mmloveaa
mmloveaa / 3-24
Last active April 3, 2016 06:42
3-24
Sorting is useful as a first step in many different operations. For example, searching for a specific elements is easier on a sorted data structure. But search is not the only task facilitated by an initial sort.
Given an array of unsorted numbers A={ a1, a2, a3,.. ,aN}, can you find the pair of numbers that has the smallest absolute difference between its elements? If there are multiple pairs that have the same absolute distance as the minimum, find them all.
Constraints
1 ≤ N ≤ 2x105
-107 ≤ x ≤ 107, where x ∈ array
No repetitions: ai≠ aj where 1 ≤ i < j ≤N
Input Format
@mmloveaa
mmloveaa / animals pics
Created March 24, 2016 07:17
animals pics
http://cdn.grumpycats.com/wp-content/uploads/2012/10/IMG_2419.jpg
http://www.stuff.co.nz/content/dam/images/1/9/3/v/a/m/image.related.StuffLandscapeSixteenByNine.620x349.193ugj.png/1452447866954.jpg
http://az616578.vo.msecnd.net/files/2016/03/13/6359346621543673841267054763_pup.jpg
http://dreamatico.com/data_images/dog/dog-5.jpg
@mmloveaa
mmloveaa / 3-22 my solution
Created March 22, 2016 19:33
3-22 my solution
function solvePuzzle(num) {
console.log(num)
var str = num.toString().split("")
console.log(str)
var arr =[]
@mmloveaa
mmloveaa / 3-21 Test IP address
Created March 22, 2016 16:52
3-21 Test IP address
testing
@mmloveaa
mmloveaa / 3-22 Count the holes
Last active October 24, 2018 09:36
3-22 Count the holes
24 -> 1
1264 -> 2
9899 -> 5
349 -> x
You swiftly determine that the puzzle can be solved by counting the number of holes in the digits of a number. e.g. 1,2,3,5,7 have no holes, 0, 4, 6, 9 have 1 hole each, and 8 has 2 holes. 2 is therefore the value of x to the last line.
@mmloveaa
mmloveaa / chocolate - 3_21
Last active March 13, 2018 05:41
chocolate
Sam loves chocolates and starts buying them on the 1st day of the year. Each day of the year, x, is numbered from 1 to Y. On days when x is odd, Sam will buy x chocolates; on days when x is even, Sam will not purchase any chocolates.
Complete the calculate function in the editor so that for each day Ni (where 1 ≤ x ≤ N ≤ Y) in array arr, the number of chocolates Sam purchased (during days 1 through N) is printed on a new line. This is a function-only challenge, so input is handled for you by the locked stub code in the editor.
Input Format
The calculate function takes an array of integers as a parameter.
The locked code in the editor handles reading the following input from stdin, assembling it into an array of integers (arr), and calling calculate(arr).
The first line of input contains an integer, T (the number of test cases). Each line i of the T subsequent lines describes the ith test case as an integer, Ni (the number of days).