Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Ratiu committed Jan 2, 2020
1 parent d2593d8 commit 5449f9c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 11 deletions.
16 changes: 12 additions & 4 deletions exercises
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ take the following input data
"name": "Gill",
"type": "fish"
}
]1. and group them by type
]

1. and group them by type
ex:
[
{
Expand All @@ -35,9 +37,15 @@ ex:
type: dog,
animals: ['Manuel', 'Sparky', 'John']
}, etc...
]2. Given 2-3 animals that are in the list create an interesction of the list.
ex: given manuel and gill, intersect with the list of animals. Some animals may NOT appear in both lists, handle that case!3. Given an array, generate a CSV representation of that array with a set of headers.
]

2. Given 2-3 animals that are in the list create an interesction of the list.
ex: given manuel and gill, intersect with the list of animals. Some animals may NOT appear in both lists, handle that case!

3. Given an array, generate a CSV representation of that array with a set of headers.
ex:
NAME, TYPE
Gill, fish
Manuel, dog4. Create a function that can only run once and throws if it gets called again.5. Create a function that is given a set of functions and a set of data which are inputs for those functions and returns the one that finished the fastest out of them, displaying ms time
Manuel, dog

4. Create a function that can only run once and throws if it gets called again.5. Create a function that is given a set of functions and a set of data which are inputs for those functions and returns the one that finished the fastest out of them, displaying ms time
70 changes: 64 additions & 6 deletions group.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
interface DataEntry {
name: 'John' | 'Sparky' | 'Nova' | 'Manuel' | 'Caesar' | 'Gill';
type: 'dog' | 'cat' | 'fish';
name: 'John' | 'Sparky' | 'Nova' | 'Manuel' | 'Caesar' | 'Gill' | 'Billy' | 'Silly';
type: 'dog' | 'cat' | 'fish' | 'bug' | 'bird';
};

const data:Array<DataEntry> = [
const data1: Array<DataEntry> = [
{
"name": "John",
"type": "dog"
Expand All @@ -30,11 +30,30 @@ const data:Array<DataEntry> = [
}
];

const data2: Array<DataEntry> = [
{
"name": "John",
"type": "dog"
},
{
"name": "Sparky",
"type": "dog"
},
{
"name": "Silly",
"type": "bug"
},
{
"name": "Billy",
"type": "bird"
}
]

interface OutputEntry {
type: string;
animals: Array<string>;
}


const groupByType = (ar: Array<DataEntry>) => {
return ar.reduce((acc:Array<OutputEntry>, element:DataEntry): Array<OutputEntry> => {
Expand All @@ -51,5 +70,44 @@ const groupByType = (ar: Array<DataEntry>) => {
}, [] as Array<OutputEntry>);
}

const elements = groupByType(data);
console.log(elements);
// 1 - group by type
const elements = groupByType(data1);
console.log(elements);

// 2 - data intersection
const intersection = (ar1: Array<DataEntry>, ar2: Array<DataEntry>): Array<DataEntry> => {
return ar1.reduce((acc: Array<DataEntry>, el1: DataEntry): Array<DataEntry> => {
const result: DataEntry | void = ar2.find((el2: DataEntry): any => el2.name === el1.name && el2.type === el1.type);
if (result) {
acc.push(result);
}

return acc;
}, [] as Array<DataEntry>);
};

console.log( intersection(data1, data2));

// 3 - generate a csv
const CSVFormater = (content: Array<Array<string>>): string => {
const result = content.map((line: Array<string>): string => line.join(','));

return result.join('\n')
};

const CSVGeneartor = (ar: Array<DataEntry>): string => {
if (ar.length === 0) {
return '';
}

const header = Object.keys(ar[0]);
const content = [];
content.push(header);
ar.forEach((el: DataEntry): void => {
content.push(Object.values(el));
});

return CSVFormater(content);
}

console.log( '---', CSVGeneartor(data1));
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"target": "ES2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
Expand Down

0 comments on commit 5449f9c

Please sign in to comment.