Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
hkirat committed Dec 3, 2023
0 parents commit 539ab3e
Show file tree
Hide file tree
Showing 18 changed files with 639 additions and 0 deletions.
18 changes: 18 additions & 0 deletions 01-js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## JavaScript Assignments

You are provided empty JavaScript files (or having function signatures) in this directory.
You have to follow the instructions given in each file and write the code in the same file to complete the assignment.

### Assignments
Feel free to start doing these in any order you like.
1. Easy
2. Medium
3. Hard

## Testing
1. Follow the comment above each problem to run test for that problem
3. To tests for all the problems of this week run ```npx jest ./tests/```

#### Development Setup
1. If you have Node.js locally, you should run these on your machine
2. If you don't, you can copy these over to repl.it and run it there. Tests wont be automated there so you will have to make use judgement to ensure if your code is correct
11 changes: 11 additions & 0 deletions 01-js/easy/anagram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Write a function `isAnagram` which takes 2 parameters and returns true/false if those are anagrams or not.
What's Anagram?
- A word, phrase, or name formed by rearranging the letters of another, such as spar, formed from rasp.
*/

function isAnagram(str1, str2) {

}

module.exports = isAnagram;
12 changes: 12 additions & 0 deletions 01-js/easy/expenditure-analysis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Implement a function `calculateTotalSpentByCategory` which takes a list of transactions as parameter
and return a list of objects where each object is unique category-wise and has total price spent as its value.
Transaction - an object like { itemName, category, price, timestamp }.
Output - [{ category1 - total_amount_spent_on_category1 }, { category2 - total_amount_spent_on_category2 }]
*/

function calculateTotalSpentByCategory(transactions) {
return [];
}

module.exports = calculateTotalSpentByCategory;
12 changes: 12 additions & 0 deletions 01-js/easy/findLargestElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
  Write a function `findLargestElement` that takes an array of numbers and returns the largest element.
  Example:
  - Input: [3, 7, 2, 9, 1]
  - Output: 9
*/

function findLargestElement(numbers) {

}

module.exports = findLargestElement;
21 changes: 21 additions & 0 deletions 01-js/hard/calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Implement a class `Calculator` having below methods
- initialise a result variable in the constructor and keep updating it after every arithmetic operation
- add: takes a number and adds it to the result
- subtract: takes a number and subtracts it from the result
- multiply: takes a number and multiply it to the result
- divide: takes a number and divide it to the result
- clear: makes the `result` variable to 0
- getResult: returns the value of `result` variable
- calculate: takes a string expression which can take multi-arithmetic operations and give its result
example input: `10 + 2 * ( 6 - (4 + 1) / 2) + 7`
Points to Note:
1. the input can have multiple continuous spaces, you're supposed to avoid them and parse the expression correctly
2. the input can have invalid non-numerical characters like `5 + abc`, you're supposed to throw error for such inputs
Once you've implemented the logic, test your code by running
*/

class Calculator {}

module.exports = Calculator;
17 changes: 17 additions & 0 deletions 01-js/hard/todo-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Implement a class `Todo` having below methods
- add(todo): adds todo to list of todos
- remove(indexOfTodo): remove todo from list of todos
- update(index, updatedTodo): update todo at given index
- getAll: returns all todos
- get(indexOfTodo): returns todo at given index
- clear: deletes all todos
Once you've implemented the logic, test your code by running
*/

class Todo {

}

module.exports = Todo;
12 changes: 12 additions & 0 deletions 01-js/medium/countVowels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Implement a function `countVowels` that takes a string as an argument and returns the number of vowels in the string.
Note: Consider both uppercase and lowercase vowels ('a', 'e', 'i', 'o', 'u').
Once you've implemented the logic, test your code by running
*/

function countVowels(str) {
// Your code here
}

module.exports = countVowels;
10 changes: 10 additions & 0 deletions 01-js/medium/palindrome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Implement a function `isPalindrome` which takes a string as argument and returns true/false as its result.
Note: the input string is case-insensitive which means 'Nan' is a palindrom as 'N' and 'n' are considered case-insensitive.
*/

function isPalindrome(str) {
return true;
}

module.exports = isPalindrome;
13 changes: 13 additions & 0 deletions 01-js/medium/times.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Write a function that calculates the time (in seconds) it takes for the JS code to calculate sum from 1 to n, given n as the input.
Try running it for
1. Sum from 1-100
2. Sum from 1-100000
3. Sum from 1-1000000000
Hint - use Date class exposed in JS
There is no automated test for this one, this is more for you to understand time goes up as computation goes up
*/

function calculateTime(n) {
return 0.01;
}
13 changes: 13 additions & 0 deletions 01-js/package-lock.json

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

12 changes: 12 additions & 0 deletions 01-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "01",
"version": "1.0.0",
"description": "You are provided empty JavaScript files (or having function signatures) in this directory. You have to follow the instructions given in each file and write the code in the same file to complete the assignment.",
"main": "anagram.js",
"directories": {
"test": "tests"
},
"keywords": [],
"author": "",
"license": "ISC"
}
37 changes: 37 additions & 0 deletions 01-js/tests/anagram.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const isAnagram = require('../easy/anagram');

describe('isAnagram', () => {
test('returns true for anagrams', () => {
expect(isAnagram('listen', 'silent')).toBe(true);
expect(isAnagram('rail safety', 'fairy tales')).toBe(
true
);
expect(isAnagram('openai', 'aiopen')).toBe(true);
expect(isAnagram('', '')).toBe(true);
});

test('returns false for non-anagrams', () => {
expect(isAnagram('hello', 'world')).toBe(false);
expect(isAnagram('openai', 'open')).toBe(false);
expect(isAnagram('hello', 'lhel')).toBe(false);
expect(isAnagram('working', 'non')).toBe(false);
});

test('returns true for anagrams with different casing', () => {
expect(isAnagram('Debit Card', 'Bad Credit')).toBe(
true
);
expect(
isAnagram('School MASTER', 'The ClassROOM')
).toBe(true);
});

test('returns true for anagrams with special characters', () => {
expect(isAnagram('abc!', '!bac')).toBe(true);
});

test('returns false for non-anagrams with special characters', () => {
expect(isAnagram('hello', 'hello!')).toBe(false);
expect(isAnagram('openai!', 'open')).toBe(false);
});
});
124 changes: 124 additions & 0 deletions 01-js/tests/calculator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
const Calculator = require('../hard/calculator');

describe('Calculator', () => {
let calc;

beforeEach(() => {
calc = new Calculator();
});

afterEach(() => {
calc.clear();
});

test('addition', () => {
calc.add(5);
expect(calc.getResult()).toBe(5);

calc.add(3);
expect(calc.getResult()).toBe(8);
});

test('subtraction', () => {
calc.subtract(5);
expect(calc.getResult()).toBe(-5);

calc.subtract(3);
expect(calc.getResult()).toBe(-8);
});

test('multiplication', () => {
calc.add(4);
calc.multiply(3);
expect(calc.getResult()).toBe(12);

calc.multiply(0);
expect(calc.getResult()).toBe(0);
});

test('division', () => {
calc.add(12);

calc.divide(4);
expect(calc.getResult()).toBe(3);

expect(() => calc.divide(0)).toThrow(Error);
expect(calc.getResult()).toBe(3);
});

test('clear', () => {
calc.add(5);
calc.clear();
expect(calc.getResult()).toBe(0);
});

test('calculate addition and multiplication', () => {
calc.calculate('2 + 3 * 4');
expect(calc.getResult()).toBe(14);
});

test('calculate division in expression', () => {
calc.calculate('( 15 + 3) / 6 ');
expect(calc.getResult()).toBe(3);
});

test('calculate subtraction in expression', () => {
calc.calculate('10 - (4 + 2)');
expect(calc.getResult()).toBe(4);
});

test('calculate complex expression', () => {
calc.calculate('(2 + 3) * (6 - (4 + 1) / 2) + 7');
expect(calc.getResult()).toBe(24.5);
});
test('calculate complex expression with spaces', () => {
calc.calculate(
'10 + 2 * ( 6 - (4 + 1) / 2) + 7'
);
expect(calc.getResult()).toBe(24);
});

test('calculate expression with decimals', () => {
calc.calculate('(2.5 + 1.5) * 3');
expect(calc.getResult()).toBe(12);
});

test('calculate expression with invalid characters', () => {
expect(() => calc.calculate('5 + abc')).toThrow(Error);
expect(() =>
calc.calculate('10 * (2 + 3) + xyz')
).toThrow(Error);
});

test('calculate division by zero', () => {
expect(() => calc.calculate('10 / 0')).toThrow(Error);
});

test('multiplication with negative numbers', () => {
calc.add(-5);
calc.multiply(-3);
expect(calc.getResult()).toBe(15);

calc.multiply(0);
expect(calc.getResult()).toBe(0);
});

test('division with decimal numbers', () => {
calc.add(10);
calc.divide(3);
expect(calc.getResult()).toBeCloseTo(3.333333, 2);

calc.divide(2);
expect(calc.getResult()).toBeCloseTo(1.666666, 2);
});

test('expression with invalid parentheses', () => {
expect(() => calc.calculate('10 + (2 + 3')).toThrow(
Error
);
expect(() => calc.calculate('10 + 2) + 3')).toThrow(
Error
);
expect(() => calc.calculate(')10 + 2(')).toThrow(Error);
});
});
37 changes: 37 additions & 0 deletions 01-js/tests/countVowels.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const countVowels = require('../medium/countVowels');

describe('countVowels', () => {
test('returns the correct count for strings with vowels', () => {
expect(countVowels('hello')).toBe(2);
expect(countVowels('programming')).toBe(4);
expect(countVowels('OpenAI')).toBe(3);
});

test('returns 0 for strings without vowels', () => {
expect(countVowels('rhythm')).toBe(0);
expect(countVowels('fly')).toBe(0);
expect(countVowels('chatbot')).toBe(0);
});

test('returns 0 for an empty string', () => {
expect(countVowels('')).toBe(0);
});

test('handles case-insensitivity correctly', () => {
expect(countVowels('EaSiEr')).toBe(3);
expect(countVowels('QUIET')).toBe(3);
expect(countVowels('aEIOU')).toBe(5);
});

test('returns the correct count for strings with spaces', () => {
expect(countVowels('the quick brown fox')).toBe(5);
expect(countVowels('a e i o u')).toBe(5);
expect(countVowels('testing spaces')).toBe(4);
});

test('returns the correct count for strings with punctuation marks', () => {
expect(countVowels('Hello, world!')).toBe(3);
expect(countVowels('Coding is fun!!!')).toBe(4);
expect(countVowels('Keep smiling, boo.')).toBe(6);
});
});
Loading

0 comments on commit 539ab3e

Please sign in to comment.