Skip to content

Commit

Permalink
(chore) Update categories with combinatoris algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
kounkou committed Nov 5, 2024
1 parent 35e7a3e commit 010def7
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 2 deletions.
23 changes: 23 additions & 0 deletions logic/combinatorics/count-combinations.js
Original file line number Diff line number Diff line change
@@ -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)
}`
},
]
26 changes: 26 additions & 0 deletions logic/combinatorics/count-permutations.js
Original file line number Diff line number Diff line change
@@ -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`
}
]
35 changes: 35 additions & 0 deletions logic/combinatorics/generate-subsets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var question = [
{
id: "subsets",
category: "Combinatorics",
placeHolderCpp: `void generateSubsets(const vector<int>& nums, int index, vector<int>& current, vector<vector<int>>& 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<int>& nums, int index, vector<int>& current, vector<vector<int>>& 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]
}`
}
]
45 changes: 45 additions & 0 deletions logic/combinatorics/unique-paths.js
Original file line number Diff line number Diff line change
@@ -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]
}`
}
]
2 changes: 1 addition & 1 deletion qml/CategoryPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
12 changes: 11 additions & 1 deletion qml/PracticePage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
<file alias="lcp.js">logic/string/lcp.js</file>
<file alias="rabin-karp.js">logic/string/rabin-karp.js</file>

<file alias="unique-paths.js">logic/combinatorics/unique-paths.js</file>
<file alias="count-permutations.js">logic/combinatorics/count-permutations.js</file>
<file alias="generate-subsets.js">logic/combinatorics/generate-subsets.js</file>
<file alias="count-combinations.js">logic/combinatorics/count-combinations.js</file>

<file alias="factorial.js">logic/recursion/factorial.js</file>

<file alias="fractional-knapsack.js">logic/greedy/fractional-knapsack.js</file>
Expand Down

0 comments on commit 010def7

Please sign in to comment.