From 010def763b892b05e2370050d5fe46d80e64d589 Mon Sep 17 00:00:00 2001 From: jacques Date: Tue, 5 Nov 2024 15:33:47 -0800 Subject: [PATCH] (chore) Update categories with combinatoris algorithms --- logic/combinatorics/count-combinations.js | 23 ++++++++++++ logic/combinatorics/count-permutations.js | 26 +++++++++++++ logic/combinatorics/generate-subsets.js | 35 ++++++++++++++++++ logic/combinatorics/unique-paths.js | 45 +++++++++++++++++++++++ qml/CategoryPage.qml | 2 +- qml/PracticePage.qml | 12 +++++- resources.qrc | 5 +++ 7 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 logic/combinatorics/count-combinations.js create mode 100644 logic/combinatorics/count-permutations.js create mode 100644 logic/combinatorics/generate-subsets.js create mode 100644 logic/combinatorics/unique-paths.js diff --git a/logic/combinatorics/count-combinations.js b/logic/combinatorics/count-combinations.js new file mode 100644 index 0000000..8a5afbb --- /dev/null +++ b/logic/combinatorics/count-combinations.js @@ -0,0 +1,23 @@ +var question = [ + { + id: "count combis", + category: "Combinatorics", + placeHolderCpp: `int countCombinations(int n, int k) {\n // Base cases\n if (k == 0 || k == n) return 1;\n ...\n}\n`, + placeHolderGo: `func countCombinations(n int, k int) int {\n // Base cases\n if k == 0 || k == n {\n return 1\n }\n ...\n}\n`, + difficulty: "Easy", + question: "Ways to choose k items from n items (no order)", + answerImage: "", + answerCpp: `int countCombinations(int n, int k) { + if (k == 0 || k == n) return 1; + + return countCombinations(n - 1, k - 1) + countCombinations(n - 1, k); +} +`, + answerGo: `func countCombinations(n int, k int) int { + if k == 0 || k == n { + return 1 + } + return countCombinations(n-1, k-1) + countCombinations(n-1, k) +}` + }, +] \ No newline at end of file diff --git a/logic/combinatorics/count-permutations.js b/logic/combinatorics/count-permutations.js new file mode 100644 index 0000000..cfd5d20 --- /dev/null +++ b/logic/combinatorics/count-permutations.js @@ -0,0 +1,26 @@ +var question = [ + { + id: "Permutations", + category: "Combinatorics", + placeHolderCpp: `int countPermutations(int n, int k) {\n int result = 1;\n for (int i = 0; i < k; ++i) {\n result *= (n - i);\n }\n ...\n}\n`, + placeHolderGo: `func countPermutations(n int, k int) int {\n result := 1\n for i := 0; i < k; i++ {\n result *= (n - i)\n }\n ...\n}\n`, + difficulty: "Medium", + question: "Ways to arrange k items from a set of n-length permutations.", + answerImage: "", + answerCpp: `int countPermutations(int n, int k) { + int result = 1;\n for (int i = 0; i < k; ++i) { + result *= (n - i); + } + + return result; +}`, + answerGo: `func countPermutations(n int, k int) int { + result := 1 + + for i := 0; i < k; i++ { + result *= (n - i) + } + + return result\n}\n` + } +] \ No newline at end of file diff --git a/logic/combinatorics/generate-subsets.js b/logic/combinatorics/generate-subsets.js new file mode 100644 index 0000000..87be7ab --- /dev/null +++ b/logic/combinatorics/generate-subsets.js @@ -0,0 +1,35 @@ +var question = [ + { + id: "subsets", + category: "Combinatorics", + placeHolderCpp: `void generateSubsets(const vector& nums, int index, vector& current, vector>& result) {\n // Recursive function to generate all subsets of a set\n ...\n}\n`, + placeHolderGo: `func generateSubsets(nums []int, index int, current []int, result *[][]int) {\n // Recursive function to generate all subsets of a set\n ...\n}\n`, + difficulty: "Hard", + question: "Generate all subsets of a given set of integers.", + answerImage: "", + answerCpp: `void generateSubsets(const vector& nums, int index, vector& current, vector>& result) { + if (index == nums.size()) { + result.push_back(current); + return; + } + + generateSubsets(nums, index + 1, current, result); + current.push_back(nums[index]); + generateSubsets(nums, index + 1, current, result); + current.pop_back(); +}`, + answerGo: `func generateSubsets(nums []int, index int, current []int, result *[][]int) { + if index == len(nums) { + subset := make([]int, len(current)) + copy(subset, current) + *result = append(*result, subset) + return + } + + generateSubsets(nums, index+1, current, result) + current = append(current, nums[index]) + generateSubsets(nums, index+1, current, result) + current = current[:len(current)-1] +}` + } +] \ No newline at end of file diff --git a/logic/combinatorics/unique-paths.js b/logic/combinatorics/unique-paths.js new file mode 100644 index 0000000..e2c5153 --- /dev/null +++ b/logic/combinatorics/unique-paths.js @@ -0,0 +1,45 @@ +var question = [ + { + id: "unique paths", + category: "Combinatorics", + placeHolderCpp: `int uniquePaths(int m, int n) {\n // Number of unique paths in an m x n grid\n int paths = ...\n}\n`, + placeHolderGo: `func uniquePaths(m int, n int) int {\n // Number of unique paths in an m x n grid\n paths := ...\n}\n`, + difficulty: "Hard", + question: "top-left to the bottom-right unique paths", + answerImage: "", + answerCpp: `int uniquePaths(int m, int n) { + int paths[m][n]; + + for (int i = 0; i < m; i++) paths[i][0] = 1; + for (int j = 0; j < n; j++) paths[0][j] = 1; + for (int i = 1; i < m; i++) { + for (int j = 1; j < n; j++) { + paths[i][j] = paths[i - 1][j] + paths[i][j - 1]; + } + } + + return paths[m - 1][n - 1]; +}`, + answerGo: `func uniquePaths(m int, n int) int { + paths := make([][]int, m) + + for i := range paths { + paths[i] = make([]int, n) + } + + for i := 0; i < m; i++ { + paths[i][0] = 1 + } + for j := 0; j < n; j++ { + paths[0][j] = 1 + } + for i := 1; i < m; i++ { + for j := 1; j < n; j++ { + paths[i][j] = paths[i-1][j] + paths[i][j-1] + } + } + + return paths[m-1][n-1] +}` + } +] \ No newline at end of file diff --git a/qml/CategoryPage.qml b/qml/CategoryPage.qml index ff63e32..7f35a84 100644 --- a/qml/CategoryPage.qml +++ b/qml/CategoryPage.qml @@ -17,7 +17,7 @@ Rectangle { categoryModel.append({ name: "Graph", selected: false, enabled: true }); categoryModel.append({ name: "Binary Search", selected: false, enabled: true }); categoryModel.append({ name: "Bit Manipulation", selected: false, enabled: true }); - categoryModel.append({ name: "Combinatorics", selected: false, enabled: false }); + categoryModel.append({ name: "Combinatorics", selected: false, enabled: true }); categoryModel.append({ name: "Divide and Conquer", selected: false, enabled: false }); categoryModel.append({ name: "Disjoint Set", selected: false, enabled: true }); categoryModel.append({ name: "DP", selected: false, enabled: true }); diff --git a/qml/PracticePage.qml b/qml/PracticePage.qml index 35f0cfc..a2cf5a9 100644 --- a/qml/PracticePage.qml +++ b/qml/PracticePage.qml @@ -49,6 +49,11 @@ import "backtracking.js" as BackTracking import "lazy-propagation.js" as LazyPropag import "range-sum-queries.js" as RangeSumQueries +import "unique-paths.js" as UniquePaths +import "count-permutations.js" as CountPermutations +import "generate-subsets.js" as GenerateSubsets +import "count-combinations.js" as CountCombinations + Rectangle { id: root @@ -131,7 +136,12 @@ Rectangle { .concat(LazyPropag.question) .concat(RangeSumQueries.question) - + + .concat(UniquePaths.question) + .concat(CountPermutations.question) + .concat(GenerateSubsets.question) + .concat(CountCombinations.question) + .concat(LinearSearch.question); if (sessionObject.selectedCategories && sessionObject.selectedCategories.length > 0) { diff --git a/resources.qrc b/resources.qrc index e24f59d..55ed252 100644 --- a/resources.qrc +++ b/resources.qrc @@ -51,6 +51,11 @@ logic/string/lcp.js logic/string/rabin-karp.js + logic/combinatorics/unique-paths.js + logic/combinatorics/count-permutations.js + logic/combinatorics/generate-subsets.js + logic/combinatorics/count-combinations.js + logic/recursion/factorial.js logic/greedy/fractional-knapsack.js