Last active
September 15, 2022 19:01
-
-
Save manuelgeek/96bbee166a8a8ce63c758f9e22416f53 to your computer and use it in GitHub Desktop.
Toptal codility
This file contains 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
// There are N blocks, numbered from 0 to n-1, arranged in a row. | |
// A couple of frogs were sitting together on one block when they had a | |
// terrible. Now they want to jump away from one another so that the | |
// distance between them will be as large as possible. The distance between | |
// blocks numbered J and K, where J <= K, is computed as K -J+1 | |
function solutions(blocks) { | |
let ans= 0; | |
for(let i=0; i<blocks.length; i++){ | |
let leftMax=i; | |
let rightMax=i; | |
//go to left | |
while(leftMax-1 >= 0 && blocks[leftMax] <= blocks[leftMax-1]){ | |
leftMax--; | |
} | |
while(rightMax+1 < blocks.length && blocks[rightMax] <= blocks[rightMax+1]){ | |
rightMax++; | |
} | |
ans= Math.max(ans, rightMax - leftMax +1); | |
} | |
return ans; | |
} | |
} |
This file contains 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
// Task Description | |
// A vending machine has the following denominations: 1c, 5c, 10c, 25c, 50c, and $1. | |
// Your task is to write a program that will be used in a vending machine to return change. | |
// Assume that the vending machine will always want to return the least number of coins or notes. | |
// Devise a function getChange(M, P) where M is how much money was inserted into the machine and P | |
// the price of the item selected, that returns an array of integers representing the number of each | |
// denomination to return. | |
// Example: | |
// getChange(5, 0.99) // should return [1,0,0,0,0,4] | |
// getChange(3.14, 1.99) // should return [0,1,1,0,0,1] | |
// getChange(3, 0.01) // should return [4,0,2,1,1,2] | |
// getChange(4, 3.14) // should return [1,0,1,1,1,0] | |
// getChange(0.45, 0.34) // should return [1,0,1,0,0,0] | |
function getChange(M, P) { | |
// const demoninations = [0.01, 0.05, 0.1, 0.25, 0.5, 1] | |
const demoninations = [1, 0.5, 0.25, 0.1, 0.05, 0.01] | |
let change = M - P | |
let result = [] | |
for (i = 0; i < demoninations.length; i++) { | |
let count = 0 | |
while (change >= demoninations[i]) { | |
console.log({ dem: demoninations[i] }) | |
change = (change - demoninations[i]).toFixed(2) // toFixed() | |
console.log({ change }) | |
count += 1 | |
} | |
console.log({ change1: change }) | |
result.push(count) | |
} | |
console.log({ result: result.reverse() }) | |
return result.reverse() | |
} | |
getChange(3.14, 1.99) |
This file contains 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
//. There is a forum that has alimit oh K charaxters per entry | |
function solution(message, K) { | |
// write your code in JavaScript (Node.js 8.9.4)let trimedMessage = message.trim() | |
if(message.length <= K) return message | |
return message.substr(0, message.lastIndexOf(' ', K)) | |
} |
This file contains 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 group of friends are going to a holiday together, Car seats and People | |
function solution(P, S) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
let allPass = P.reduce((p,c) => p + c) | |
S.sort().reverse() | |
let index = 0; | |
while (allPass > 0) { | |
allPass = allPass - S[index] | |
index += 1 | |
} | |
return index | |
} | |
// https://stackblitz.com/edit/minimum-car-code-challenge?file=readme.md,mincar.test.js,mincar.js |
This file contains 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
// AN industrial company has N factories< Polution, | |
function solution(A) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
let totalsExpected = Math.floor(A.reduce((p, c) => p + c) / 2) | |
let totalFiltered = 0 | |
let allFilters = 0 | |
const newA = A.sort().reverse() | |
for (let i = 0; i < newA.length; i++) { | |
allFilters ++ | |
totalFiltered += Math.ceil(newA[i] / 2) | |
if(totalFiltered >= totalsExpected) break; | |
} | |
return allFilters | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment