Last active
July 7, 2017 23:22
-
-
Save calam1/b00ae96319e3424ddbc7277d13ad371f to your computer and use it in GitHub Desktop.
CodeWars String Mix javascript solution
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
var s1 = "my&friend&Paul has heavy hats! &" | |
var s2 = "my friend John has many many friends &" | |
// mix(s1, s2) --> "2:nnnnn/1:aaaa/1:hhh/2:mmm/2:yyy/2:dd/2:ff/2:ii/2:rr/=:ee/=:ss" | |
// var s1 = "Are they here"; | |
// var s2 = "yes, they are here"; | |
// "2:eeeee/2:yy/=:hh/=:rr") | |
// Test.assertEquals(mix("looping is fun but dangerous", "less dangerous than coding"), "1:ooo/1:uuu/2:sss/=:nnn/1:ii/2:aa/2:dd/2:ee/=:gg") | |
// Test.assertEquals(mix(" In many languages", " there's a pair of functions"), "1:aaa/1:nnn/1:gg/2:ee/2:ff/2:ii/2:oo/2:rr/2:ss/2:tt") | |
// Test.assertEquals(mix("Lords of the Fallen", "gamekult"), "1:ee/1:ll/1:oo") | |
// Test.assertEquals(mix("codewars", "codewars"), "") | |
// Test.assertEquals(mix("A generation must confront the looming ", "codewarrs"), "1:nnnnn/1:ooooo/1:tttt/1:eee/1:gg/1:ii/1:mm/=:rr") | |
=================================================================================================================================== | |
// SECOND ATTEMPT W/Unit tests | |
function accumulateValues(str) { | |
var array = []; | |
for (var i = 0; i < str.length; i++) { | |
var char = str[i]; | |
if (char === char.toLowerCase() && /[a-z]/.test(char)) { | |
array.push(char); | |
} | |
} | |
return array.sort(); | |
} | |
function createObject(owner, str) { | |
var collect = accumulateValues(str); | |
var distinctValues = collect.filter((val, i) => { | |
return collect.indexOf(val) === i; | |
}); | |
var array = []; | |
for (var i = 0; i < distinctValues.length; i++) { | |
var count = collect.reduce((accum, val) => { | |
return accum + (val === distinctValues[i]); | |
}, 0); | |
array.push({owner: owner, key: distinctValues[i], count: count}); | |
} | |
return sortArray(array); | |
} | |
function sortArray(array) { | |
return array.slice().sort((a, b) => { | |
return a.key > b.key ? 1 : -1; | |
}); | |
} | |
function mergeArrayOfObjects(arrayOne, arrayTwo) { | |
var merged = arrayOne.concat(arrayTwo); | |
var array = []; | |
merged.forEach( x => { | |
array.push(x.key); | |
}) | |
var distinct = array.filter((x, i) => { | |
return array.indexOf(x) === i; | |
}); | |
var mergedArray = []; | |
for (var i = 0; i < distinct.length; i++) { | |
var distinctValue = distinct[i]; | |
var a = arrayOne.find((x => x.key === distinctValue)); | |
var b = arrayTwo.find((x => x.key === distinctValue)); | |
if (a && b) { | |
if (a.count > b.count) { | |
mergedArray.push(a); | |
} else if (a.count === b.count) { | |
mergedArray.push({owner: '=', key: a.key, count: a.count}); | |
} else { | |
mergedArray.push(b); | |
} | |
} else if (a && !b) { | |
mergedArray.push(a); | |
} else if (!a && b) { | |
mergedArray.push(b); | |
} | |
} | |
var sortedMergedArray = mergedArray.slice().sort((a, b) => { | |
return a.key > b.key ? 1 : -1; | |
}) | |
return sortedMergedArray; | |
} | |
function formatResults(values) { | |
var sortByCount = values.slice().sort((a, b) => { | |
if (a.count === b.count) { | |
if (a.owner === b.owner) { | |
return a.key > b.key ? 1 : -1; | |
} | |
return a.owner > b.owner ? 1 : -1; | |
} | |
return a.count > b.count ? -1 : 1; | |
}); | |
var filtered = sortByCount.filter((x) => x.count > 1); | |
console.log('sortedByCount', filtered); | |
var formattedString = ''; | |
filtered.forEach(x => { | |
formattedString += x.owner; | |
formattedString += ':'; | |
for (var i = 0; i < x.count; i++) { | |
formattedString += x.key; | |
} | |
formattedString += '/' | |
}) | |
return formattedString.substring(0, formattedString.length - 1); | |
} | |
function highestLetterCounter(str1, str2) { | |
var arrayOne = createObject(1, str1); | |
var arrayTwo = createObject(2, str2); | |
var mergedSortedArray = mergeArrayOfObjects(arrayOne, arrayTwo); | |
return formatResults(mergedSortedArray); | |
} | |
var s1 = "my&friend&Paul has heavy hats! &" | |
var s2 = "my friend John has many many friends &" | |
// mix(s1, s2) --> "2:nnnnn/1:aaaa/1:hhh/2:mmm/2:yyy/2:dd/2:ff/2:ii/2:rr/=:ee/=:ss" | |
console.log(highestLetterCounter(s1, s2)); | |
var expect = require('chai').expect; | |
describe('formatted output', () => { | |
var expected = '=:aa/2:cc/2:dd/1:b'; | |
const arr = [{owner:'=', key: 'a', count: 2}, {owner: 1, key: 'b', count: 1}, {owner: 2, key: 'c', count: 2}, {owner: 2, key: 'd', count: 2}]; | |
it('shoule look like this: ', () => { | |
expect(expected).to.equal(formatResults(arr)); | |
}) | |
}) | |
describe('mergeArrayOfObjects', () => { | |
var arrayA = []; | |
var arrayB = []; | |
const expected = [{owner:'=', key: 'a', count: 2}, {owner: 1, key: 'b', count: 1}, {owner: 2, key: 'c', count: 2}, {owner: 2, key: 'd', count: 2}]; | |
before(() => { | |
arrayA.push({owner: 1, key: 'a', count: 2}); | |
arrayA.push({owner: 1, key: 'b', count: 1}); | |
arrayA.push({owner: 1, key: 'd', count: 1}); | |
arrayB.push({owner: 2, key: 'a', count: 2}); | |
arrayB.push({owner: 2, key: 'c', count: 2}); | |
arrayB.push({owner: 2, key: 'd', count: 2}); | |
}) | |
it("should contain objects: [{owner:':', key: 'a', count: 2}, {owner: 1, key: 'b', count: 1}, {owner: 2, key: 'c', count: 2}, {owner: 2, key: 'd', count: 2}]", () => { | |
expect(expected).to.eql(mergeArrayOfObjects(arrayA, arrayB)); | |
}) | |
}) | |
describe('sortArray', () => { | |
var array = []; | |
var orig = []; | |
before(() => { | |
array.push({owner: 1, key: 'a', count: 2}); | |
array.push({owner: 1, key: 'b', count: 1}); | |
orig.push({owner: 1, key: 'b', count: 1}); | |
orig.push({owner: 1, key: 'a', count: 2}); | |
}) | |
it('should sort array in order of key', () => { | |
expect(sortArray(orig)).to.eql(array); | |
}) | |
}) | |
describe('createObject', () => { | |
var array = []; | |
before(() => { | |
array.push({owner: 1, key: 'a', count: 3}); | |
array.push({owner: 1, key: 'b', count: 2}); | |
}) | |
it('should have key of a with count of 3 and key of b with count of 2', () => { | |
expect(createObject(1, ['a', 'a', 'a', 'b', 'b'])).to.eql(array); | |
}) | |
}) | |
describe('save off lowercase letters of a string', () => { | |
var array = []; | |
before(() => { | |
array.push('a'); | |
array.push('a'); | |
array.push('b'); | |
}) | |
it('should match [a, a, b]', () => { | |
expect(accumulateValues('aBAba?DW')).to.eql(array); | |
}) | |
}); | |
=================================================================================================================================== | |
// FIRST ATTEMPT | |
function fix(value, s) { | |
value[s] += 1; | |
return value; | |
} | |
function aggregate(exists, char, s) { | |
if (char === char.toLowerCase() && /[a-z]/.test(char.toLowerCase())) { | |
if (!exists) { | |
var charVal = { | |
char: char, | |
s1: 0, | |
s2: 0 | |
}; | |
charVal[s] = 1; | |
return charVal; | |
} else { | |
var addUp = fix(exists, s); | |
return addUp; | |
} | |
} | |
} | |
function test(s1, s2) { | |
var map = new Map(); | |
for (var i = 0; i < s1.length; i++) { | |
var char = s1[i]; | |
var exists = map.get(char); | |
// var value = aggregate(exists, char, 's1'); | |
var value = aggregate(exists, char, 's1'); | |
if (value) map.set(char, value); | |
} | |
for (var j = 0; j < s2.length; j++) { | |
var char = s2[j]; | |
var exists = map.get(char); | |
var value = aggregate(exists, char, 's2'); | |
if (value) map.set(char, value); | |
} | |
var array = Array.from(map.values()); | |
var filtered = []; | |
array.forEach(x => { | |
// console.log(x); | |
var data; | |
var key; | |
if (x.s1 === x.s2) { | |
data = x.s1; | |
key = '=:' | |
} else if (x.s1 > x.s2) { | |
data = x.s1; | |
key = '1:'; | |
} else { | |
data = x.s2; | |
key = '2:'; | |
} | |
filtered.push({char: x.char, count: data, key: key}); | |
}); | |
var sorted = filtered.slice().sort((a, b) => { | |
if (a.count === b.count) { | |
if (a.key === b.key) { | |
return a.char > b.char ? 1 : -1; | |
} | |
return a.key > b.key ? 1 : -1; | |
} | |
return a.count > b.count ? -1 : 1; | |
}); | |
sorted = sorted.filter(x => x.count > 1); | |
var response = ''; | |
sorted.forEach(x => { | |
response+=x.key | |
for (var i = 0; i < x.count; i++) { | |
response += x.char; | |
} | |
response+='/'; | |
}); | |
return response.slice(0, -1); | |
} | |
console.log(test(s1, s2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment