diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 000000000..f91284705 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "windows-gcc-x86", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "C:/MinGW/bin/gcc.exe", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "windows-gcc-x86", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..2f014ea69 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": true, + "cwd": ".", + "program": "build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..bb879da5a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/codewars/Adding Big Numbers/.gitkeep b/codewars/Adding Big Numbers/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Adding Big Numbers/Adding_Big_Numbers.cpp b/codewars/Adding Big Numbers/Adding_Big_Numbers.cpp new file mode 100644 index 000000000..c90e0b0f4 --- /dev/null +++ b/codewars/Adding Big Numbers/Adding_Big_Numbers.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; +typedef unsigned long long ull; + +string add(const string& a, const string& b) { + string ans; + char n = '0', c; + short int t; + const int A_size = a.size(), B_size = b.size(); + if (A_size == 0) { + if (B_size == 0) return "0"; + return b; + } + if (B_size == 0) return a; + + for (int i = 1; i <= max(A_size, B_size); i++) { + t = n + (A_size >= i) * (a[A_size - i] - '0') + + (B_size >= i) * (b[B_size - i] - '0') - '0'; + c = 48 + t % 10; + n = 48 + t / 10; + ans = c + ans; + } + if (n != '0') ans = n + ans; + return ans; +} \ No newline at end of file diff --git a/codewars/Anagram difference/.gitkeep b/codewars/Anagram difference/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Anagram difference/Anagram_difference.c b/codewars/Anagram difference/Anagram_difference.c new file mode 100644 index 000000000..2589b6df9 --- /dev/null +++ b/codewars/Anagram difference/Anagram_difference.c @@ -0,0 +1,18 @@ +#include + +unsigned anagram_difference(const char *word1, const char *word2) { + int A[26] = {}, B[26] = {}; + int i = 0; + while (word1[i] != '\0') { + A[word1[i] - 'a']++; + i++; + } + i = 0; + while (word2[i] != '\0') { + B[word2[i] - 'a']++; + i++; + } + unsigned a = 0; + for (int i = 0; i < 26; i++) a += abs(A[i] - B[i]); + return a; +} diff --git a/codewars/Array Deep Count/.gitkeep b/codewars/Array Deep Count/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Array Deep Count/Array_Deep_Count.py b/codewars/Array Deep Count/Array_Deep_Count.py new file mode 100644 index 000000000..f8c874b89 --- /dev/null +++ b/codewars/Array Deep Count/Array_Deep_Count.py @@ -0,0 +1,10 @@ +def R(a): + b = 0 + for i in a: + if type(i) == list: + b += R(i) + b += 1 + return b + +def deep_count(a): + return R(a) \ No newline at end of file diff --git a/codewars/Build Tower/.gitkeep b/codewars/Build Tower/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Build Tower/Build_Tower.cpp b/codewars/Build Tower/Build_Tower.cpp new file mode 100644 index 000000000..ffd08048c --- /dev/null +++ b/codewars/Build Tower/Build_Tower.cpp @@ -0,0 +1,14 @@ +#include + +using namespace std; + +void towerBuilder(unsigned nFloors) { + vector V(nFloors, "*"); + for (int i = 0; i < nFloors; i++) { + string a, b; + for (int j = i; j < nFloors - 1; j++) a += "-"; + for (int k = 0; k < i; k++) b += "*"; + V[i] = a + b + V[i] + b + a; + cout << V[i] << endl; + } +} \ No newline at end of file diff --git a/codewars/Convert string to camel case/.gitkeep b/codewars/Convert string to camel case/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Convert string to camel case/Convert_to_Camel_Case.cpp b/codewars/Convert string to camel case/Convert_to_Camel_Case.cpp new file mode 100644 index 000000000..cb6eeca95 --- /dev/null +++ b/codewars/Convert string to camel case/Convert_to_Camel_Case.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +string to_camel_case(string text) { + string ans; + for (int i = 0; i < text.size(); i++) { + if ((text[i] == '-' || text[i] == '_') + && i < text.size() - 1) { + if (text[i + 1] >= 97 && text[i + 1] <= 122) { + ans += text[i + 1] - 32; + } else ans += text[i + 1]; + i++; + } + else ans += text[i]; + } + return ans; +} \ No newline at end of file diff --git a/codewars/Duplicate Encoder/.gitkeep b/codewars/Duplicate Encoder/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Duplicate Encoder/Duplicate_Encoder.cpp b/codewars/Duplicate Encoder/Duplicate_Encoder.cpp new file mode 100644 index 000000000..6126a3c81 --- /dev/null +++ b/codewars/Duplicate Encoder/Duplicate_Encoder.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +string duplicate_encoder(const string& word) { + string s = word; + int M[256] = {}; + for (int i = 0; i < s.size(); i++) { + if (s[i] >= 97 && s[i] <= 122) M[s[i] - 32]++; + if (s[i] >= 65 && s[i] <= 90) M[s[i] + 32]++; + M[s[i]]++; + } + for (int i = 0; i < s.size(); i++) { + if (M[s[i]] == 1) s[i] = '('; + else s[i] = ')'; + } + return s; +} diff --git a/codewars/Find the missing letter/.gitkeep b/codewars/Find the missing letter/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Find the missing letter/Find_the_Missing_Letter.cpp b/codewars/Find the missing letter/Find_the_Missing_Letter.cpp new file mode 100644 index 000000000..ae168b826 --- /dev/null +++ b/codewars/Find the missing letter/Find_the_Missing_Letter.cpp @@ -0,0 +1,10 @@ +#include + +using namespace std; + +char findMissingLetter(const vector& chars) { + for (int i = 1; i < chars.size(); i++) { + if (chars[i] - chars[i - 1] > 1) return chars[i] - 1; + } + return chars[0] - 1; +} diff --git a/codewars/Flatten a Nested Map/.gitkeep b/codewars/Flatten a Nested Map/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Flatten a Nested Map/Flatten_a_Nested_Map.js b/codewars/Flatten a Nested Map/Flatten_a_Nested_Map.js new file mode 100644 index 000000000..d2301a26d --- /dev/null +++ b/codewars/Flatten a Nested Map/Flatten_a_Nested_Map.js @@ -0,0 +1,40 @@ +function flattenMap(map) { + let m = [map] + let a = [] + const ans = {} + let node = null + let parent = null + let pkey = null + + while (Object.keys(map).length) { + + if (m.length) { + node = m.pop() + } else { + node = map + } + + const keys = Object.keys(node) + const value = node[keys[0]] + a.push(keys[0]) + + if (value && value.toString() === "[object Object]") { + parent = node + pkey = keys[0] + m.push(value) + } else { + ans[a.join("/")] = value + a = [] + delete node[keys[0]] + + if (!Object.keys(node).length) { + if (parent) { + delete parent[pkey] + } + } + m = [] + m.push(map) + } + } + return ans +} \ No newline at end of file diff --git a/codewars/Fun with tree - max sum/.gitkeep b/codewars/Fun with tree - max sum/.gitkeep deleted file mode 100644 index 8b1378917..000000000 --- a/codewars/Fun with tree - max sum/.gitkeep +++ /dev/null @@ -1 +0,0 @@ - diff --git a/codewars/Fun with tree - max sum/Fun_with_Trees_Max_Sum.cpp b/codewars/Fun with tree - max sum/Fun_with_Trees_Max_Sum.cpp new file mode 100644 index 000000000..de90b61fc --- /dev/null +++ b/codewars/Fun with tree - max sum/Fun_with_Trees_Max_Sum.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +class Solution { + public: + static int maxSum(TreeNode* root) { + if (!root) return root -> value; + if (!root -> left) return root -> value + maxSum(root -> right); + if (!root -> right) return root -> value + maxSum(root -> left); + return root -> value + max(maxSum(root -> left), maxSum(root -> right)); + } +}; diff --git a/codewars/Linked Lists - Sorted Insert/.gitkeep b/codewars/Linked Lists - Sorted Insert/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Linked Lists - Sorted Insert/Linked_Lists_Sorted_Insert.py b/codewars/Linked Lists - Sorted Insert/Linked_Lists_Sorted_Insert.py new file mode 100644 index 000000000..68fadb444 --- /dev/null +++ b/codewars/Linked Lists - Sorted Insert/Linked_Lists_Sorted_Insert.py @@ -0,0 +1,15 @@ +def sorted_insert(head, data): + V = [data] + temp = head + while temp: + V.append(temp.data) + temp = temp.next + head = v = None + for i in sorted(V): + if head is None: + head = Node(i) + v = head + else: + v.next = Node(i) + v = v.next + return head \ No newline at end of file diff --git a/codewars/Merge two arrays/.gitkeep b/codewars/Merge two arrays/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Merge two arrays/Merge_Two_Arrays.c b/codewars/Merge two arrays/Merge_Two_Arrays.c new file mode 100644 index 000000000..28eacddfd --- /dev/null +++ b/codewars/Merge two arrays/Merge_Two_Arrays.c @@ -0,0 +1,21 @@ +#include +#include +#include + +void merge_arrays ( + size_t len_a, const int a[len_a], + size_t len_b, const int b[len_b], + int merged[len_a + len_b] + ) { + int i = 0, j = 0; + while (i + j < len_a + len_b) { + if (i < len_a) { + merged[i + j] = a[i]; + i++; + } + if (j < len_b) { + merged[i + j] = b[j]; + j++; + } + } +} diff --git a/codewars/Moving Zeros To The End/.gitkeep b/codewars/Moving Zeros To The End/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Moving Zeros To The End/Moving_Zeroes_To_The_End.cpp b/codewars/Moving Zeros To The End/Moving_Zeroes_To_The_End.cpp new file mode 100644 index 000000000..97a441444 --- /dev/null +++ b/codewars/Moving Zeros To The End/Moving_Zeroes_To_The_End.cpp @@ -0,0 +1,14 @@ +#include + +using namespace std; + +vector move_zeroes(const vector & input) { + vector V; + int a = 0; + for (int i = 0; i < input.size(); i++) { + if (input[i] == 0) a++; + else V.push_back(input[i]); + } + while (a--) V.push_back(0); + return V; +} \ No newline at end of file diff --git a/codewars/Permutations/.gitkeep b/codewars/Permutations/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Permutations/So_Many_Permutations.cpp b/codewars/Permutations/So_Many_Permutations.cpp new file mode 100644 index 000000000..a423cc2ff --- /dev/null +++ b/codewars/Permutations/So_Many_Permutations.cpp @@ -0,0 +1,13 @@ +#include +#include + +using namespace std; + +vector permutations(string s) { + vector v; + sort(s.begin(), s.end()); + do {v.push_back(s);} + while (next_permutation(s.begin(), s.end())); + + return v; +} diff --git a/codewars/Product of consecutive Fib numbers/.gitkeep b/codewars/Product of consecutive Fib numbers/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Product of consecutive Fib numbers/Product_of_Consecutive_Fib.cpp b/codewars/Product of consecutive Fib numbers/Product_of_Consecutive_Fib.cpp new file mode 100644 index 000000000..11d57735a --- /dev/null +++ b/codewars/Product of consecutive Fib numbers/Product_of_Consecutive_Fib.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; + +typedef unsigned long long ull; +class ProdFib +{ + public: + static vector productFib(ull prod) { + ull a = 0, b = 1; + while (a * b < prod) { + swap(a, b); + b += a; + } + return {a, b, (a * b == prod ? true : false)}; + } +}; diff --git a/codewars/Simple Pig Latin/.gitkeep b/codewars/Simple Pig Latin/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Simple Pig Latin/Simple_Pig_Latin.cpp b/codewars/Simple Pig Latin/Simple_Pig_Latin.cpp new file mode 100644 index 000000000..0e9b36d49 --- /dev/null +++ b/codewars/Simple Pig Latin/Simple_Pig_Latin.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +string pig_it(string str) { + int t = 0; + string temp, ans; + for (int i = 0; i < str.size() - 1; i++) { + if (str[i] == ' ') { + if (str[t] != '!' && str[t] != '?' && str[t] != '.' && str[t] != ','){ + temp = str.substr(t + 1, i - t - 1) + str[t] + "ay "; + t = i + 1; + ans += temp; + } + else { + temp = str.substr(t + 1, i - t - 1) + str[t] + " "; + t = i + 1; + ans += temp; + } + } + } + if (str[t] != '!' && str[t] != '?' && str[t] != '.' && str[t] != ',') + ans = ans + str.substr(t + 1, str.size() - t - 1) + str[t] + "ay"; + else ans += str[t]; + return ans; +} diff --git a/codewars/Snail/.gitkeep b/codewars/Snail/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Snail/Snail.cpp b/codewars/Snail/Snail.cpp new file mode 100644 index 000000000..9e083ba28 --- /dev/null +++ b/codewars/Snail/Snail.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +vector snail(const vector > &snail_map) { + int n = snail_map[0].size(); + vector > V (n, vector (n, 0)); + int x = 0, y = 0, d = 0, k = 1; + int dy[4] = {1, 0, -1, 0}; + int dx[4] = {0, 1, 0, -1}; + for (int i = 0; i < n * n; i++) { + V[x][y] = k; + k++; + if (x + dx[d] < 0 || x + dx[d] >= n || + y + dy[d] < 0 || y + dy[d] >= n || + V[x + dx[d]][y + dy[d]] != 0) d = (d + 1) % 4; + x = x + dx[d]; + y = y + dy[d]; + } + vector ans(n * n); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + ans[V[i][j] - 1] = snail_map[i][j]; + } + } + return ans; +} \ No newline at end of file diff --git a/codewars/Sum of Digits - Digital Root/.gitkeep b/codewars/Sum of Digits - Digital Root/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Sum of Digits - Digital Root/Sum_of_Digits_Digital_Root.cpp b/codewars/Sum of Digits - Digital Root/Sum_of_Digits_Digital_Root.cpp new file mode 100644 index 000000000..aaad981cb --- /dev/null +++ b/codewars/Sum of Digits - Digital Root/Sum_of_Digits_Digital_Root.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +int digital_root(int n) { + int sum = 0; + while (n > 0) { + sum += n % 10; + n /= 10; + } + if (sum >= 10) sum = digital_root(sum); + return sum; +} \ No newline at end of file diff --git a/codewars/Sum of Intervals/.gitkeep b/codewars/Sum of Intervals/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Sum of Intervals/Sum_of_Intervals.cpp b/codewars/Sum of Intervals/Sum_of_Intervals.cpp new file mode 100644 index 000000000..8107cc8af --- /dev/null +++ b/codewars/Sum of Intervals/Sum_of_Intervals.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int sum_intervals(vector > intervals) { + sort(intervals.begin(), intervals.end()); + int a = 0, L = intervals[0].first, R = intervals[0].second; + for (int i = 1; i < intervals.size(); i++) { + if (intervals[i].first >= R) { + //a += (R + L - 1)*(R - L) / 2; + a += R - L; + L = intervals[i].first; + R = intervals[i].second; + } + else { + //a += (intervals[i].first + L - 1)*(intervals[i].first - L) / 2; + a += intervals[i].first - L; + L = intervals[i].first; + R = max(R,intervals[i].second); + } + } + //a += (R + L - 1)*(R - L) / 2; + a += R - L; + return a; +} diff --git a/codewars/Sum of pairs/.gitkeep b/codewars/Sum of pairs/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Sum of pairs/Sum_of_Pairs.py b/codewars/Sum of pairs/Sum_of_Pairs.py new file mode 100644 index 000000000..898838a6d --- /dev/null +++ b/codewars/Sum of pairs/Sum_of_Pairs.py @@ -0,0 +1,6 @@ +def sum_pairs(nums: list[int], total: int) -> list[int]: + seens = set() + for num in nums: + if (seen := total - num) in seens: + return [seen, num] + seens.add(num) \ No newline at end of file diff --git a/codewars/Tic-Tac-Toe Checker/.gitkeep b/codewars/Tic-Tac-Toe Checker/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Tic-Tac-Toe Checker/Tic-Tac-Toe_Checker.c b/codewars/Tic-Tac-Toe Checker/Tic-Tac-Toe_Checker.c new file mode 100644 index 000000000..521bcd34f --- /dev/null +++ b/codewars/Tic-Tac-Toe Checker/Tic-Tac-Toe_Checker.c @@ -0,0 +1,37 @@ +enum game_state { + NOT_FINISHED = -1, DRAW = 0, X_WON = 1, O_WON = 2 +}; + +enum square { EMPTY = 0, X = 1, O = 2 }; + +enum game_state check_game_state (const enum square board[3][3]) +{ + if (board[0][0]!=0 && (board[0][0]==board[0][1] && board[0][0]==board[0][2] || + board[0][0]==board[1][0] && board[0][0]==board[2][0] || + board[0][0]==board[1][1] && board[0][0]==board[2][2] )) return board[0][0]; + if (board[0][1]!=0 && (board[0][1]==board[0][0] && board[0][1]==board[0][2] || + board[0][1]==board[1][1] && board[0][1]==board[2][1] )) return board[0][1]; + if (board[0][2]!=0 && (board[0][2]==board[0][0] && board[0][2]==board[0][1] || + board[0][2]==board[1][2] && board[0][2]==board[2][2] || + board[0][2]==board[1][1] && board[0][2]==board[2][0] )) return board[0][2]; + if (board[1][0]!=0 && (board[1][0]==board[0][0] && board[1][0]==board[2][0] || + board[1][0]==board[1][1] && board[1][0]==board[1][2] )) return board[1][0]; + if (board[1][1]!=0 && (board[1][1]==board[0][0] && board[1][1]==board[2][2] || + board[1][1]==board[0][2] && board[1][1]==board[2][0] || + board[1][1]==board[0][1] && board[1][1]==board[2][1] || + board[1][1]==board[1][0] && board[1][1]==board[1][2] )) return board[1][1]; + if (board[1][2]!=0 && (board[1][2]==board[0][2] && board[1][2]==board[2][2] || + board[1][2]==board[1][0] && board[1][2]==board[1][1] )) return board[1][2]; + if (board[2][0]!=0 && (board[2][0]==board[0][0] && board[2][0]==board[1][0] || + board[2][0]==board[2][1] && board[2][0]==board[2][2] || + board[2][0]==board[1][1] && board[2][0]==board[0][2] )) return board[2][0]; + if (board[2][1]!=0 && (board[2][1]==board[0][1] && board[2][1]==board[1][1] || + board[2][1]==board[2][0] && board[2][1]==board[2][2] )) return board[2][1]; + if (board[2][2]!=0 && (board[2][2]==board[2][0] && board[2][2]==board[2][1] || + board[2][2]==board[0][2] && board[2][2]==board[1][2] || + board[2][2]==board[0][0] && board[2][2]==board[1][1] )) return board[2][2]; + + int c = 0; + for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) if (board[i][j] == 0) c++; + if (c == 0) return 0; else return -1; +} diff --git a/codewars/Valid Parentheses/.gitkeep b/codewars/Valid Parentheses/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Valid Parentheses/Valid_Parentheses.c b/codewars/Valid Parentheses/Valid_Parentheses.c new file mode 100644 index 000000000..18d655c8e --- /dev/null +++ b/codewars/Valid Parentheses/Valid_Parentheses.c @@ -0,0 +1,17 @@ +#include + +bool validParentheses(const char *str_in) { + int p = 0, i = 0; + bool pr = true; + while (str_in[i] != '\0') { + if (str_in[i] == '(') p++; + if (str_in[i] == ')') p--; + if (p < 0) { + pr = false; + break; + } + i++; + } + if (p != 0) pr = false; + return pr; +} diff --git a/codewars/Where my anagrams at/.gitkeep b/codewars/Where my anagrams at/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/codewars/Where my anagrams at/Where_my_anagrams_at.py b/codewars/Where my anagrams at/Where_my_anagrams_at.py new file mode 100644 index 000000000..6d1111edd --- /dev/null +++ b/codewars/Where my anagrams at/Where_my_anagrams_at.py @@ -0,0 +1,17 @@ +def anagrams(word, words): + a = [0] * 26 + for c in word: + a[ord(c) - 97] += 1 + ans = [] + for s in words: + b = [0] * 26 + for c in s: + b[ord(c) - 97] += 1 + p = 1 + for i in range(26): + if a[i] != b[i]: + p = 0 + break + if p == 1: + ans.append(s) + return ans \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 000000000..9f25b8cbc --- /dev/null +++ b/package-lock.json @@ -0,0 +1,10 @@ +{ + "name": "TProgramming_2024", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "TProgramming_2024" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 000000000..680aef3dc --- /dev/null +++ b/package.json @@ -0,0 +1,6 @@ +{ + "name": "TProgramming_2024", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} diff --git a/rpgsaga/saga/src/function.cpp b/rpgsaga/saga/src/function.cpp new file mode 100644 index 000000000..0562512b9 --- /dev/null +++ b/rpgsaga/saga/src/function.cpp @@ -0,0 +1,73 @@ +#include "function.h" +#include +#include +//#include +#include +#include + +double Problem :: validate_p(double p) { + if (p > 1) { + return 1; + } + if (p < 0.0001) { + return 0.0001; + } + return p; +} +double Problem :: validate_x(double x) { + const double p = Problem :: precision; + double v = x / M_PI; + //x cannot be Pi+-2Pi*n and 10*Pi*n, where n is integer + if (std :: abs(v - (int)v) < p && + //if (std :: fabs(v - (int)v) < p && + ((int)v % 2 != 0 || (int)v % 10 == 0)) { + return (v - (int)v < 0 ? x - p: x + p); + //if x is invalid, it is changed to closest valid x + } + return x; +} +std :: vector Problem :: validate_x(std :: vector X) { + for (int i = 0; i < (int)X.size(); i++) + X[i] = (Problem :: validate_x(X[i])); + return X; +} +double Problem :: round_up(double v) { + const double p = Problem :: precision; + return std :: round(v / p) * p; + //safer rounding on doubles +} +double Problem :: solve_y(double x) { + const double p = Problem :: precision, + a = Problem :: a, + b = Problem :: b; + return round_up((a + pow(tan(b * x), 2)) / + (b + pow(tan(M_PI_2 - a * x), 2))); + //solving the problem itself and rounding up +} +std :: vector Problem :: solve_y(std :: vector X) { + std :: vector Y; + for (int i = 0; i < (int)X.size(); i++) + Y.push_back(Problem :: solve_y(X[i])); + return Y; +} + +//validate all values passed and solve the problem in constructor +Problem :: Problem() {} +Problem :: Problem(double p, double x) : + precision(validate_p(p)), + x(validate_x(x)), + y(solve_y(x)) {} +Problem :: Problem(double p, std :: vector X) : + precision(validate_p(p)), + X(validate_x(X)), + Y(solve_y(X)) {} + +double Problem :: pass_p() { + return Problem :: precision; +} +double Problem :: pass_y() { + return Problem :: y; +} +std :: vector Problem :: pass_Y() { + return Problem :: Y; +} diff --git a/rpgsaga/saga/src/function.h b/rpgsaga/saga/src/function.h new file mode 100644 index 000000000..6533a05fa --- /dev/null +++ b/rpgsaga/saga/src/function.h @@ -0,0 +1,32 @@ +#pragma once +#ifndef FUNCTION_H +#define FUNCTION_H +#include + +class Problem { + +private: + +const double a = 0.1, b = 0.5; +double precision, x, y; +std :: vector X, Y; + +double validate_p(double p); +double validate_x(double x); +std :: vector validate_x(std :: vector X); +double round_up(double v); +double solve_y(double x); +std :: vector solve_y(std :: vector X); + +public: + +Problem(); +Problem(double precision, double x); +Problem(double precision, std :: vector X); + +double pass_p(); +double pass_y(); +std :: vector pass_Y(); +}; + +#endif diff --git a/rpgsaga/saga/src/test.cpp b/rpgsaga/saga/src/test.cpp new file mode 100644 index 000000000..92d670035 --- /dev/null +++ b/rpgsaga/saga/src/test.cpp @@ -0,0 +1,57 @@ +#include "test.h" +#include +#include +/* encountered a bug where abs() is int overloaded despite the signature, + could be resolved via substitution with fabs() or usage of*/ +//#include +#include +#include + +Test :: Test() {} +Test :: Test(double precision, double actual, double expected) : + precision(precision), + actual(actual), + expected(expected) {} +Test :: Test(double precision, + std :: vector Actual, + std :: vector Expected) : + precision(precision), + Actual(Actual), + Expected(Expected) {} + +//reject the answer if difference is greater-equal than precision +void Test :: evaluate_test() { + //if (std :: abs(Test :: actual - Test :: expected) >= precision) + if (std :: fabs(Test :: actual - Test :: expected) >= precision) + throw std :: string {"Test failed!"}; + else std :: cout << std :: string {"Test passed!"} << std :: endl; +} +void Test :: evaluate_Test() { + std :: vector A = Test :: Actual; + std :: vector E = Test :: Expected; + for (int i = 0; i < (int)A.size(); i++) { + //if (std :: abs(A[i] - E[i]) >= precision) + if (std :: fabs(A[i] - E[i]) >= precision) + throw std :: string {"Test failed!"}; + } + std :: cout << std :: string {"Test passed!"} << std :: endl; +} +//non-members +void try_test(Problem& problem, double expect) { + std :: cout << std :: string{"running test... "}; + try { + Test test(problem.pass_p(), problem.pass_y(), expect); + test.evaluate_test(); + } catch (const std :: string& error_message) { + std :: cout << error_message << std :: endl; + } +} +void try_test(Problem& problem, std :: vector & expect) { + std :: cout << std :: string{"running test... "}; + try { + Test test(problem.pass_p(), problem.pass_Y(), expect); + test.evaluate_Test(); + } catch (const std :: string& error_message) { + std :: cout << error_message << std :: endl; + } +} diff --git a/rpgsaga/saga/src/test.h b/rpgsaga/saga/src/test.h new file mode 100644 index 000000000..1952106f8 --- /dev/null +++ b/rpgsaga/saga/src/test.h @@ -0,0 +1,31 @@ +#pragma once +#ifndef TEST_H +#define TEST_H +#include "function.h" +#include +#include +#include +#include + +class Test { + +private: + +double precision, actual, expected; +std :: vector Actual, Expected; + +public: + +Test(); +Test(double precision, double actual, double expected); +Test(double precision, + std :: vector Actual, + std :: vector Expected); + +void evaluate_test(); //single value +void evaluate_Test(); //multiple (vector of) values +}; +//non-members functions to help run tests +void try_test(Problem& problem, double expect); +void try_test(Problem& problem, std :: vector & expect); +#endif diff --git a/rpgsaga/saga/tests/drivercode.cpp b/rpgsaga/saga/tests/drivercode.cpp new file mode 100644 index 000000000..9af28f3d5 --- /dev/null +++ b/rpgsaga/saga/tests/drivercode.cpp @@ -0,0 +1,79 @@ +#include "test.h" +#include +#include +//#include +#include +#include + +int main() { + //first testing if Problem and Test are working properly + //testting Problem :: round_up() and Problem :: validate_p() + std :: cout << std :: string {"calculations are rounded up to precision which is limited to p=[0.0001..1],\notherwise it defaults to closest boundary"} << std :: endl; + for (double i = 0.00001; i <= 10; i *= 10) { + Problem problem_p(i, 3); + std :: cout << std :: string{"tried p: "} << i + << std :: string{", ans is "} << problem_p.pass_y() + << std :: endl; + } + std :: cout << std :: endl; + //testing Problem :: validate_x() + std :: cout << std :: string{"invalid x values are slightly modified to run calculations without problem,\nx cannot be Pi+-2Pi*n and 10*Pi*n, where n is integer"} << std :: endl; + Problem problem_x1(0.0001, 0); + std :: cout << std :: string{"tried x: 0, ans is "} << problem_x1.pass_y() << std :: endl; + Problem problem_x2(0.0001, 7 * M_PI); + std :: cout << std :: string{"tried x: 7*Pi, ans is "} << problem_x2.pass_y() << std :: endl; + Problem problem_x3(0.0001, 10 * M_PI); + std :: cout << std :: string{"tried x: 10*Pi, ans is "} << problem_x3.pass_y() << std :: endl; + Problem problem_x4(0.0001, -6 * M_PI + M_PI); + std :: cout << std :: string{"tried x: Pi-6*Pi, ans is "} << problem_x4.pass_y() << std :: endl; + std :: cout << std :: endl; + //testing Test :: evaluate_test() + std :: cout << std :: string{"test fails when difference between expected and actual values is greater than (equal to) p"} + << std :: endl; + double pt{0.001}; + double expected_t1{0.0068}; + Problem problem_t1(pt, 1.15); + std :: cout << std :: string{"p: "} << problem_t1.pass_p() + << std :: string{", expecting: "} << expected_t1 + << std :: string{", actual is "} << problem_t1.pass_y() + << std :: endl; + try_test(problem_t1, expected_t1); + double expected_t2{0.008}; + Problem problem_t2(pt, 1.15); + std :: cout << std :: string{"p: "} << problem_t2.pass_p() + << std :: string{", expecting: "} << expected_t2 + << std :: string{", actual is "} << problem_t2.pass_y() + << std :: endl; + try_test(problem_t2, expected_t2); + std :: cout << std :: endl; + //now seeing lab results + //problem A + double p{0.0001}; + double x_st{0.15}, x_fin{1.37}, x_dif{0.25}; + std :: vector A_data; + for (double x = x_st; x <= x_fin; x += x_dif) { + A_data.push_back(x); + } + Problem problem_A(p, A_data); + std :: vector actual_A = problem_A.pass_Y(); + std :: vector expected_A {0, 0.0002, 0.0009, 0.0027, 0.0069}; + //running tests on A + std :: cout << std :: string{"Problem A answers: "}; + for (int i = 0; i < (int)actual_A.size(); i++) + std :: cout << actual_A[i] << std :: string {" "}; + std :: cout << std :: endl; + try_test(problem_A, expected_A); + std :: cout << std :: endl; + //problem B + std :: vector B_data {0.2, 0.3, 0.44, 0.6, 0.56}; + Problem problem_B(p, B_data); + std :: vector actual_B {problem_B.pass_Y()}; + std :: vector expected_B {0, 0.0001, 0.0003, 0.0007, 0.0006}; + //running tests on B + std :: cout << std :: string{"Problem B answers: "}; + for (int i = 0; i < (int)actual_B.size(); i++) + std :: cout << actual_B[i] << std :: string {" "}; + std :: cout << std :: endl; + try_test(problem_B, expected_B); + std :: cout << std :: endl; +}