- function alphabetRange (start, end, step = 1) {
- return new Array(Math.ceil((end.charCodeAt(0) - start.charCodeAt(0))/step))
- .fill(start.charCodeAt(0))
- .map((x, i) => String.fromCharCode(i*step + start.charCodeAt(0)));
- }
- console.log(alphabetRange('c', 'h', 2));
Map en action
String in action
- let s = 'abcdefghijklmnopqrstuvwxyz'
- s= s.substring(s.indexOf('p'), s.indexOf('t')+ 1);
- let A= s.split('');
code
Unique !
Nous allons donner un ensemble de fonction recherchant les valeurs uniques d'un tableau.
- let t = [2,0,2,0,2,3];
- console.time("time");
- unique(t);
- console.timeEnd("time");
Nous pourrons comparer le temps d'exécution des différentes propositions de code.
Nous pourrions également integrer un test élémentaire sur le tableau en argument
- if (!Array.isArray(arr)) {
- throw new TypeError('array-unique expects an array.');
- }
Les écritures suivantes ne sont pas commentées
- function unique(t){
- let cur = t.length;
- while (--cur){
- for (let i=0; i< cur; i++){
- if (t[i] == t[cur]){
- t.splice(cur,1);
- break;
- }
- }
- }
- }
- function unique(arr) {
- let i = -1;
- while (i++ < arr.length) {
- var j = i + 1;
- for (; j < arr.length; ++j) {
- if (arr[i] === arr[j]) {
- arr.splice(j--, 1);
- }
- }
- }
- return arr;
- };
- function unique(array) {
- let passed = [];
- for (let i=0 ; i < array.length; i++) {
- if (array.indexOf( array[i]) === i)
- passed.push(array[i]);
- }
- return passed;
- }
code
- function unique(t){
- let u = t.reduce(function(a,c){
- if (!a.includes(c)){
- a.push(c)
- }
- return a;
- },[])
- return u;
- }
code
- function unique(value,index,tab){
- return tab.indexOf(value) === index;
- }
- console.time("while")
- t = t.filter(unique);
- console.timeEnd("while");
- console.time("while")
- let v = [...new Set(t)];
- console.timeEnd("while");
- const T = [
- { name: 'dupont', id:22 },
- { name: 'dupont', id:20 },
- { name: 'dupond', id:21 },
- { name: 'dupond', id:21 },
- ];
- const unique = [];
- const map = new Map();
- for (const item of T) {
- if(!map.has(item.id)){
- map.set(item.id, true); // set any value to Map
- unique.push({
- id: item.id,
- name: item.name
- });
- }
- }
- console.log(unique)
Projet
Ecrire l'équivalent Js de ce code Python
Exemple de résultat
[6, 6, 0, 4, 8, 7, 6, 4, 7, 5]
[(6, 3), (4, 2), (7, 2), (0, 1), (8, 1), (5, 1)]
[(6, 3)]
code
- import random
- from collections import Counter
- l = [random.randint(0,9) for i in range(10)]
- print (l)
- print(Counter(l).most_common())
- print(Counter(l).most_common(1))
Exemple de résultat
[6, 6, 0, 4, 8, 7, 6, 4, 7, 5]
[(6, 3), (4, 2), (7, 2), (0, 1), (8, 1), (5, 1)]
[(6, 3)]
code
passage des paramètres
Ecrire les valeurs affichées
let start = args;
return [start];
}
console.log(testPassagePara(1))
//-------------------------------------------
const testPassagePara = function(args) {
let start = args;
return [start];
}
console.log(testPassagePara())
//-------------------------------------------
const testPassagePara = function(args=-1) {
let start = args;
return [start];
}
console.log(testPassagePara())
//-------------------------------------------
const testPassagePara = function(...args) {
let [start,end,step] = args;
return [start,end,step];
}
console.log(testPassagePara(1, 5, 2, -1))
//-------------------------------------------
const testPassagePara = function(...args) {
let [start,end,step] = args;
return {start,end,step};
}
console.log(testPassagePara(1, 5, 2, -1))
//-------------------------------------------
const testPassagePara = function(...args) {
let [start,end,step] = args;
return {start,end,step};
}
console.log(testPassagePara(1))
//-------------------------------------------
const testPassagePara = function(...args) {
let [start=0,end=0,step=1] = args;
return [start,end,step];
}
console.log(testPassagePara(1))
nb occurence
const words = [1,2,7];
const alphabetical = words.reduce((a, x) => {
if (!a[x]) a[x] = 0;
a[x]=a[x]+1;
return a},{});
-----------------------
const alphabetical = words.reduce((a, x) => {
if (!a[x]) a[x] = 0;
a[x]=a[x]+1;
return a},[]);
-----------------------
const words = [1,2,2,1,5];
let min = words.reduce((a, x) => Math.min(a,x));
console.log(min);
let max = words.reduce((a, x) => Math.max(a,x));
console.log(max);
let t = Array.from({length:max-min+1},()=>0);
console.log(t);
const alphabetical = words.reduce((a, x) => {
a[x-1]= a[x-1]+1;
return a},t);
--------------------------------
const words = [1,2,2,1,5];
let minMaxDistSumMoyNb = words.reduce((a, x) => {
let [m,M] = [Math.min(a[0],x),Math.max(a[1],x)];
let sum = a[3]+x;
let nb = a[5]+1;
let moy = sum/nb;
return [m,M,M-m+1,sum,moy,nb]
}
,[Number.MAX_VALUE,Number.MIN_VALUE,Number.MIN_VALUE,0,0,0]);
console.log(minMaxDistSumMoyNb);
const alphabetical = words.reduce((a, x) => {
if (!a[x]) a[x] = 0;
a[x]=a[x]+1;
return a},{});
-----------------------
const alphabetical = words.reduce((a, x) => {
if (!a[x]) a[x] = 0;
a[x]=a[x]+1;
return a},[]);
-----------------------
const words = [1,2,2,1,5];
let min = words.reduce((a, x) => Math.min(a,x));
console.log(min);
let max = words.reduce((a, x) => Math.max(a,x));
console.log(max);
let t = Array.from({length:max-min+1},()=>0);
console.log(t);
const alphabetical = words.reduce((a, x) => {
a[x-1]= a[x-1]+1;
return a},t);
--------------------------------
const words = [1,2,2,1,5];
let minMaxDistSumMoyNb = words.reduce((a, x) => {
let [m,M] = [Math.min(a[0],x),Math.max(a[1],x)];
let sum = a[3]+x;
let nb = a[5]+1;
let moy = sum/nb;
return [m,M,M-m+1,sum,moy,nb]
}
,[Number.MAX_VALUE,Number.MIN_VALUE,Number.MIN_VALUE,0,0,0]);
console.log(minMaxDistSumMoyNb);
swap
en C
void swap(int *xp, int *yp){
int temp = *xp;
*xp = *yp;
*yp = temp;
}
int main()
{
int x=1, y=2;
swap(&x, &y);
printf("x = %d, y = %d", x, y);
return 0;
}
➥ code dans Pythontutor
js
let swap = (x,y) => [y,x];
let x=1, y=2;
[x,y] = swap(x,y);
console.log(`x=${x} y=${y}`)
➥ code dans Pythontutor
Aide TD max col
let TAB_MAX_Value = TAB.reduce((max_val,lig_val)=> Math.max(max_val,Math.max(...(lig_val))),Number.MIN_VALUE);
// ecriture => let TAB_Min = TAB.reduce((m,l) => Math.min(m,Math.min(...(l))),TAB[0][0]);
code source
// ecriture => let TAB_Min = TAB.reduce((m,l) => Math.min(m,Math.min(...(l))),TAB[0][0]);
code source
Inscription à :
Articles (Atom)