2021 Twitter Software Apprenticeship Coding Challenge

Jreyes
1 min readJul 18, 2021

--

Last week Flatiron school sent me an opportunity to apply for Twitter Software Apprenticeship and I went for it.

I’m going to leave the solution for the funs with anagrams challenge and I hope this will help someone else.

Given an array of strings, remove each string that is an anagram of an earlier string, then return the remaining array in sorted order.

Example

str = [‘code’, ‘doce’, ‘ecod’, ‘framer’, ‘frame’]

code and doce are anagrams. Remove doce from the array and keep the first occurrence code in the array. code and ecod are anagrams. Remove ecod from the array and keep the first occurrence code in the array. code and framer are not anagrams. Keep both strings in the array. framer and frame are not anagrams due to the extra r in framer. Keep both strings in the array. Order the remaining strings in ascending order ( alphabetically): [‘code’,’frame’,’framer’].

for this example, I'm going to be using javascript:

function funWithAnagrams(text) { let result = []; function checkForAnagram(word) { return result.some(r => { if (r.length !== word.length) { return false } return word.split(‘’).sort().toString() ===       r.split(‘’).sort().toString(); })} for (let word of text) { if (!result.includes(word) && !checkForAnagram(word)) { result.push(word) }} return result.sort(function (a, b) {   a.localeCompare(b);});}console.log(funWithAnagrams([‘code’, ‘aaagmnrs’, ‘anagrams’, ‘doce’, ‘code’, ‘aaagmnrs’, ‘code’]));console.log(funWithAnagrams([‘poke’, ‘pkoe’, ‘okpe’, ‘ekop’]));

the first console.log will return [ ‘code’, ‘aaagmnrs’ ] and the second [ ‘poke’ ] since all the words in the second console.log are anagrams of poke.

I hope this is useful and I will see you next week.

--

--

Jreyes
Jreyes

Written by Jreyes

I'm a software engineer making his way into the tech industry.

No responses yet