Skip to content

Instantly share code, notes, and snippets.

View nicaralava's full-sized avatar

Jean Nica Ralava nicaralava

View GitHub Profile
@nicaralava
nicaralava / the-descent.php
Created April 12, 2018 02:52
codingame/puzzle/the-descent
<?php
/**
* The while loop represents the game.
* Each iteration represents a turn of the game
* where you are given inputs (the heights of the mountains)
* and where you have to print an output (the index of the mountain to fire on)
* The inputs you are given are automatically updated according to your last actions.
**/
$table = array();
@nicaralava
nicaralava / temperatures.php
Created April 10, 2018 19:09
codingame/puzzle/temperatures
<?php
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
fscanf(STDIN, "%d",
$n // the number of temperatures to analyse
);
$inputs = fgets(STDIN);
@nicaralava
nicaralava / update-records.js
Created February 24, 2018 13:22
Record Collection
var collection = {
"2548": {
"album": "Slippery When Wet",
"artist": "Bon Jovi",
"tracks": [
"Let It Rock",
"You Give Love a Bad Name"
]
},
"2468": {
@nicaralava
nicaralava / add-together.js
Last active February 23, 2018 15:51
Arguments Optional
function addTogether() {
var _arguments = arguments;
if (typeof arguments[0] !== 'number' || arguments[1] && typeof arguments[1] !== 'number') {
return undefined;
}
if (arguments[1]) {
return arguments[0] + arguments[1];
}
return function (val) {
return addTogether(_arguments[0], val);
@nicaralava
nicaralava / unite-unique.js
Created February 23, 2018 06:54
Sorted Union | Get all unique values in an array (remove duplicates)
function uniteUnique(arr) {
var newArray = [];
var numbArg = arguments.length;
var numbVal;
for (var i = 0; i < numbArg; i++){
numbVal = arguments[i].length;
for (var j = 0; j < numbVal; j++ ){
newArray.push(arguments[i][j]);
}
}
@nicaralava
nicaralava / what-is-in-a-name.js
Created February 23, 2018 06:31
Wherefore art thou
function whatIsInAName(collection, source) {
var arr = [];
var propertyListe = Object.keys(source);
arr = collection.filter(function (val){
return propertyListe.every(function (propertyValue){
return source[propertyValue] === val[propertyValue];
});
});
return arr;
}
@nicaralava
nicaralava / binary-agent.js
Created February 22, 2018 11:56
Binary Agent
function binaryAgent(str) {
var arraySplited = str.split(" ");
var newArray = arraySplited.map(function(val){
return String.fromCharCode(parseInt(val, 2));
});
return newArray.join("");
}
binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");
@nicaralava
nicaralava / find-element.js
Created February 22, 2018 10:21
Finders Keepers
function findElement(arr, func) {
var num = 0;
num = arr.filter(func).shift();
return num;
}
findElement([1, 3, 5, 8, 9, 10], function(num){ return num % 2 === 0; });
@nicaralava
nicaralava / smallest-commons.js
Created February 22, 2018 08:30
Smallest Common Multiple
function smallestCommons(arr) {
var max = Math.max(arr[0], arr[1]);
var min = Math.min(arr[0], arr[1]);
var res = min;
for(var i = min; i<= max; i++){
if(res % i !== 0){
res += min;
i = min;
}
}
@nicaralava
nicaralava / sum-primes.js
Created February 22, 2018 07:53
Sum All Primes
function sumPrimes(num) {
var sum = 2;
for ( var i = 2; i <= num; i++) {
for (var j = 2; j < i; j++){
if (i % j === 0){
break;
}
else if (j === i-1) {
sum += i;
}