From b9e442b064c82f37cd79bd28a71d265affa0ab89 Mon Sep 17 00:00:00 2001 From: HARSH VERMA Date: Wed, 25 Feb 2026 21:28:01 +0530 Subject: [PATCH 01/11] Refactor linear search to use vector instead of array --- .../cpp/linear_search/Linear Search In Array.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/code/languages/cpp/linear_search/Linear Search In Array.cpp b/code/languages/cpp/linear_search/Linear Search In Array.cpp index 3e1be3bcd5..a43aec3b81 100644 --- a/code/languages/cpp/linear_search/Linear Search In Array.cpp +++ b/code/languages/cpp/linear_search/Linear Search In Array.cpp @@ -2,12 +2,13 @@ //If x is present then return its location, otherwise return -1 #include +#include using namespace std; -int search(int arr[], int N, int x) +int linearSearch(vector arr, int x) { int i; - for (i = 0; i < N; i++) + for (i = 0; i < arr.size(); i++) if (arr[i] == x) return i; return -1; @@ -16,12 +17,11 @@ int search(int arr[], int N, int x) // Driver's code int main(void) { - int arr[] = { 2, 3, 4, 10, 40 }; + vector arr = { 2, 3, 4, 10, 40 }; int x = 10; - int N = sizeof(arr) / sizeof(arr[0]); // Function call - int result = search(arr, N, x); + int result = linearSearch(arr, x); (result == -1) ? cout << "Element is not present in array" : cout << "Element is present at index " << result; From 56e6dc886f1f9e515d7db7ae3e05f00c0da3a254 Mon Sep 17 00:00:00 2001 From: HARSH VERMA Date: Wed, 25 Feb 2026 21:30:52 +0530 Subject: [PATCH 02/11] Refactor rock-paper-scissors game logic Refactor game logic to use a dictionary for win conditions and improve input handling. --- .../rock_paper_scissors/rock_paper_scissor.py | 49 ++++++++++++------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/code/languages/python/rock_paper_scissors/rock_paper_scissor.py b/code/languages/python/rock_paper_scissors/rock_paper_scissor.py index a4eaaf12e2..ab946ebeb5 100644 --- a/code/languages/python/rock_paper_scissors/rock_paper_scissor.py +++ b/code/languages/python/rock_paper_scissors/rock_paper_scissor.py @@ -1,26 +1,37 @@ from random import choice -while (True): - print("Rock \nPaper \nScissors!!!!!") - player1 = input("Enter your choice: ") - choices = ["ROCK", "PAPER", "SCISSORS"] +choices = ["ROCK", "PAPER", "SCISSORS"] + +# Rules: what each choice beats +wins_against = { + "ROCK": "SCISSORS", + "SCISSORS": "PAPER", + "PAPER": "ROCK" +} + +while True: + print("\nRock\nPaper\nScissors!!!!!") + player1 = input("Enter your choice: ").strip().upper() + + if player1 not in choices: + print("Invalid choice. Please choose Rock, Paper, or Scissors.") + continue + player2 = choice(choices) + print("SHOOT!!!") - print(f"player 2 played {player2}") - if (player1 != player2): - if (player1.upper() == "ROCK") and (player2.upper() == "SCISSORS"): - print("player 1 wins") - elif (player1.upper() == "SCISSORS") and (player2.upper() == "PAPER"): - print("player 1 wins") - elif (player1.upper() == "PAPER") and (player2.upper() == "ROCK"): - print("player 1 wins") - else: - print("player 2 wins") - else: + print(f"Player 2 played {player2}") + + if player1 == player2: print("Tied") + elif wins_against[player1] == player2: + print("Player 1 wins 🎉") + else: + print("Player 2 wins 🤖") + print("---------------------------------------") - flag = input("Do you want to continue? ") - if flag.lower() == "no": + flag = input("Do you want to continue? (yes/no): ").strip().lower() + + if flag != "yes": + print("Thanks for playing!") break - elif flag.lower() == "yes": - continue \ No newline at end of file From 18d30017c72e0ed426422b1bdf20ecf3b6c29314 Mon Sep 17 00:00:00 2001 From: HARSH VERMA Date: Wed, 25 Feb 2026 21:53:26 +0530 Subject: [PATCH 03/11] Refactor linear search to use vectors and improve output --- .../Linear Search In Duplicate Array.cpp | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/code/languages/cpp/linear_search/Linear Search In Duplicate Array.cpp b/code/languages/cpp/linear_search/Linear Search In Duplicate Array.cpp index 3c2354fb2c..147d495919 100644 --- a/code/languages/cpp/linear_search/Linear Search In Duplicate Array.cpp +++ b/code/languages/cpp/linear_search/Linear Search In Duplicate Array.cpp @@ -1,41 +1,39 @@ -// C++ code to linearly search x in arr[]. -//If x is present then return its location, otherwise return -1 - #include +#include using namespace std; -int *linearSearch(int arr[], int result[], int N, int x) +void linearSearch(const vector& arr, vector& result, int x) { - int pos = -1, ind = 0; - for (int i = 0; i arr = {2, 3, 4, 10, 40, 3, 10, 2, 10}; int x = 10; - int N = sizeof(arr) / sizeof(arr[0]); - int result[N]; - linearSearch(arr, result, N, x); - int size = sizeof(result) / sizeof(result[0]); - cout<<"Key found at position(s) : \n"; - for (int i = 0; i < size; i++) + + vector result; + + linearSearch(arr, result, x); + + if (result.empty()) + { + cout << "Key not found"; + } + else { - if (result[i] != -1) - cout< Date: Wed, 25 Feb 2026 22:16:15 +0530 Subject: [PATCH 04/11] Refactor simple calculator to use calculate function Refactored calculator code to use a user-defined function for calculations. Improved structure and readability by separating calculation logic from user input. --- .../cpp/calculator/simpleCalculator.cpp | 74 +++++++++++-------- 1 file changed, 43 insertions(+), 31 deletions(-) diff --git a/code/languages/cpp/calculator/simpleCalculator.cpp b/code/languages/cpp/calculator/simpleCalculator.cpp index eecceb4387..06dc93f3b5 100644 --- a/code/languages/cpp/calculator/simpleCalculator.cpp +++ b/code/languages/cpp/calculator/simpleCalculator.cpp @@ -1,40 +1,52 @@ -# include +#include using namespace std; -int main() { - - char op; - float num1, num2; - - cout << "Enter operator: +, -, *, /: "; - cin >> op; - - cout << "Enter two operands: "; - cin >> num1 >> num2; - - switch(op) { +// User-defined function +float calculate(float num1, float num2, char op) +{ + switch (op) + { + case '+': + return num1 + num2; + + case '-': + return num1 - num2; + + case '*': + return num1 * num2; + + case '/': + if (num2 == 0) + { + cout << "Error! Division by zero.\n"; + return 0; + } + return num1 / num2; + + default: + cout << "Error! Operator is not correct.\n"; + return 0; + } +} - case '+': - cout << num1 << " + " << num2 << " = " << num1 + num2; - break; +int main() +{ + char op; + float num1, num2, result; - case '-': - cout << num1 << " - " << num2 << " = " << num1 - num2; - break; + cout << "Enter operator (+, -, *, /): "; + cin >> op; - case '*': - cout << num1 << " * " << num2 << " = " << num1 * num2; - break; + cout << "Enter two operands: "; + cin >> num1 >> num2; - case '/': - cout << num1 << " / " << num2 << " = " << num1 / num2; - break; + result = calculate(num1, num2, op); - default: - // If the operator is other than +, -, * or /, error message is shown - cout << "Error! operator is not correct"; - break; - } + // Print result only if operator is valid + if (op == '+' || op == '-' || op == '*' || op == '/') + { + cout << num1 << " " << op << " " << num2 << " = " << result; + } - return 0; + return 0; } From 1b3648f370a6ea93f4eba20798ef98083a79085d Mon Sep 17 00:00:00 2001 From: HARSH VERMA Date: Wed, 25 Feb 2026 22:34:42 +0530 Subject: [PATCH 05/11] Refactor linear search implementation and fix output --- .../languages/c/linear_search/linear_search.c | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/code/languages/c/linear_search/linear_search.c b/code/languages/c/linear_search/linear_search.c index a46cfc9d55..43b25e0e54 100644 --- a/code/languages/c/linear_search/linear_search.c +++ b/code/languages/c/linear_search/linear_search.c @@ -1,33 +1,33 @@ - #include - #include - /* - Input : integer array indexed from 0, key to be searched - Ouput : Position of the key in the array if found, else -1 - */ - int linearSearch(int a[], int n, int key) { - int pos = -1; - counter=0; - for (int i = 0; i < n; ++i) { - if (a[i] == key) { - pos = i; - break; +#include + +/* +Input : integer array indexed from 0, key to be searched +Output : Position of the key in the array if found, else -1 +*/ + +int linearSearch(int arr[], int size, int key) +{ + for (int i = 0; i < size; ++i) + { + if (arr[i] == key) + { + return i; // key found } } - return pos; - } + return -1; // key not found +} + +int main() +{ + int arr[] = {8, 12, 3, 4, 7, 2, 13}; + int size = sizeof(arr) / sizeof(arr[0]); - int main() { - int a[] = {8, 12, 3, 4, 7, 2, 13}; - int n = sizeof(a) / sizeof(a[0]); - int pos = linearSearch(a, n, 13); - if (pos != 1) - printf("Key found at position : %d \n", pos); + int pos = linearSearch(arr, size, 12); + + if (pos != -1) + printf("Key found at position : %d\n", pos); else - printf("Key not found \n"); - return 0; - } + printf("Key not found\n"); - /* - Output : - Key found at position : 2 - */ + return 0; +} From 2be5c73060a8ef279c9501c76a5b8d5e9aeb2dcc Mon Sep 17 00:00:00 2001 From: HARSH VERMA Date: Thu, 26 Feb 2026 00:30:37 +0530 Subject: [PATCH 06/11] Refactor rock-paper-scissors game implementation Refactor game logic and improve readability. --- .../c/rock_paper_scissor/rock_game.c | 126 ++++++++---------- 1 file changed, 56 insertions(+), 70 deletions(-) diff --git a/code/languages/c/rock_paper_scissor/rock_game.c b/code/languages/c/rock_paper_scissor/rock_game.c index 1edf7e3ee1..e6532dde48 100644 --- a/code/languages/c/rock_paper_scissor/rock_game.c +++ b/code/languages/c/rock_paper_scissor/rock_game.c @@ -1,101 +1,87 @@ -// Part of Cosmos by OpenGenus #include #include #include - -int generaterandomfunc(int n) +// Generate random choice: 0, 1, or 2 +int generateRandom(int n) { - srand(time(NULL)); return rand() % n; } -int greater(char char1, char char2) -{ - // return 1 if c1>c2 and 0 otherwise . if c1==c2 it will return -1 - if (char1 == char2) - { +// Returns: +// 1 -> first wins +// 0 -> second wins +// -1 -> draw +int compare(char p1, char p2) +{ + if (p1 == p2) return -1; - } - else if ((char1 == 'r') && (char2 == 's')) - { - return 1; - } - else if ((char2 == 'r' && char1 == 's')) - { - return 0; - } - else if ((char1 == 'p') && (char2 == 'r')) - { + if ((p1 == 'r' && p2 == 's') || + (p1 == 'p' && p2 == 'r') || + (p1 == 's' && p2 == 'p')) return 1; - } - else if ((char2 == 'p') && (char1 == 'r')) - { - return 0; - } - else if ((char1 == 's') && (char2 == 'p')) - { - return 1; - } - else if ((char2 == 's') && (char1 == 'p')) - { - return 0; - } + return 0; } int main() { - int playerscore = 0, compscore = 0, temp; - char playerchar, compchar; - char dict[45] = {'r', 'p', 's'}; - printf("welcome to the rock paper scissor game\n"); + int playerScore = 0, cpuScore = 0; + int choice; + char playerChar, cpuChar; + char options[] = {'r', 'p', 's'}; - for (int i = 0; i < 3; i++) + srand(time(NULL)); // seed ONCE + + printf("Welcome to Rock Paper Scissors Game 🎮\n"); + for (int i = 0; i < 3; i++) { - printf("choose 1 for rock choose 2 for paper choose 3 for scissor \n"); - printf("player no.1 turn \n"); - scanf("%d", &temp); - playerchar = dict[temp - 1]; - printf("you choose %c\n", playerchar); - - printf("choose 1 for rock choose 2 for paper choose 3 for scissor \n"); - printf("player no.1 turn \n"); - temp = generaterandomfunc(3) + 1; - compchar = dict[temp - 1]; - printf("cpu choose %c\n", compchar); - if (greater(compchar, playerchar) == 1) //compchar is greater than player char + printf("\nRound %d\n", i + 1); + printf("Choose:\n1. Rock\n2. Paper\n3. Scissors\n"); + scanf("%d", &choice); + + if (choice < 1 || choice > 3) { - compscore += 1; - printf("cpu got it hurray\n"); + printf("Invalid choice! Try again.\n"); + i--; + continue; } - else if (greater(compchar, playerchar) == -1) + + playerChar = options[choice - 1]; + cpuChar = options[generateRandom(3)]; + + printf("You chose: %c\n", playerChar); + printf("CPU chose: %c\n", cpuChar); + + int result = compare(playerChar, cpuChar); + + if (result == 1) { - compscore += 1; - playerscore += 1; - printf("its a draw \n"); + printf("You win this round! 🎉\n"); + playerScore++; + } + else if (result == 0) + { + printf("CPU wins this round 🤖\n"); + cpuScore++; } else { - playerscore += 1; - printf("you got it hurray\n"); + printf("It's a draw 🤝\n"); } - printf("you : %d \ncpu : %d \n", playerscore, compscore); - } - if (playerscore > compscore) - { - printf("you win \n"); - } - else if (compscore > playerscore) - { - printf("cpu win \n"); + printf("Score → You: %d | CPU: %d\n", playerScore, cpuScore); } + + printf("\nFinal Result:\n"); + if (playerScore > cpuScore) + printf("You won the game 🏆\n"); + else if (cpuScore > playerScore) + printf("CPU won the game 🤖\n"); else - { - printf("match is draw"); - } + printf("Game is a draw 🤝\n"); + return 0; } From ce9a6f1cca6113bb4d2d10bcac03a89c65f11a4e Mon Sep 17 00:00:00 2001 From: HARSH VERMA Date: Thu, 5 Mar 2026 22:00:46 +0530 Subject: [PATCH 07/11] Refactor spiral matrix generation for clarity --- .../src/2d_array/SpiralMatrix.cpp | 55 +++++++++++++++---- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/code/data_structures/src/2d_array/SpiralMatrix.cpp b/code/data_structures/src/2d_array/SpiralMatrix.cpp index bbecfaad26..abf2ca132a 100644 --- a/code/data_structures/src/2d_array/SpiralMatrix.cpp +++ b/code/data_structures/src/2d_array/SpiralMatrix.cpp @@ -2,50 +2,81 @@ #include using namespace std; +// Function to generate an n x n spiral matrix vector> generateSpiralMatrix(int n) { + + // Create an empty n x n matrix filled with 0 vector> matrix(n, vector(n)); - int left = 0; - int right = n - 1; - int top = 0; - int bottom = n - 1; + // These variables represent the current boundaries + int left = 0; // left column boundary + int right = n - 1; // right column boundary + int top = 0; // top row boundary + int bottom = n - 1; // bottom row boundary + // Direction indicator + // 0 -> left to right + // 1 -> top to bottom + // 2 -> right to left + // 3 -> bottom to top int direction = 0; + + // Value that will be filled inside the matrix int value = 1; + // Continue while the boundaries are valid while (left <= right && top <= bottom) { + + // Move from LEFT to RIGHT across the top row if (direction == 0) { for (int i = left; i <= right; i++) { - matrix[top][i] = value++; + matrix[top][i] = value++; // fill value then increment } - top++; - } else if (direction == 1) { + top++; // move the top boundary downward + } + + // Move from TOP to BOTTOM down the right column + else if (direction == 1) { for (int j = top; j <= bottom; j++) { matrix[j][right] = value++; } - right--; - } else if (direction == 2) { + right--; // move the right boundary left + } + + // Move from RIGHT to LEFT across the bottom row + else if (direction == 2) { for (int i = right; i >= left; i--) { matrix[bottom][i] = value++; } - bottom--; - } else { + bottom--; // move the bottom boundary upward + } + + // Move from BOTTOM to TOP up the left column + else { for (int j = bottom; j >= top; j--) { matrix[j][left] = value++; } - left++; + left++; // move the left boundary right } + + // Change direction (cycle through 0 → 1 → 2 → 3 → 0) direction = (direction + 1) % 4; } + + // Return the completed spiral matrix return matrix; } int main() { int n; + + // Take matrix size as input cin >> n; + // Generate the spiral matrix vector> spiralMatrix = generateSpiralMatrix(n); + // Print the matrix for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << spiralMatrix[i][j] << " "; From 617b4db229c4ddc5f03e22a0d236a3414a173a16 Mon Sep 17 00:00:00 2001 From: HARSH VERMA Date: Thu, 5 Mar 2026 22:05:44 +0530 Subject: [PATCH 08/11] Refactor largest element finder in C++ --- .../Largest_element.cpp | 46 ++++++++++++++----- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/code/languages/cpp/largest-element-in-an-array/Largest_element.cpp b/code/languages/cpp/largest-element-in-an-array/Largest_element.cpp index d0207a4500..afd0227fa4 100644 --- a/code/languages/cpp/largest-element-in-an-array/Largest_element.cpp +++ b/code/languages/cpp/largest-element-in-an-array/Largest_element.cpp @@ -1,25 +1,49 @@ #include using namespace std; - + +// Function to find the largest element in the array int findlargestelement(int arr[], int n){ - int largest = arr[0]; - for(int i=0; i>n; + + // Ask user for the size of the array + cout << "Enter the size of array: "; + cin >> n; + + // Declare an array of size n int arr[n]; - cout<<"Enter array elements: "; - for(int i=0; i>arr[i]; + + // Take array elements as input from the user + cout << "Enter array elements: "; + for(int i = 0; i < n; i++){ + cin >> arr[i]; } + + // Call the function to find the largest element int largest = findlargestelement(arr, n); - cout<<"largest Element is: "< Date: Fri, 6 Mar 2026 23:57:07 +0530 Subject: [PATCH 09/11] Refactor countX function and update driver code --- code/languages/python/tuple/example.py | 28 +++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/code/languages/python/tuple/example.py b/code/languages/python/tuple/example.py index 05139c0a9e..25e377420b 100644 --- a/code/languages/python/tuple/example.py +++ b/code/languages/python/tuple/example.py @@ -1,17 +1,27 @@ +# Function to count how many times a value x appears in a tuple def countX(tup, x): - count = 0 + count = 0 # Initialize a counter variable to 0 + + # Loop through each element in the tuple for ele in tup: + # Check if the current element is equal to x if (ele == x): - count = count + 1 - return count - -# Driver Code + count = count + 1 # If yes, increase the counter by 1 + + return count # Return the final count + + +# Driver Code (main part of the program) + +# Tuple containing numbers tup = (10, 8, 5, 2, 10, 15, 10, 8, 5, 8, 8, 2) + +# Values whose frequency we want to find enq = 4 enq1 = 10 enq2 = 8 -print(countX(tup, enq)) -print(countX(tup, enq1)) -print(countX(tup, enq2)) - +# Calling the function and printing results +print(countX(tup, enq)) # Count how many times 4 appears +print(countX(tup, enq1)) # Count how many times 10 appears +print(countX(tup, enq2)) # Count how many times 8 appears From 458b31bcbf4b3e824b6d88271c42f106dc9ef377 Mon Sep 17 00:00:00 2001 From: HARSH VERMA Date: Fri, 6 Mar 2026 23:58:39 +0530 Subject: [PATCH 10/11] Enhance code documentation with comments Added comments for clarity and understanding of the code. --- .../python/2d-array-numpy/2d-array-numpy.py | 101 ++++++++++++------ 1 file changed, 68 insertions(+), 33 deletions(-) diff --git a/code/languages/python/2d-array-numpy/2d-array-numpy.py b/code/languages/python/2d-array-numpy/2d-array-numpy.py index d947d254d9..06bbb3c870 100644 --- a/code/languages/python/2d-array-numpy/2d-array-numpy.py +++ b/code/languages/python/2d-array-numpy/2d-array-numpy.py @@ -1,68 +1,103 @@ -import numpy as np +import numpy as np # Importing NumPy library for numerical operations -# 1D array -arr = np.array([2,4,6],dtype='int32') -print(arr) +# Creating a 1D array +arr = np.array([2,4,6], dtype='int32') # Define a NumPy array with datatype int32 +print(arr) # Print the 1D array -# 2D array -arr = np.array([[1,2,3],[4,5,6]]) -print(arr) -print(arr.shape) -print(arr.dtype) +# Creating a 2D array +arr = np.array([[1,2,3],[4,5,6]]) # 2 rows and 3 columns matrix +print(arr) # Print the 2D array -print(arr[1,1]) -print(arr[1,:]) -arr = np.ones((4,4)) -t = arr[1:3,1:3] +# Printing properties of the array +print(arr.shape) # Shows dimensions of the array (rows, columns) +print(arr.dtype) # Shows the datatype of elements in the array + + +# Accessing elements +print(arr[1,1]) # Access element at row index 1, column index 1 +print(arr[1,:]) # Access entire second row + + +# Creating an array filled with ones +arr = np.ones((4,4)) # Create a 4x4 matrix of ones + +# Slicing the array +t = arr[1:3,1:3] # Extract a 2x2 sub-matrix from rows 1-2 and columns 1-2 print(t) -arr_zeros = np.zeros((3,5)) + +# Creating an array filled with zeros +arr_zeros = np.zeros((3,5)) # 3 rows and 5 columns of zeros print(arr_zeros) -arr_ones = 2*np.ones((3,5)) + +# Creating an array filled with 2s +arr_ones = 2 * np.ones((3,5)) # Multiply ones matrix by 2 print(arr_ones) -arr_rand = np.random.rand(3,4) + +# Creating an array with random numbers +arr_rand = np.random.rand(3,4) # Random values between 0 and 1 print(arr_rand) -arr_i = np.identity(3) + +# Creating an identity matrix +arr_i = np.identity(3) # 3x3 identity matrix print(arr_i) + +# Creating a matrix for arithmetic operations a = np.array([[1,2,3],[4,6,2],[0,7,1]]) -print(a+2) -print(a-4) -print(a*3) -print(a/2) -print(a**2) +print(a+2) # Add 2 to every element +print(a-4) # Subtract 4 from every element +print(a*3) # Multiply each element by 3 +print(a/2) # Divide each element by 2 +print(a**2) # Square each element + + +# Matrix operations a = np.array([[1,2,3],[4,6,2],[0,7,1]]) b = np.array([[3,6],[1,4],[7,2]]) c = np.array([[1,0,3],[2,3,1],[0,0,1]]) -add = a+c -mul = np.matmul(a,b) + +add = a + c # Matrix addition +mul = np.matmul(a,b) # Matrix multiplication print(add) print(mul) -print(np.min(b)) -print(np.max(b)) -print(np.linalg.det(a)) -print(np.sum(b,axis=0)) -print(np.sum(b,axis=1)) +# Finding minimum, maximum and determinant +print(np.min(b)) # Smallest element in matrix b +print(np.max(b)) # Largest element in matrix b +print(np.linalg.det(a)) # Determinant of matrix a + + +# Sum operations along axes +print(np.sum(b,axis=0)) # Sum of each column +print(np.sum(b,axis=1)) # Sum of each row -before = np.array([[1,2,3,4],[5,6,7,8]]) -after = before.reshape(4,2) + +# Reshaping arrays +before = np.array([[1,2,3,4],[5,6,7,8]]) # 2x4 matrix +after = before.reshape(4,2) # Convert to 4x2 matrix print(after) + +# Horizontal stacking (joining matrices side by side) a = np.identity(2) b = np.array([[1,2],[2,1]]) -hstack = np.hstack((a,b)) + +hstack = np.hstack((a,b)) # Combine matrices horizontally print(hstack) + +# Vertical stacking (joining matrices one above another) a = np.identity(2) b = np.array([[1,2],[2,1]]) -vstack = np.vstack((a,b)) + +vstack = np.vstack((a,b)) # Combine matrices vertically print(vstack) From 4b71725fd51c31965e9d84cbcf3702f8e880154a Mon Sep 17 00:00:00 2001 From: HARSH VERMA Date: Sun, 15 Mar 2026 22:31:58 +0530 Subject: [PATCH 11/11] Enhance DelannoyGenerator function with comments Added comments to explain the Delannoy number generation process and clarified the base and recursive cases. --- .../src/delannoy_number/delannoy_number.py | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/code/mathematical_algorithms/src/delannoy_number/delannoy_number.py b/code/mathematical_algorithms/src/delannoy_number/delannoy_number.py index 8b27e3c995..a7b221ee6f 100644 --- a/code/mathematical_algorithms/src/delannoy_number/delannoy_number.py +++ b/code/mathematical_algorithms/src/delannoy_number/delannoy_number.py @@ -1,12 +1,31 @@ -def DelannoyGenerator(n,m): - if n==0 or m==0: - d = 1 - else: - d = DelannoyGenerator(n-1,m) + DelannoyGenerator(n,m-1) + DelannoyGenerator(n-1,m-1) +# Function to generate the Delannoy number for coordinates (n, m) +def DelannoyGenerator(n, m): + + # Base Case: + # If either n or m is 0, there is only one path to reach that point + # because we can only move in one direction (either horizontal or vertical) + if n == 0 or m == 0: + d = 1 + + else: + # Recursive Case: + # The Delannoy number is calculated as the sum of: + # 1. Paths coming from (n-1, m) -> move vertically + # 2. Paths coming from (n, m-1) -> move horizontally + # 3. Paths coming from (n-1, m-1) -> move diagonally + d = ( + DelannoyGenerator(n - 1, m) + + DelannoyGenerator(n, m - 1) + + DelannoyGenerator(n - 1, m - 1) + ) - return d + # Return the calculated Delannoy number + return d + +# Take input from the user for coordinates n and m n = int(input("Provide the 'n' value: ")) m = int(input("Provide the 'm' value: ")) -print(f"The delannoy number is: {DelannoyGenerator(n,m)}") \ No newline at end of file +# Print the computed Delannoy number +print(f"The delannoy number is: {DelannoyGenerator(n, m)}")