Skip to content

Instantly share code, notes, and snippets.

View yvan-sraka's full-sized avatar

Yvan Sraka yvan-sraka

View GitHub Profile
// PHP
<?php
function max($tab) {
$result = 0;
for ($i = 0; count($tab); $i++) {
if ($tab[$i] > $result) {
$result = $tab[$i];
}
}
// La recherche dichotomique en JS
// ENTREE -> Algo -> SORTIE
// ENTREE: dictionnaire + le mot à chercher
// SORTIE: -1 OU position
// cf. Code in Game / HackerRank
// dummyIndexOf 1s -> 200000 2s -> 400000
/* ###### CONSTANT ###### */
var BILLS = [200, 100, 50, 20, 10, 5, 2, 1] // ARRAY of bills with really cheap bills like coins
/* ###### SMART ALGORITHM ###### */
var cash_machine = function (amount) { // FUNCTION
var i = 0; // iterator
while (amount > 0) { // check if their is still money to give for the customer
if (BILLS[i] <= amount) { // try with the larger bill
console.log("Cash Machine give me a bill of " + BILLS[i] + "€"); // GIVE ME MY MONEY!!!
amount = amount - BILLS[i]; // reduce the amount of money we still have to gave for the customer
/* ###### CONSTANT ###### */
var BILLS = [200, 100, 50, 20, 10, 5, 2, 1] // ARRAY of bills with really cheap bills like coins
/* ###### SMART ALGORITHM ###### */
var cash_machine = function (amount) { // FUNCTION
var i = 0; // iterator
while (amount > 0) { // check if their is still money to give for the customer
if (BILLS[i] <= amount) { // try with the larger bill
console.log("Cash Machine give me a bill of " + BILLS[i] + "€"); // GIVE ME MY MONEY!!!
amount = amount - BILLS[i]; // reduce the amount of money we still have to gave for the customer
/*
* Factorial
* n! = 1 * 2 * .. * n
*
* 5! = 5 * 4 * 3 * 2 * 1 = 120
*/
// MATHS:
// 0! = 1
// n! = n * (n - 1)!
/*
* INPUT: a number N
*
* OUPUT: f(N) where f is the fibonacci function :
*
* 0 1 1 2 3 5 8 13 21 34 ..
*
* f(0) => 0
* f(1) => 1
* f(2) => 0 + 1 = 1
// Definition of Map function
function Map(arr, func) {
m_arr = [];
for (var i = 0; i < arr.length; ++i)
m_arr.push(func(arr[i]));
return m_arr;
}
// Definition of Filter function
function Filter(arr, func) {
// PROTOTYPE //
// setTimeout(Function, Number);
///////////////
// Example
// 1.
setTimeout(
// first parameter
// (which is an anonymous function without parameter)
/*
* Triangle
*
* INPUT:
* - a number N
*
* OUPUT: (eg for N=5)
* #
* ##
* ###
var T = [];
T[0] = 0;
T[1] = 1;
for (var i = 2; i < 100; i++) {
T[i] = T[i-2] + T[i-1];
}
function fib(x) {