From cb43e9166a67cc4665f0526efb885bd5f27b1834 Mon Sep 17 00:00:00 2001 From: Anunihu Chukwuebuka James Date: Tue, 31 Jan 2023 15:45:28 +0100 Subject: [PATCH 1/2] Task3 Solutions --- src/isolate-duplicates/index.js | 36 ++++++++++++++++++++++++++++++++- src/morse/index.js | 12 +++++++++-- src/remove-dulplicates/index.js | 16 ++++++++++++++- 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/isolate-duplicates/index.js b/src/isolate-duplicates/index.js index 79ae426..f5096c2 100644 --- a/src/isolate-duplicates/index.js +++ b/src/isolate-duplicates/index.js @@ -1,3 +1,37 @@ -function isolateDuplicates(text) {} +function isolateDuplicates(text) { + if (typeof (text) !== "string") { + return 'Please enter a valid string' + } + + let alphabets = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' + let finalStr = '' + let count = 0 + let countObj = _.countBy(text) + let keysArr = Object.keys(countObj) + let valuesArr = Object.values(countObj) + for (let j = 0; j < text.length; j++) { + let m = j + if (text[m] === text[j] && text.lastIndexOf(text[j]) - text.indexOf(text[j]) > 1) { + finalStr += text[j] + if (j - text.indexOf(text[j]) == 1) { + finalStr += '[' + } + if (j === text.lastIndexOf(text[j])) { + finalStr += ']' + } + } else { + finalStr += text[j] + } + } + + for (let index = 0; index < keysArr.length; index++) { + if (valuesArr[index] > 2) { + count++ + } + } + return [finalStr, count] +} +console.log(isolateDuplicates("aaaabbcdefffffffg")) +console.log(isolateDuplicates("hellllloooooodeeeecadevvsss")) module.exports = isolateDuplicates; diff --git a/src/morse/index.js b/src/morse/index.js index cf65063..1d38341 100644 --- a/src/morse/index.js +++ b/src/morse/index.js @@ -58,6 +58,14 @@ const MORSE_CODE = { Object.freeze(MORSE_CODE); -function morse(text) {} - +function morse(text) { + text = text.split(' '); + console.log(text) + let translatedCode; + translatedCode = text.map(a => a.split(' ').map(b => MORSE_CODE[b]).join('')).join(' ') + return translatedCode; +} +console.log(morse('...-..- ..... .-.-.- ----- --... .--. . .-. -... --- - - .-.. .')) module.exports = morse; + + diff --git a/src/remove-dulplicates/index.js b/src/remove-dulplicates/index.js index 75c6c9b..d784189 100644 --- a/src/remove-dulplicates/index.js +++ b/src/remove-dulplicates/index.js @@ -1,3 +1,17 @@ -function removeDuplicates(obj) {} +function removeDuplicates(obj) { + let arr = Object.entries(obj) + let newArr + console.log(arr) + for (let index = 0; index < arr.length; index++) { + newArr = [...new Set(arr[index][1].sort())] + console.log(newArr) + } +} +console.log(removeDuplicates({ + "432": ["A", "A", "B", "D"], + "53": ["L", "G", "B", "C"], + "236": ["L", "A", "X", "G", "H", "X"], + "11": ["P", "R", "S", "D"] +})) module.exports = removeDuplicates; From 5b384c17d63d5cb3ab3f1c956f13bc3a3a13a201 Mon Sep 17 00:00:00 2001 From: Anunihu Chukwuebuka James Date: Wed, 1 Feb 2023 22:21:06 +0100 Subject: [PATCH 2/2] Final solution --- src/isolate-duplicates/index.js | 33 +++++++++++---------------------- src/morse/index.js | 16 ++++++++-------- src/remove-dulplicates/index.js | 31 +++++++++++++++++++++---------- tsconfig.json | 18 +++++++++--------- 4 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src/isolate-duplicates/index.js b/src/isolate-duplicates/index.js index f5096c2..ce9a997 100644 --- a/src/isolate-duplicates/index.js +++ b/src/isolate-duplicates/index.js @@ -1,37 +1,26 @@ function isolateDuplicates(text) { if (typeof (text) !== "string") { - return 'Please enter a valid string' + throw new Error('Please enter a valid string') } - let alphabets = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' let finalStr = '' let count = 0 - let countObj = _.countBy(text) - let keysArr = Object.keys(countObj) - let valuesArr = Object.values(countObj) + let newText = text.toLowerCase() for (let j = 0; j < text.length; j++) { - let m = j - if (text[m] === text[j] && text.lastIndexOf(text[j]) - text.indexOf(text[j]) > 1) { - finalStr += text[j] - if (j - text.indexOf(text[j]) == 1) { - finalStr += '[' - } - if (j === text.lastIndexOf(text[j])) { - finalStr += ']' - } - } else { - finalStr += text[j] + finalStr += text[j] + + if (newText[j] === newText[j - 1] && newText[j] === newText[j + 1] && newText[j] !== newText[j - 2]) { + finalStr += '[' + count += 0.5 } - } - for (let index = 0; index < keysArr.length; index++) { - if (valuesArr[index] > 2) { - count++ + if (newText[j] === newText[j - 1] && newText[j] !== newText[j + 1] && newText[j] === newText[j - 2]) { + finalStr += ']' + count += 0.5 } } return [finalStr, count] } -console.log(isolateDuplicates("aaaabbcdefffffffg")) -console.log(isolateDuplicates("hellllloooooodeeeecadevvsss")) + module.exports = isolateDuplicates; diff --git a/src/morse/index.js b/src/morse/index.js index 1d38341..d614bfa 100644 --- a/src/morse/index.js +++ b/src/morse/index.js @@ -59,13 +59,13 @@ const MORSE_CODE = { Object.freeze(MORSE_CODE); function morse(text) { - text = text.split(' '); - console.log(text) - let translatedCode; - translatedCode = text.map(a => a.split(' ').map(b => MORSE_CODE[b]).join('')).join(' ') - return translatedCode; + if (typeof text != 'string') { + throw new Error('Please provide a morse string') + } else { + text = text.trim().split(' '); + let translatedCode; + translatedCode = text.map(a => a.split(' ').map(b => MORSE_CODE[b]).join('')).join(' ') + return translatedCode; + } } -console.log(morse('...-..- ..... .-.-.- ----- --... .--. . .-. -... --- - - .-.. .')) module.exports = morse; - - diff --git a/src/remove-dulplicates/index.js b/src/remove-dulplicates/index.js index d784189..cba99b3 100644 --- a/src/remove-dulplicates/index.js +++ b/src/remove-dulplicates/index.js @@ -1,17 +1,28 @@ function removeDuplicates(obj) { let arr = Object.entries(obj) let newArr - console.log(arr) - for (let index = 0; index < arr.length; index++) { - newArr = [...new Set(arr[index][1].sort())] - console.log(newArr) + let keysArr = [] + let valuesArr = [] + let c = [] + let diff = (a, b) => [...new Set([...a].filter((x) => !b.includes(x)))]; + + for (let index = 0; index < arr.length; index++) { + keysArr.push(arr[index][0]) + newArr = [...new Set(arr[index][1])] + valuesArr.push(newArr) } + let finalArr = valuesArr.reverse().map((value) => { + const subCollector = c; + c = [...new Set([...c, ...value])]; + return [...new Set(diff(value, subCollector))] + }) + finalArr = finalArr.reverse() + const newObj = {} + keysArr.forEach((element, index) => { + let newElement = element + newObj[newElement] = finalArr[index] + }) + return newObj } -console.log(removeDuplicates({ - "432": ["A", "A", "B", "D"], - "53": ["L", "G", "B", "C"], - "236": ["L", "A", "X", "G", "H", "X"], - "11": ["P", "R", "S", "D"] -})) module.exports = removeDuplicates; diff --git a/tsconfig.json b/tsconfig.json index ee19d46..b2dfb5b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,7 +11,7 @@ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ /* Language and Environment */ - "target": "es2020", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "target": "es2020" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "jsx": "preserve", /* Specify what JSX code is generated. */ // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ @@ -25,7 +25,7 @@ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ /* Modules */ - "module": "commonjs", /* Specify what module code is generated. */ + "module": "commonjs" /* Specify what module code is generated. */, // "rootDir": "./", /* Specify the root folder within your source files. */ // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ @@ -39,7 +39,7 @@ // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ /* JavaScript Support */ - "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ + "allowJs": true /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */, // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ @@ -47,11 +47,11 @@ // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ // "declarationMap": true, /* Create sourcemaps for d.ts files. */ // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ - "sourceMap": true, /* Create source map files for emitted JavaScript files. */ + "sourceMap": true /* Create source map files for emitted JavaScript files. */, // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ // "outDir": "./", /* Specify an output folder for all emitted files. */ // "removeComments": true, /* Disable emitting comments. */ - "noEmit": true, /* Disable emitting files from a compilation. */ + "noEmit": true /* Disable emitting files from a compilation. */, // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ @@ -71,12 +71,12 @@ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ - "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ + "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */, // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ - "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ + "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, /* Type Checking */ - "strict": true, /* Enable all strict type-checking options. */ + "strict": true /* Enable all strict type-checking options. */, // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ @@ -98,6 +98,6 @@ /* Completeness */ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ - "skipLibCheck": true /* Skip type checking all .d.ts files. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ } }