This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createHeap (arr) { | |
var startIndex = Math.floor(arr.length / 2) - 1 | |
while(startIndex >= 0) { | |
heapify(arr, startIndex) | |
startIndex -= 1 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//var arr = [7, 5, 1, 4, 2, 3,8] | |
function createHeap (arr) { | |
var startIndex = Math.floor(arr.length / 2) - 1 | |
while(startIndex >= 0) { | |
heapify(arr, startIndex) | |
startIndex -= 1 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arr = [5,4,3,2,1] | |
function insertionSort (arr) { | |
if (arr.length === 0) { | |
return | |
} | |
start = 1 | |
while(start <= arr.length-1) { | |
var index = start -1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arr = [2,1,3,4,2,5,6,3,7] | |
mergeSort(arr, 0, arr.length - 1) | |
function mergeSort (arr, low, high) { | |
if (low == high) { | |
return [arr[low]] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import FirstName from './FirstName' | |
import LastName from './LastName' | |
class NameForm extends Component { | |
constructor (props) { | |
super(props) | |
this.state = {showFirstName: false, showLastName: false} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = "this is name a good , name is good, name" | |
arr = ['abhinav', 'kankia', 'sid'] | |
count = 0 | |
a.gsub!("name") do |val| | |
val = arr[count] | |
count += 1 | |
val | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makeHash (arr) { | |
var map = {} | |
for (var i=0;i<arr.length;i++) { | |
if (map.hasOwnProperty(arr[i])) { | |
map[arr[i]] += 1 | |
} else { | |
map[arr[i]] = 1 | |
} | |
} | |
return map |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function countOfNumbers (arr) { | |
var divisor = arr.length + 1 | |
var returnArr = [] | |
for(var i=0;i<arr.length;i++) { | |
arr[arr[i] % divisor - 1] += divisor | |
} | |
for(var i=0;i<arr.length;i++) { | |
returnArr.push([i+1, Math.floor(arr[i] / divisor) ]) | |
} | |
return returnArr |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* given an array and value as k find if there is a subarray with sum = k */ | |
function findSubArray (arr, k) { | |
var sumArr = [] | |
var sum = 0 | |
for(var i=0;i<arr.length;i++) { | |
sum += arr[i] | |
if (sum === k) { | |
return [0, i] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function dateDisplay (milliseconds) { | |
var currentTime = Date.now() | |
var diff = currentTime - milliseconds | |
var divisors = [1000, 60, 60, 24] | |
var labels = [' milliseconds ago', ' seconds ago', ' minutes ago', ' hours ago', ' days ago'] | |
for(var i=0;i<divisors.length;i++){ | |
if (diff/divisors[i] < 1) { | |
return diff/divisors[i] + labels[i] |