From e3f2f86158b45b74cb713ddea728328b7e793005 Mon Sep 17 00:00:00 2001 From: anoushka Date: Fri, 31 May 2024 21:48:16 +0300 Subject: [PATCH 1/3] 2D arrays blog post --- docs/dsa/arrays/2d-arrays.md | 201 +++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 docs/dsa/arrays/2d-arrays.md diff --git a/docs/dsa/arrays/2d-arrays.md b/docs/dsa/arrays/2d-arrays.md new file mode 100644 index 000000000..dfd07acfa --- /dev/null +++ b/docs/dsa/arrays/2d-arrays.md @@ -0,0 +1,201 @@ +--- +id: two-dimensional-arrays-DSA +title: Two-Dimensional Arrays +sidebar_label: Two-Dimensional Arrays +sidebar_position: 5 +description: "In this blog post, we'll delve into the world of two-dimensional arrays, a vital data structure in programming. You'll learn what 2D arrays are, how to initialize and traverse them, and their common uses in real-world applications like matrix operations, image processing, and game boards. We'll also tackle classic algorithmic challenges involving 2D arrays, such as rotating a matrix and finding the largest sum subgrid. By the end, you'll have a solid understanding of how to effectively use 2D arrays to solve complex problems in your programming projects." +tags: [dsa, arrays, 2d arrays] +--- + +Welcome, curious coder! Today, we embark on an exciting journey through the world of two-dimensional arrays. These versatile data structures are the backbone of numerous algorithmic problems and are a staple in the toolkit of any proficient programmer. Whether you're working on games, simulations, or complex data analysis, understanding 2D arrays is crucial. Let's dive in and explore their structure, uses, and how to manipulate them efficiently. + +## What is a Two-Dimensional Array? + +Imagine a spreadsheet, a grid of rows and columns where each cell can hold a piece of data. This is essentially what a two-dimensional (2D) array is: a collection of data organized in a tabular format. Instead of having a single index like a one-dimensional array, a 2D array uses two indices to access its elements, typically represented as `array[row][column]`. + +In pseudo-code, we define a 2D array as follows: + +``` +DECLARE array[rowCount][columnCount] +``` + +In C++, this can be represented as: + +```cpp +int array[rowCount][columnCount]; +``` + +## Initializing a 2D Array + +Initialization of a 2D array can be done in multiple ways. Here’s a simple example in pseudo-code: + +``` +DECLARE array[3][3] +array[0][0] = 1 +array[0][1] = 2 +array[0][2] = 3 +array[1][0] = 4 +array[1][1] = 5 +array[1][2] = 6 +array[2][0] = 7 +array[2][1] = 8 +array[2][2] = 9 +``` + +And here’s how it looks in C++: + +```cpp +int array[3][3] = { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9} +}; +``` + +## Traversing a 2D Array + +One of the fundamental operations is traversing a 2D array. This is often done using nested loops. Here’s a pseudo-code example to print all elements of a 3x3 array: + +``` +FOR row FROM 0 TO 2 + FOR column FROM 0 TO 2 + PRINT array[row][column] +``` + +And in C++: + +```cpp +for (int row = 0; row < 3; ++row) { + for (int column = 0; column < 3; ++column) { + std::cout << array[row][column] << " "; + } + std::cout << std::endl; +} +``` + +## Common Uses of 2D Arrays + +Two-dimensional arrays shine in various algorithmic problems and real-world applications. Here are a few common scenarios where 2D arrays are indispensable: + +### 1. Matrix Operations + +Matrices are a classic use case for 2D arrays. Operations like addition, subtraction, and multiplication of matrices are performed using nested loops to iterate over the elements. + +Matrix Addition Pseudo-Code: + +``` +DECLARE matrixA[rowCount][columnCount] +DECLARE matrixB[rowCount][columnCount] +DECLARE matrixSum[rowCount][columnCount] + +FOR row FROM 0 TO rowCount - 1 + FOR column FROM 0 TO columnCount - 1 + matrixSum[row][column] = matrixA[row][column] + matrixB[row][column] +``` + +Matrix Addition in C++: + +```cpp +int matrixA[2][2] = {{1, 2}, {3, 4}}; +int matrixB[2][2] = {{5, 6}, {7, 8}}; +int matrixSum[2][2]; + +for (int row = 0; row < 2; ++row) { + for (int column = 0; column < 2; ++column) { + matrixSum[row][column] = matrixA[row][column] + matrixB[row][column]; + } +} +``` + +### 2. Image Processing + +Images can be represented as 2D arrays where each element corresponds to a pixel value. Manipulating images, applying filters, or detecting edges often involves traversing and modifying 2D arrays. + +### 3. Game Boards + +Games like chess, tic-tac-toe, and Sudoku use 2D arrays to represent the game board. Each element in the array represents a cell on the board, storing the state of the game. + +Example: Tic-Tac-Toe Board in Pseudo-Code + +``` +DECLARE board[3][3] +board[0][0] = 'X' +board[0][1] = 'O' +board[0][2] = 'X' +board[1][0] = 'O' +board[1][1] = 'X' +board[1][2] = 'O' +board[2][0] = 'X' +board[2][1] = 'X' +board[2][2] = 'O' +``` + +## Algorithmic Challenges Involving 2D Arrays + +2D arrays often appear in algorithmic challenges and coding interviews. Here are a couple of classic problems: + +### 1. Rotating a Matrix + +Given an `N x N` matrix, rotate it 90 degrees clockwise. This involves first transposing the matrix and then reversing the rows. + +Rotation Pseudo-Code: + +``` +DECLARE matrix[N][N] + +// Transpose the matrix +FOR row FROM 0 TO N - 1 + FOR column FROM row TO N - 1 + SWAP matrix[row][column] WITH matrix[column][row] + +// Reverse each row +FOR row FROM 0 TO N - 1 + REVERSE matrix[row] +``` + +Rotation in C++: + +```cpp +int matrix[N][N]; // Assume this is initialized + +// Transpose the matrix +for (int row = 0; row < N; ++row) { + for (int column = row; column < N; ++column) { + std::swap(matrix[row][column], matrix[column][row]); + } +} + +// Reverse each row +for (int row = 0; row < N; ++row) { + std::reverse(matrix[row], matrix[row] + N); +} +``` + +### 2. Finding the Largest Sum Subgrid + +Given a `M x N` matrix, find the subgrid with the largest sum. This problem can be solved using dynamic programming and is a 2D extension of the 1D maximum subarray problem (Kadane's algorithm). + +Largest Sum Subgrid Pseudo-Code: + +``` +DECLARE maxSum = -INFINITY +FOR startRow FROM 0 TO M - 1 + DECLARE temp[columnCount] + FOR endRow FROM startRow TO M - 1 + FOR column FROM 0 TO N - 1 + temp[column] = temp[column] + matrix[endRow][column] + DECLARE currentMax = Kadane(temp) + maxSum = MAX(maxSum, currentMax) +``` + +## Tips for Working with 2D Arrays + +1. **Boundary Checks**: Always ensure your indices are within the bounds of the array to avoid runtime errors. +2. **Memory Considerations**: Be mindful of the memory usage, especially for large 2D arrays, as they can consume a significant amount of memory. +3. **Initialization**: Properly initialize your arrays to avoid unexpected behavior due to garbage values. + +## In Conclusion + +Two-dimensional arrays are a fundamental data structure that every programmer should master. They are used in a wide variety of applications, from simple data storage to complex algorithmic challenges. By understanding how to manipulate and use 2D arrays effectively, you'll be well-equipped to tackle a broad range of programming problems.
+ +So, next time you encounter a grid-based problem or need to perform operations on tabular data, you'll have the confidence and knowledge to use 2D arrays to their full potential. Happy coding! \ No newline at end of file From 7111165b8a8bef86fb28c33a74ff071038e3d988 Mon Sep 17 00:00:00 2001 From: anoushka <153769363+iamanolive@users.noreply.github.com> Date: Fri, 31 May 2024 23:58:17 +0300 Subject: [PATCH 2/3] introduction to tensorflow --- tensorflow-tutorials/introduction.ipynb | 326 ++++++++++++++++++++++++ 1 file changed, 326 insertions(+) create mode 100644 tensorflow-tutorials/introduction.ipynb diff --git a/tensorflow-tutorials/introduction.ipynb b/tensorflow-tutorials/introduction.ipynb new file mode 100644 index 000000000..a3960ae5e --- /dev/null +++ b/tensorflow-tutorials/introduction.ipynb @@ -0,0 +1,326 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [], + "authorship_tag": "ABX9TyO0fxb6omsPqcaelthdhOlo", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "source": [ + "import tensorflow as tf" + ], + "metadata": { + "id": "A2iC7nlsQ_tU" + }, + "execution_count": 8, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# creating tensors" + ], + "metadata": { + "id": "d3SLZ6TiR4Zw" + } + }, + { + "cell_type": "code", + "source": [ + "string = tf.Variable(\"this is a string\", tf.string)\n", + "number = tf.Variable(324, tf.int16)\n", + "floating = tf.Variable(3.567, tf.float64)" + ], + "metadata": { + "id": "nW0NfMdPVmDo" + }, + "execution_count": 9, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# ranks or degrees of tensors" + ], + "metadata": { + "id": "njzcqxqsWDpc" + } + }, + { + "cell_type": "code", + "source": [ + "rank1_tensor = tf.Variable([\"Test\", \"Ok\", \"Somebody\", \"Somebody\"], tf.string)\n", + "rank2_tensor = tf.Variable([[\"test\", \"ok\"], [\"test\", \"yes\"], [\"ok\", \"ok\"]], tf.string)" + ], + "metadata": { + "id": "jY_yrAmUVxmp" + }, + "execution_count": 27, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "tf.rank(rank1_tensor)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "JKhqMx7iWe7x", + "outputId": "7f11b444-016e-4654-8436-6340e612def7" + }, + "execution_count": 28, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": {}, + "execution_count": 28 + } + ] + }, + { + "cell_type": "code", + "source": [ + "tf.rank(rank2_tensor)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "sw5Ns_2YWsEo", + "outputId": "e96a3e35-d3b6-4056-e327-ca08c35c2795" + }, + "execution_count": 29, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": {}, + "execution_count": 29 + } + ] + }, + { + "cell_type": "code", + "source": [ + "tf.rank(number)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "4qSdqk-aXRO1", + "outputId": "41573023-fe3c-44a9-8f15-4e119d58fec3" + }, + "execution_count": 30, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": {}, + "execution_count": 30 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "# shapes of tensors" + ], + "metadata": { + "id": "JSh-sGGtXFbd" + } + }, + { + "cell_type": "code", + "source": [ + "rank1_tensor.shape" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "lKeKVZ0DXXXo", + "outputId": "9bdef519-f5b6-42fd-af37-93f0d86f51f8" + }, + "execution_count": 31, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "TensorShape([4])" + ] + }, + "metadata": {}, + "execution_count": 31 + } + ] + }, + { + "cell_type": "code", + "source": [ + "rank2_tensor.shape" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Nq55Y62_XH2C", + "outputId": "d662fd43-8d3a-4ad5-e894-67e3e5d34557" + }, + "execution_count": 32, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "TensorShape([3, 2])" + ] + }, + "metadata": {}, + "execution_count": 32 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "# changing shapes" + ], + "metadata": { + "id": "fZq-ciAuX9nd" + } + }, + { + "cell_type": "code", + "source": [ + "tensor1 = tf.ones([1, 2, 3])\n", + "tensor2 = tf.reshape(tensor1, [2, 3, 1])\n", + "tensor3 = tf.reshape(tensor2, [3, -1])" + ], + "metadata": { + "id": "6B220CYWYA-r" + }, + "execution_count": 37, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "print(tensor1)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "yk7p22RyYS2I", + "outputId": "c5d3f951-7ba4-4f10-f76f-508d7efadbff" + }, + "execution_count": 38, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "tf.Tensor(\n", + "[[[1. 1. 1.]\n", + " [1. 1. 1.]]], shape=(1, 2, 3), dtype=float32)\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "print(tensor2)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "121lHMJYYVG3", + "outputId": "0b1d5684-ce45-4f6d-a108-1a54ba008f6d" + }, + "execution_count": 39, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "tf.Tensor(\n", + "[[[1.]\n", + " [1.]\n", + " [1.]]\n", + "\n", + " [[1.]\n", + " [1.]\n", + " [1.]]], shape=(2, 3, 1), dtype=float32)\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "print(tensor3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "UnbSuXgxYWiw", + "outputId": "9c1f3139-6863-47a9-f95b-067fc158a914" + }, + "execution_count": 44, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "tf.Tensor(\n", + "[[1. 1.]\n", + " [1. 1.]\n", + " [1. 1.]], shape=(3, 2), dtype=float32)\n" + ] + } + ] + } + ] +} \ No newline at end of file From 1e141aa660170062ff522133a425d4a6fe9cffab Mon Sep 17 00:00:00 2001 From: anoushka <153769363+iamanolive@users.noreply.github.com> Date: Fri, 31 May 2024 23:58:42 +0300 Subject: [PATCH 3/3] delete files --- tensorflow-tutorials/introduction.ipynb | 326 ------------------------ 1 file changed, 326 deletions(-) delete mode 100644 tensorflow-tutorials/introduction.ipynb diff --git a/tensorflow-tutorials/introduction.ipynb b/tensorflow-tutorials/introduction.ipynb deleted file mode 100644 index a3960ae5e..000000000 --- a/tensorflow-tutorials/introduction.ipynb +++ /dev/null @@ -1,326 +0,0 @@ -{ - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "provenance": [], - "authorship_tag": "ABX9TyO0fxb6omsPqcaelthdhOlo", - "include_colab_link": true - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - } - }, - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "view-in-github", - "colab_type": "text" - }, - "source": [ - "\"Open" - ] - }, - { - "cell_type": "code", - "source": [ - "import tensorflow as tf" - ], - "metadata": { - "id": "A2iC7nlsQ_tU" - }, - "execution_count": 8, - "outputs": [] - }, - { - "cell_type": "markdown", - "source": [ - "# creating tensors" - ], - "metadata": { - "id": "d3SLZ6TiR4Zw" - } - }, - { - "cell_type": "code", - "source": [ - "string = tf.Variable(\"this is a string\", tf.string)\n", - "number = tf.Variable(324, tf.int16)\n", - "floating = tf.Variable(3.567, tf.float64)" - ], - "metadata": { - "id": "nW0NfMdPVmDo" - }, - "execution_count": 9, - "outputs": [] - }, - { - "cell_type": "markdown", - "source": [ - "# ranks or degrees of tensors" - ], - "metadata": { - "id": "njzcqxqsWDpc" - } - }, - { - "cell_type": "code", - "source": [ - "rank1_tensor = tf.Variable([\"Test\", \"Ok\", \"Somebody\", \"Somebody\"], tf.string)\n", - "rank2_tensor = tf.Variable([[\"test\", \"ok\"], [\"test\", \"yes\"], [\"ok\", \"ok\"]], tf.string)" - ], - "metadata": { - "id": "jY_yrAmUVxmp" - }, - "execution_count": 27, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "tf.rank(rank1_tensor)" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "JKhqMx7iWe7x", - "outputId": "7f11b444-016e-4654-8436-6340e612def7" - }, - "execution_count": 28, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "" - ] - }, - "metadata": {}, - "execution_count": 28 - } - ] - }, - { - "cell_type": "code", - "source": [ - "tf.rank(rank2_tensor)" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "sw5Ns_2YWsEo", - "outputId": "e96a3e35-d3b6-4056-e327-ca08c35c2795" - }, - "execution_count": 29, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "" - ] - }, - "metadata": {}, - "execution_count": 29 - } - ] - }, - { - "cell_type": "code", - "source": [ - "tf.rank(number)" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "4qSdqk-aXRO1", - "outputId": "41573023-fe3c-44a9-8f15-4e119d58fec3" - }, - "execution_count": 30, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "" - ] - }, - "metadata": {}, - "execution_count": 30 - } - ] - }, - { - "cell_type": "markdown", - "source": [ - "# shapes of tensors" - ], - "metadata": { - "id": "JSh-sGGtXFbd" - } - }, - { - "cell_type": "code", - "source": [ - "rank1_tensor.shape" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "lKeKVZ0DXXXo", - "outputId": "9bdef519-f5b6-42fd-af37-93f0d86f51f8" - }, - "execution_count": 31, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "TensorShape([4])" - ] - }, - "metadata": {}, - "execution_count": 31 - } - ] - }, - { - "cell_type": "code", - "source": [ - "rank2_tensor.shape" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "Nq55Y62_XH2C", - "outputId": "d662fd43-8d3a-4ad5-e894-67e3e5d34557" - }, - "execution_count": 32, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "TensorShape([3, 2])" - ] - }, - "metadata": {}, - "execution_count": 32 - } - ] - }, - { - "cell_type": "markdown", - "source": [ - "# changing shapes" - ], - "metadata": { - "id": "fZq-ciAuX9nd" - } - }, - { - "cell_type": "code", - "source": [ - "tensor1 = tf.ones([1, 2, 3])\n", - "tensor2 = tf.reshape(tensor1, [2, 3, 1])\n", - "tensor3 = tf.reshape(tensor2, [3, -1])" - ], - "metadata": { - "id": "6B220CYWYA-r" - }, - "execution_count": 37, - "outputs": [] - }, - { - "cell_type": "code", - "source": [ - "print(tensor1)" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "yk7p22RyYS2I", - "outputId": "c5d3f951-7ba4-4f10-f76f-508d7efadbff" - }, - "execution_count": 38, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "tf.Tensor(\n", - "[[[1. 1. 1.]\n", - " [1. 1. 1.]]], shape=(1, 2, 3), dtype=float32)\n" - ] - } - ] - }, - { - "cell_type": "code", - "source": [ - "print(tensor2)" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "121lHMJYYVG3", - "outputId": "0b1d5684-ce45-4f6d-a108-1a54ba008f6d" - }, - "execution_count": 39, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "tf.Tensor(\n", - "[[[1.]\n", - " [1.]\n", - " [1.]]\n", - "\n", - " [[1.]\n", - " [1.]\n", - " [1.]]], shape=(2, 3, 1), dtype=float32)\n" - ] - } - ] - }, - { - "cell_type": "code", - "source": [ - "print(tensor3)" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "UnbSuXgxYWiw", - "outputId": "9c1f3139-6863-47a9-f95b-067fc158a914" - }, - "execution_count": 44, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "tf.Tensor(\n", - "[[1. 1.]\n", - " [1. 1.]\n", - " [1. 1.]], shape=(3, 2), dtype=float32)\n" - ] - } - ] - } - ] -} \ No newline at end of file