Skip to content

Commit c4f5ab6

Browse files
committed
complete folder of assignments
1 parent 9bf973b commit c4f5ab6

File tree

209 files changed

+80676
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

209 files changed

+80676
-1
lines changed

.DS_Store

2 KB
Binary file not shown.

assignments-master/.DS_Store

8 KB
Binary file not shown.

assignments-master/01-js/.DS_Store

6 KB
Binary file not shown.

assignments-master/01-js/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## JavaScript Assignments
2+
3+
You are provided empty JavaScript files (or having function signatures) in this directory.
4+
You have to follow the instructions given in each file and write the code in the same file to complete the assignment.
5+
6+
### Assignments
7+
Feel free to start doing these in any order you like.
8+
1. Easy
9+
2. Medium
10+
3. Hard
11+
12+
## Testing
13+
1. Follow the comment above each problem to run test for that problem
14+
3. To tests for all the problems of this week run ```npx jest ./tests/```
15+
16+
#### Development Setup
17+
1. If you have Node.js locally, you should run these on your machine
18+
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Write a function `isAnagram` which takes 2 parameters and returns true/false if those are anagrams or not.
3+
What's Anagram?
4+
- A word, phrase, or name formed by rearranging the letters of another, such as spar, formed from rasp.
5+
*/
6+
7+
function isAnagram(str1, str2) {
8+
9+
}
10+
11+
module.exports = isAnagram;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Implement a function `calculateTotalSpentByCategory` which takes a list of transactions as parameter
3+
and return a list of objects where each object is unique category-wise and has total price spent as its value.
4+
transactions is an array where each
5+
Transaction - an object like
6+
{
7+
id: 1,
8+
timestamp: 1656076800000,
9+
price: 10,
10+
category: 'Food',
11+
itemName: 'Pizza',
12+
}
13+
Output - [{ category: 'Food', totalSpent: 10 }] // Can have multiple categories, only one example is mentioned here
14+
*/
15+
16+
function calculateTotalSpentByCategory(transactions) {
17+
return [];
18+
}
19+
20+
module.exports = calculateTotalSpentByCategory;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
  Write a function `findLargestElement` that takes an array of numbers and returns the largest element.
3+
  Example:
4+
  - Input: [3, 7, 2, 9, 1]
5+
  - Output: 9
6+
*/
7+
8+
function findLargestElement(numbers) {
9+
let max = numbers[0];
10+
for(let i=0;i<numbers.length;i++){
11+
numbers[i]>max ? max = numbers[i] : "";
12+
}
13+
return max;
14+
}
15+
console.log(findLargestElement([3, 7, 2, 9, 1]))
16+
17+
module.exports = findLargestElement;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Implement a class `Calculator` having below methods
3+
- initialise a result variable in the constructor and keep updating it after every arithmetic operation
4+
- add: takes a number and adds it to the result
5+
- subtract: takes a number and subtracts it from the result
6+
- multiply: takes a number and multiply it to the result
7+
- divide: takes a number and divide it to the result
8+
- clear: makes the `result` variable to 0
9+
- getResult: returns the value of `result` variable
10+
- calculate: takes a string expression which can take multi-arithmetic operations and give its result
11+
example input: `10 + 2 * ( 6 - (4 + 1) / 2) + 7`
12+
Points to Note:
13+
1. the input can have multiple continuous spaces, you're supposed to avoid them and parse the expression correctly
14+
2. the input can have invalid non-numerical characters like `5 + abc`, you're supposed to throw error for such inputs
15+
16+
Once you've implemented the logic, test your code by running
17+
*/
18+
19+
class Calculator {}
20+
21+
module.exports = Calculator;
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
Implement a class `Todo` having below methods
3+
- add(todo): adds todo to list of todos
4+
- remove(indexOfTodo): remove todo from list of todos
5+
- update(index, updatedTodo): update todo at given index
6+
- getAll: returns all todos
7+
- get(indexOfTodo): returns todo at given index
8+
- clear: deletes all todos
9+
10+
Once you've implemented the logic, test your code by running
11+
*/
12+
13+
class Todo {
14+
15+
}
16+
17+
module.exports = Todo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
Implement a function `countVowels` that takes a string as an argument and returns the number of vowels in the string.
3+
Note: Consider both uppercase and lowercase vowels ('a', 'e', 'i', 'o', 'u').
4+
5+
Once you've implemented the logic, test your code by running
6+
*/
7+
8+
function countVowels(str) {
9+
// Your code here
10+
}
11+
12+
module.exports = countVowels;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
Implement a function `isPalindrome` which takes a string as argument and returns true/false as its result.
3+
Note: the input string is case-insensitive which means 'Nan' is a palindrom as 'N' and 'n' are considered case-insensitive.
4+
*/
5+
6+
function isPalindrome(str) {
7+
return true;
8+
}
9+
10+
module.exports = isPalindrome;
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
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.
3+
Try running it for
4+
1. Sum from 1-100
5+
2. Sum from 1-100000
6+
3. Sum from 1-1000000000
7+
Hint - use Date class exposed in JS
8+
There is no automated test for this one, this is more for you to understand time goes up as computation goes up
9+
*/
10+
11+
function calculateTime(n) {
12+
return 0.01;
13+
}

assignments-master/01-js/package-lock.json

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assignments-master/01-js/package.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "01",
3+
"version": "1.0.0",
4+
"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.",
5+
"main": "anagram.js",
6+
"directories": {
7+
"test": "tests"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC"
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const isAnagram = require('../easy/anagram');
2+
3+
describe('isAnagram', () => {
4+
test('returns true for anagrams', () => {
5+
expect(isAnagram('listen', 'silent')).toBe(true);
6+
expect(isAnagram('rail safety', 'fairy tales')).toBe(
7+
true
8+
);
9+
expect(isAnagram('openai', 'aiopen')).toBe(true);
10+
expect(isAnagram('', '')).toBe(true);
11+
});
12+
13+
test('returns false for non-anagrams', () => {
14+
expect(isAnagram('hello', 'world')).toBe(false);
15+
expect(isAnagram('openai', 'open')).toBe(false);
16+
expect(isAnagram('hello', 'lhel')).toBe(false);
17+
expect(isAnagram('working', 'non')).toBe(false);
18+
});
19+
20+
test('returns true for anagrams with different casing', () => {
21+
expect(isAnagram('Debit Card', 'Bad Credit')).toBe(
22+
true
23+
);
24+
expect(
25+
isAnagram('School MASTER', 'The ClassROOM')
26+
).toBe(true);
27+
});
28+
29+
test('returns true for anagrams with special characters', () => {
30+
expect(isAnagram('abc!', '!bac')).toBe(true);
31+
});
32+
33+
test('returns false for non-anagrams with special characters', () => {
34+
expect(isAnagram('hello', 'hello!')).toBe(false);
35+
expect(isAnagram('openai!', 'open')).toBe(false);
36+
});
37+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
const Calculator = require('../hard/calculator');
2+
3+
describe('Calculator', () => {
4+
let calc;
5+
6+
beforeEach(() => {
7+
calc = new Calculator();
8+
});
9+
10+
afterEach(() => {
11+
calc.clear();
12+
});
13+
14+
test('addition', () => {
15+
calc.add(5);
16+
expect(calc.getResult()).toBe(5);
17+
18+
calc.add(3);
19+
expect(calc.getResult()).toBe(8);
20+
});
21+
22+
test('subtraction', () => {
23+
calc.subtract(5);
24+
expect(calc.getResult()).toBe(-5);
25+
26+
calc.subtract(3);
27+
expect(calc.getResult()).toBe(-8);
28+
});
29+
30+
test('multiplication', () => {
31+
calc.add(4);
32+
calc.multiply(3);
33+
expect(calc.getResult()).toBe(12);
34+
35+
calc.multiply(0);
36+
expect(calc.getResult()).toBe(0);
37+
});
38+
39+
test('division', () => {
40+
calc.add(12);
41+
42+
calc.divide(4);
43+
expect(calc.getResult()).toBe(3);
44+
45+
expect(() => calc.divide(0)).toThrow(Error);
46+
expect(calc.getResult()).toBe(3);
47+
});
48+
49+
test('clear', () => {
50+
calc.add(5);
51+
calc.clear();
52+
expect(calc.getResult()).toBe(0);
53+
});
54+
55+
test('calculate addition and multiplication', () => {
56+
calc.calculate('2 + 3 * 4');
57+
expect(calc.getResult()).toBe(14);
58+
});
59+
60+
test('calculate division in expression', () => {
61+
calc.calculate('( 15 + 3) / 6 ');
62+
expect(calc.getResult()).toBe(3);
63+
});
64+
65+
test('calculate subtraction in expression', () => {
66+
calc.calculate('10 - (4 + 2)');
67+
expect(calc.getResult()).toBe(4);
68+
});
69+
70+
test('calculate complex expression', () => {
71+
calc.calculate('(2 + 3) * (6 - (4 + 1) / 2) + 7');
72+
expect(calc.getResult()).toBe(24.5);
73+
});
74+
test('calculate complex expression with spaces', () => {
75+
calc.calculate(
76+
'10 + 2 * ( 6 - (4 + 1) / 2) + 7'
77+
);
78+
expect(calc.getResult()).toBe(24);
79+
});
80+
81+
test('calculate expression with decimals', () => {
82+
calc.calculate('(2.5 + 1.5) * 3');
83+
expect(calc.getResult()).toBe(12);
84+
});
85+
86+
test('calculate expression with invalid characters', () => {
87+
expect(() => calc.calculate('5 + abc')).toThrow(Error);
88+
expect(() =>
89+
calc.calculate('10 * (2 + 3) + xyz')
90+
).toThrow(Error);
91+
});
92+
93+
test('calculate division by zero', () => {
94+
expect(() => calc.calculate('10 / 0')).toThrow(Error);
95+
});
96+
97+
test('multiplication with negative numbers', () => {
98+
calc.add(-5);
99+
calc.multiply(-3);
100+
expect(calc.getResult()).toBe(15);
101+
102+
calc.multiply(0);
103+
expect(calc.getResult()).toBe(0);
104+
});
105+
106+
test('division with decimal numbers', () => {
107+
calc.add(10);
108+
calc.divide(3);
109+
expect(calc.getResult()).toBeCloseTo(3.333333, 2);
110+
111+
calc.divide(2);
112+
expect(calc.getResult()).toBeCloseTo(1.666666, 2);
113+
});
114+
115+
test('expression with invalid parentheses', () => {
116+
expect(() => calc.calculate('10 + (2 + 3')).toThrow(
117+
Error
118+
);
119+
expect(() => calc.calculate('10 + 2) + 3')).toThrow(
120+
Error
121+
);
122+
expect(() => calc.calculate(')10 + 2(')).toThrow(Error);
123+
});
124+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const countVowels = require('../medium/countVowels');
2+
3+
describe('countVowels', () => {
4+
test('returns the correct count for strings with vowels', () => {
5+
expect(countVowels('hello')).toBe(2);
6+
expect(countVowels('programming')).toBe(3);
7+
expect(countVowels('OpenAI')).toBe(4);
8+
});
9+
10+
test('returns 0 for strings without vowels', () => {
11+
expect(countVowels('rhythm')).toBe(0);
12+
expect(countVowels('fly')).toBe(0);
13+
expect(countVowels('chatbot')).toBe(2);
14+
});
15+
16+
test('returns 0 for an empty string', () => {
17+
expect(countVowels('')).toBe(0);
18+
});
19+
20+
test('handles case-insensitivity correctly', () => {
21+
expect(countVowels('EaSiEr')).toBe(4);
22+
expect(countVowels('QUIET')).toBe(3);
23+
expect(countVowels('aEIOU')).toBe(5);
24+
});
25+
26+
test('returns the correct count for strings with spaces', () => {
27+
expect(countVowels('the quick brown fox')).toBe(5);
28+
expect(countVowels('a e i o u')).toBe(5);
29+
expect(countVowels('testing spaces')).toBe(4);
30+
});
31+
32+
test('returns the correct count for strings with punctuation marks', () => {
33+
expect(countVowels('Hello, world!')).toBe(3);
34+
expect(countVowels('Coding is fun!!!')).toBe(4);
35+
expect(countVowels('Keep smiling, boo.')).toBe(6);
36+
});
37+
});

0 commit comments

Comments
 (0)