diff --git a/C++/h1.cpp b/C++/h1.cpp index 2d23603..f76d9df 100644 --- a/C++/h1.cpp +++ b/C++/h1.cpp @@ -4,6 +4,7 @@ // which should be the return value for the function longest pallindrome(char* s). #include +#include using namespace std; void printSubStr( @@ -36,10 +37,10 @@ int longestPalSubstr(string str) } for (int k = 3; k <= n; ++k) { - for (int i = 0; i < n - k; ++i) { + for (int i = 0; i < n - k+1; ++i) { int j = i + k - 1; - if (table[i + 1][j] && str[i] == str[j]) { + if (table[i + 1][j-1] && str[i] == str[j]) { table[i][j] = true; if (k > maxLength) { diff --git a/C++/m1.cpp b/C++/m1.cpp index caa5f1c..48f6437 100644 --- a/C++/m1.cpp +++ b/C++/m1.cpp @@ -3,42 +3,46 @@ #include using namespace std; -class Solution +bool isPalindrome(string s) { -public: - bool isPalindrome(string s) - { - int i = 0, j = s.size(); + int i = 0, j = s.size() - 1; - while (i < j) - { - if (s[i++] != s[j--]) - return false; - } - return true; - } + while (i < j) + { + if (s[i++] != s[j--]) + return false; + } + return true; +} - string longestPalindrome(string s) - { - int n = s.size(); - if (n == 0) - return ""; +string longestPalindrome(string s) +{ + int n = s.size(); + if (n == 0) + return ""; + + if (n == 1) + return s; - if (n == 1) - return s; + string result = ""; + for (int i = 0; i < n - 1; i++) + { + for (int j = 1; j <= n - i; j++) + { + if (isPalindrome(s.substr(i, j))) + { + if (result.size() < s.substr(i, j).size()) + result = s.substr(i, j); + } + } + } + return result; +} - string result = ""; - for (int i = 0; i < n - 1; i++) - { - for (int j = 1; j <= n - i; j++) - { - if (isPalindrome(s.substr(i, j))) - { - if (result.size() < j) - result = s.substr(i, j + 1); - } - } - } - return result; - } -}; +int main() +{ + string s = "babad"; + // cout << isPalindrome(s); + cout << longestPalindrome(s); + return 0; +} \ No newline at end of file diff --git a/C++/m2.cpp b/C++/m2.cpp index c42208e..6f5d35f 100644 --- a/C++/m2.cpp +++ b/C++/m2.cpp @@ -1,35 +1,53 @@ // find unique triplet such that the sum is 0 -#include -#include -#include +#include +#include +#include #include using namespace std; -class Solution { -public: - vector> threeSum(vector& nums) { - int target = 0; - sort(nums.begin(), nums.end()); - set> s; - vector> output; - for (int i = 0; i < nums.size(); i++){ - int j = i + 1; - int k = i+j; - while (j < k) { - int sum = nums[i] + nums[j] + nums[k]; - if (sum == target) { - s.insert({nums[i], nums[j], nums[k]}); - j++; - } else if (sum < target) { - j++; - } else { - k--; - } - } - } - for(auto triplets : s) - output.push_back(triplets); - return output; - } -}; +vector> threeSum(vector &nums) +{ + int target = 0; + sort(nums.begin(), nums.end()); + set> s; + vector> output; + for (int i = 0; i < nums.size(); i++) + { + int j = i + 1; + int k = nums.size() - 1; + while (j < k) + { + int sum = nums[i] + nums[j] + nums[k]; + if (sum == target) + { + s.insert({nums[i], nums[j], nums[k]}); + j++; + } + else if (sum < target) + { + j++; + } + else + { + k--; + } + } + } + for (auto triplets : s) + output.push_back(triplets); + return output; +} + +int main() +{ + vector nums = {-1, 0, 1, 2, -1, -4}; + vector> output = threeSum(nums); + for (auto triplets : output) + { + for (auto num : triplets) + cout << num << " "; + cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/C++/s2.cpp b/C++/s2.cpp index d995ee5..507df77 100644 --- a/C++/s2.cpp +++ b/C++/s2.cpp @@ -12,7 +12,7 @@ int main(){ for(int i=0; i < myVec.size(); i++){ std :: cout << myVec[i] << std :: endl; } - std :: cout << "Element at index 3: "<< myVec.at(3) << std::endl; - + std :: cout << "Element at index 3: "<< myVec[3] << std :: endl; + return 0; } diff --git a/JS & TS/four.js b/JS & TS/four.js index 71006fa..92a1ac0 100644 --- a/JS & TS/four.js +++ b/JS & TS/four.js @@ -21,7 +21,7 @@ let team = [ }, ]; const filterBySalary = (peopleArray, threshold = 300) => { - return peopleArray.find((person) => person.salary > threshold); + return peopleArray.filter((person) => person.salary > threshold); }; const filteredTeam = filterBySalary(team, 300); console.log(team, filteredTeam); diff --git a/JS & TS/one.js b/JS & TS/one.js index b2f1d83..b041c1e 100644 --- a/JS & TS/one.js +++ b/JS & TS/one.js @@ -1,5 +1,11 @@ -const printNames = (names) => { - return `We are ${names.toLowerCase()}`; +var getUser = function () { + var user = { + name: "user", + email: "user@gmail.com", + password: "snvpoj9f" + }; + return user; }; -const intro = printNames("krish" + +"bob"); -console.log(intro); +var user1 = getUser(); +// Print the user1 object +console.log(user1); diff --git a/JS & TS/one.ts b/JS & TS/one.ts index 95f1155..343140c 100644 --- a/JS & TS/one.ts +++ b/JS & TS/one.ts @@ -1,6 +1,7 @@ type User = { name: string; email: string; + password: string; }; const getUser = (): User => { @@ -13,3 +14,5 @@ const getUser = (): User => { }; const user1 = getUser(); +// Print the user1 object +console.log(user1); \ No newline at end of file diff --git a/JS & TS/three.js b/JS & TS/three.js index e149777..d5660db 100644 --- a/JS & TS/three.js +++ b/JS & TS/three.js @@ -20,7 +20,7 @@ const team = [ }, ]; const hasFriends = (person) => { - return !(person.friends === false); + return !(person.friends === false) && person.friends !== undefined; }; const jamesHolden = team[3]; diff --git a/JS & TS/two.js b/JS & TS/two.js index 9664a6f..6950a1e 100644 --- a/JS & TS/two.js +++ b/JS & TS/two.js @@ -24,6 +24,11 @@ const noOfPeopleEarning = (employees) => { let total = 0; for (const employee of employees) { const earns = employee.earns; + // If earns is undefined, it will be false + if (earns === undefined) { + console.log("undefined"); + continue; + } if (earns === false) { continue; } diff --git a/Web-Dev/event-propogation/initial.js b/Web-Dev/event-propogation/initial.js index 71d2c11..19117c3 100644 --- a/Web-Dev/event-propogation/initial.js +++ b/Web-Dev/event-propogation/initial.js @@ -1,13 +1,15 @@ // Event listener for form submit document.getElementById("form").addEventListener("submit", function () { + // Prevent the default action of the event which is to submit the form + event.preventDefault(); // Prevents the default action of the event which is to submit the form signInWithUserNameAndPassword(); }); // Event listener for sign-in button document .getElementById("signin-button") - .addEventListener("click", function (event) { - event.preventDefault(); + .addEventListener("onClick", function (event) { + event.preventDefault(); // Prevents the default action of the event which is to submit the form console.log(userName, password); });