Skip to content

Instantly share code, notes, and snippets.

@pakman198
Last active January 14, 2020 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pakman198/e904eea62faa94141dbcf0cb365382c6 to your computer and use it in GitHub Desktop.
Save pakman198/e904eea62faa94141dbcf0cb365382c6 to your computer and use it in GitHub Desktop.
Algorithms / Data Structures
// Array Chunking
/*
Produces an output of an array with nested arrays of n given size
chunk([1,2,3,4,5], 2) ===> [ [1,2], [3,4], [5 ]
*/
function chunk(arr, size) {
const chunked = [];
let index = 0;
while(index < arr.length) {
console.log(index , arr.length)
chunked.push( arr.slice(index, index + size) )
index += size;
}
return chunked;
}
const arr = [1,2,3,4,5,6,7,8,9];
var x = chunk(arr, 2);
console.log({ x })
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// Anagrams
function anagrams(str1, str2) {
const s1 = str1.replace(/[^\w]/g, "").toLowerCase();
const s2 = str2.replace(/[^\w]/g, "").toLowerCase();
const map1 = generateMap(s1);
const map2 = generateMap(s2);
if(Object.keys(map1).length !== Object.keys(map2).length){
return false;
}
for(let char in map1) {
if(map1[char] !== map2[char]) {
return false;
}
}
return true
function generateMap(str) {
return str.split('').reduce((acc, curr) => {
/* console.log({ acc, curr}); */
/* if(acc[curr]) {
acc[curr] += 1;
} else {
acc[curr] = 1;
} */
acc[curr] = acc[curr] + 1 || 1;
return acc ;
}, {});
}
}
const str1 = "rail safety";
const str2 = "fairy tales";
var x = anagrams(str1, str2);
console.log({ x })
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// Anagrams 2
function anagrams(str1, str2) {
return clean(str1) === clean(str2)
function clean(str) {
return str.replace(/\[^\w]/, '')
.toLowerCase()
.split('')
.sort()
.join();
}
}
const str1 = "rail safety";
const str2 = "fairy tales";
const x = anagrams(str1, str2);
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// Capitalize
function capitalize(str) {
const words = [];
for(let w of str.split(' ')) {
words.push(w[0].toUpperCase() + w.slice(1))
}
return words.join(' ');
}
var x = capitalize('hello world');
console.log({ x })
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// Steps
// produces half pyramid
function steps(n) {
for(let i = 0; i < n; ++i) {
let str = '';
for(let j = 0; j< n; ++j) {
str += j <= i ? '#' : ' ';
}
console.log({str})
}
}
steps(7);
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// Pyramid
/*
' * '
' *** '
'*****'
*/
function pyramid(n) {
const base = (n * 2) - 1;
const middle = Math.floor(base/2);
for(let i = 0; i < n; ++i) {
let level = '';
for(let j = 0; j < base; ++j) {
if(middle - i <= j && middle + i >= j){
level += '#'
} else {
level += ' '
}
}
console.log({level})
}
}
pyramid(3);
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// Voewls
function vowels(str) {
const vowels = ['a', 'e', 'i', 'o', 'u'];
const x = str.replace(/[^\w]/g, "")
.toLowerCase()
.split('')
.reduce((acc, curr) => {
if(vowels.includes(curr)){
acc[curr] = acc[curr] + 1 || 1;
}
return acc;
}, {});
console.log({ x })
return x;
}
vowels('Hello World'); // { e: 1, o: 2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment