Last active
September 12, 2024 12:13
-
-
Save Ovicakov/c5a51a04688455907fdfef0ca638c664 to your computer and use it in GitHub Desktop.
algorithms
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
// Bubble sort | |
const bubbleSort = (numbersArray) => { | |
const arrayLenght = numbersArray.length; | |
for (let i = 0; i < arrayLenght; i++) { | |
for (let j = 0; j < arrayLenght; j++) { | |
if (numbersArray[j] > numbersArray[j +1]) { | |
const currentNumber = numbersArray[j] | |
numbersArray[j] = numbersArray[j +1] | |
numbersArray[j +1] = currentNumber | |
} | |
} | |
} | |
return numbersArray | |
} | |
const numbers = [13, 5, 54, 2, 167, 45, 1, 56] | |
console.log(bubbleSort(numbers)) | |
/* | |
Exercice de récursivité, avec refactorisation en programmation fonctionnelle, | |
issu de cet article : https://www.jesuisundev.com/comprendre-la-recursivite-en-7-min/#comment-11083 | |
*/ | |
const dump = [ | |
{ | |
'type': 'folder', | |
'name': 'cats', | |
'children': [ | |
{ | |
'type': 'image', | |
'name': 'Buffy' | |
}, | |
{ | |
'type': 'image', | |
'name': 'Gizmo' | |
}, | |
{ | |
'type': 'folder', | |
'name': 'small-cat', | |
'children': [ | |
{ | |
'type': 'image', | |
'name': 'Fluffy' | |
}, | |
{ | |
'type': 'image', | |
'name': 'Harry' | |
}, | |
{ | |
'type': 'folder', | |
'name': 'black-cat', | |
'children': [ | |
{ | |
'type': 'image', | |
'name': 'Daisy' | |
}, | |
{ | |
'type': 'image', | |
'name': 'Toby' | |
} | |
] | |
}, | |
{ | |
'type': 'folder', | |
'name': 'white-cat', | |
'children': [ | |
{ | |
'type': 'image', | |
'name': 'Minnie' | |
}, | |
{ | |
'type': 'image', | |
'name': 'Lucy' | |
} | |
] | |
} | |
] | |
}, | |
{ | |
'type': 'folder', | |
'name': 'future-cat', | |
'children': [] | |
} | |
] | |
} | |
] | |
function catNames(folder) { | |
return folder.reduce((acc, curr) => { | |
return acc.concat(curr.type === 'folder' ? catNames(curr.children) : curr.name) | |
}, []) | |
} | |
console.log(catNames(dump)) | |
// assocPath from ramda reproduction | |
const assocPath = (array, myValue) => { | |
return array.reduceRight((objet, currentValue) => { | |
const lastValue = array[array.length - 1] | |
return currentValue === lastValue ? {[currentValue]: myValue} : {[currentValue]: objet} | |
}, {}) | |
} | |
const arr = ['a', 'b', 'c'] | |
console.log(assocPath(arr, 'Zidane')) | |
// Coding game | |
/* | |
takes an integer input n. If the number consists of one digit, | |
print the remainder of this number when divided by 2 (i.e., number mod 2). | |
example : if the number = 3, the number consists of one digit. You will print its | |
remainder when divided by 2. So, since (3 mod 2) = 1, you will print 1. | |
Otherwise, if the number has more than one digit, then repeatedly add the digits | |
of this number, until the result consists of only one digit. | |
example : if number = 98651, the sum of its digits is: 9 + 8 + 6 + 5 + 1 = 29. Now add the digits of 29: 2 + 9 = 11. | |
Now add the digits of 11: 1 + 1 = 2. The result consists of only one digit, so you will print 2. | |
*/ | |
function addDigits(n) { | |
const result = String(n).split``.reduce((acc, curr) => acc+= Number(curr), 0) | |
return String(result).length > 1 ? addDigits(result) : result | |
} | |
console.log(addDigits(255678)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment