From e8f2b0f61c7a34485ac230be8df30eb607c5b649 Mon Sep 17 00:00:00 2001 From: Debanjan110d Date: Sun, 16 Feb 2025 23:01:50 +0530 Subject: [PATCH 1/2] Add N-Dimensional Array Creator snippet --- .../python/[numpy}/n_dimensional_array.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 snippets/python/[numpy}/n_dimensional_array.md diff --git a/snippets/python/[numpy}/n_dimensional_array.md b/snippets/python/[numpy}/n_dimensional_array.md new file mode 100644 index 00000000..38fdd6a6 --- /dev/null +++ b/snippets/python/[numpy}/n_dimensional_array.md @@ -0,0 +1,20 @@ +--- +title: N-Dimensional Array Creator +description: Creates an N-dimensional array filled with a single element. +author: Debanjan110d +tags: numpy, arrays, python, n-dimensional +--- + +```py +import numpy as np + +n = int(input("Enter the number of dimensions: ")) +n_dim_array = np.array([1]) +n_dim_array = n_dim_array.reshape(*([1] * n)) + +print(f"Created an {n}-dimensional array.") +print(f"Number of dimensions: {n_dim_array.ndim}") + +# Usage: +# -> Run the script and enter the number of dimensions. +# -> It will create an N-dimensional array and display its number of dimensions. From d6230284af6fb6d99e78b06967e9483fce347658 Mon Sep 17 00:00:00 2001 From: Debanjan110d Date: Mon, 17 Feb 2025 19:06:48 +0530 Subject: [PATCH 2/2] Update N-Dimensional Array Creator snippet to match guidelines --- snippets/python/[numpy}/n_dimensional_array.md | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/snippets/python/[numpy}/n_dimensional_array.md b/snippets/python/[numpy}/n_dimensional_array.md index 38fdd6a6..7e5c1759 100644 --- a/snippets/python/[numpy}/n_dimensional_array.md +++ b/snippets/python/[numpy}/n_dimensional_array.md @@ -8,13 +8,11 @@ tags: numpy, arrays, python, n-dimensional ```py import numpy as np -n = int(input("Enter the number of dimensions: ")) -n_dim_array = np.array([1]) -n_dim_array = n_dim_array.reshape(*([1] * n)) +def create_n_dimensional_array(n, fill_value=1): + """Creates an N-dimensional NumPy array filled with a given value.""" + return np.full([1] * n, fill_value) -print(f"Created an {n}-dimensional array.") -print(f"Number of dimensions: {n_dim_array.ndim}") - -# Usage: -# -> Run the script and enter the number of dimensions. -# -> It will create an N-dimensional array and display its number of dimensions. +# Example usage: +# arr = create_n_dimensional_array(3) +# print(arr.ndim) # Output: 3 +``` \ No newline at end of file