Skip to content

Instantly share code, notes, and snippets.

@evandocarmo
evandocarmo / consecutiveDistance.js
Last active October 10, 2017 00:55
We'll call the consecutive distance rating of an integer sequence the sum of the distances between consecutive integers. Consider the sequence 1 7 2 11 8 34 3. 1 and 2 are consecutive integers, but their distance apart in the sequence is 2. 2 and 3 are consecutive integers, and their distance is 4. The distance between 7 and 8 is 3. The sum of t…
//O(n²)
function consecutiveDistance(arr) {
let total = 0;
for(let i = 0; i < array.length; i++) {
for(let j = i + 1; j < array.length; j++) {
if(arr[i] === arr[j] - 1 || arr[j] === arr[i] - 1) {
total += (j + 1) - (i + 1);
}
}
}
@evandocarmo
evandocarmo / stacksHeights.js
Last active October 6, 2017 21:33
Equal Stacks
h1 = readLine().split(' ');
stacks['h1']['array'] = h1.map(Number);
h2 = readLine().split(' ');
stacks['h2']['array'] = h2.map(Number);
h3 = readLine().split(' ');
stacks['h3']['array'] = h3.map(Number);
function pass() {
let max = 0;
let maxName = '';
@evandocarmo
evandocarmo / csvToMongo.js
Created August 26, 2017 00:09
How to read, parse and save csv objects to mongo database
const mongoose = require('mongoose');
const connection = mongoose.connect(process.env.MONGO_URL);
const Model = require('./models/model');
//read csv file and save to database
const csv = require('csv');
const fs = require('fs');
let file = './objects.csv';
let parser = csv.parse({delimiter: ','},(err, data)=>{
/* The goal of this gist is to try and clarify the following code... */
import {
Injectable
} from '@angular/core';
import {
Observable
} from 'rxjs/Observable';
import {
Subject
} from 'rxjs/Subject';
/* The goal of this gist is to try and clarify the following code... */
import {
Injectable
} from '@angular/core';
import {
Observable
} from 'rxjs/Observable';
import {
Subject
} from 'rxjs/Subject';
@evandocarmo
evandocarmo / queueWithTwoStacks.js
Last active August 15, 2017 00:50
Implementation of a Queue class using two stacks as a challenge for HackerRank
class Queue {
constructor() {
this.inStack = [];
this.outStack = [];
}
enqueue(element) {
this.inStack.push(element);
}
dequeue() {
if (!this.outStack.length)
var closingMap = {
'{': '}',
'[': ']',
'(': ')'
};
function isBalancedBracketSequence(expression) {
var length = expression.length;
if (length <= 1 || length % 2 !== 0) {
return false;
ANSWER: The intention of the code is to hide block letter b, where the target of the anchor tag is NOT blank. However,
there is a small typo causing a bug. The original code is as follows: **/
$("div a[target!='_blank"]").hide();
The double quotes after blank cose the selector, causing a bug. The correct code would be:
$("div a[target!='_blank']").hide();
Which should work fine.
/*
1. Write a SQL query that returns names and emails of customers whose credit cards are about to get expired (in the next 14 days). Data model looks like this:*
1. Table Customer (id, name, address, email)
2. Table CustomerCreditCards(id,customer_id, ccnumber, cvs, expirationdate)
*/
SELECT name, email FROM Customer
INNER JOIN CustomerCreditCards
ON Customer.id = CustomerCreditCards.customer_id
WHERE DATEDIFF(CustomerCreditCards.expirationdate,NOW()) <= 14;
@evandocarmo
evandocarmo / carthook-1.js
Last active August 12, 2017 00:38
Carthook Support Engineer Interview
//2. Write a short javascript function that displays the first non repeating character in a string.Example: If the string is "Alphabet", it would print out the letter "l"
function firstUnrepeatedLetter (word){
word = word.toLowerCase();
let hash = {};
for(let char of word){
if(!hash[char])
hash[char] = 1;
else
hash[char] += 1;
}