<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Prakash's blog]]></title><description><![CDATA[About Me
Full stack developer with some good problem-solving skills.
The stack I use to develop websites is MERN (MongoDB, Express, React, NodeJs).]]></description><link>https://chandra-prakash-babu.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Mon, 31 Aug 2026 22:44:24 GMT</lastBuildDate><atom:link href="https://chandra-prakash-babu.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Building and Pushing Docker Images to AWS 
                                    ECR Seamless Integration.]]></title><description><![CDATA[Introduction

Containerization is transforming modern application development.

Amazon ECR is a vital AWS service for Docker image management.

ECR provides secure and private container image storage.

Seamless integration with AWS services like ECS ...]]></description><link>https://chandra-prakash-babu.hashnode.dev/building-and-pushing-docker-images-to-aws-ecr-seamless-integration</link><guid isPermaLink="true">https://chandra-prakash-babu.hashnode.dev/building-and-pushing-docker-images-to-aws-ecr-seamless-integration</guid><category><![CDATA[Docker]]></category><category><![CDATA[Docker compose]]></category><category><![CDATA[AWS ECR]]></category><category><![CDATA[docker images]]></category><dc:creator><![CDATA[PONDURI CHANDRA PRAKASH BABU]]></dc:creator><pubDate>Sun, 15 Oct 2023 12:07:29 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1697369738593/cb7c2174-ef44-4fe3-a0a7-6db646a17330.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-introduction"><strong>Introduction</strong></h3>
<ul>
<li><p>Containerization is transforming modern application development.</p>
</li>
<li><p>Amazon ECR is a vital AWS service for Docker image management.</p>
</li>
<li><p>ECR provides secure and private container image storage.</p>
</li>
<li><p>Seamless integration with AWS services like ECS and EKS.</p>
</li>
<li><p>ECR reduces data transfer costs when used within the same region.</p>
</li>
<li><p>ECR repositories are private for enhanced security.</p>
</li>
<li><p>Docker CLI compatibility simplifies image management.</p>
</li>
<li><p>Support for image versioning and lifecycle policies.</p>
</li>
<li><p>Global image replication enhances availability.</p>
</li>
<li><p>Image vulnerability scanning for enhanced security.</p>
</li>
</ul>
<h3 id="heading-setting-up-the-development-environment"><strong>Setting Up the Development Environment</strong></h3>
<p>Before we dive into building and pushing Docker images to AWS ECR, let's ensure our development environment is properly set up. Install Docker on your local machine and make sure you have the necessary AWS credentials and access to an AWS account with ECR enabled.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697370242187/b02051c3-f85c-4bc2-9f5f-b2733b59e4ef.png" alt class="image--center mx-auto" /></p>
<p>Before we can push the Docker image to AWS ECR, we need to authenticate ourselves. We'll cover the various authentication methods, such as using AWS CLI, Docker CLI, or IAM role-based authentication. Once authenticated, we'll use the <code>docker push</code> command to push the Docker image to our ECR repository.</p>
<ul>
<li>Ensure you have the necessary AWS CLI installed and configured on your local machine. You can refer to the AWS CLI documentation for instructions on installation and configuration.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697370103189/46d5093e-2699-47ab-ad94-76ab4bd78db0.png" alt class="image--center mx-auto" /></p>
<p>After Installing AWS CLI, give permission for <strong>AmazonEC2ContainerRegistryFullAccess</strong> policy to IAM users. After that create an access key and use the access key ID and secret access key for configuration.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697370088812/db1cfda6-1e6d-40b5-a098-8d186f3cbf4a.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697370096662/0260748b-b212-49d2-a3c6-97eb46fa00bc.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-create-a-repository-in-aws-ecr"><strong>Create a Repository in AWS ECR:</strong></h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697370548637/f1df0723-43a6-4e47-a3f4-65c1e5cef876.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-authenticating-and-pushing-docker-images-into-aws-ecr"><strong>Authenticating and Pushing docker images into AWS ECR</strong></h3>
<p>Open a terminal or command prompt and authenticate your Docker client with the ECR registry by running the following command:</p>
<pre><code class="lang-bash"> aws ecr get-login-password --region &lt;region&gt; | docker login --username AWS --password-stdin &lt;account-id&gt;.dkr.ecr.&lt;region&gt;.amazonaws.com
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697370076557/7200a1f7-4300-4fc6-bc6d-f7e61d1336bd.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Replace <code>&lt;region&gt;</code> with the AWS region where your ECR repository is located, and <code>&lt;account-id&gt;</code> with your AWS account ID. This command retrieves an authentication token and uses it to log in to the ECR registry.</p>
</li>
<li><p>Tag your Docker image with the ECR repository URI. Run the following command:</p>
<pre><code class="lang-bash">    docker tag &lt;local-image&gt; &lt;repository-uri&gt;
</code></pre>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697370070790/88b1758b-b296-4254-90b5-b7cffa3bf3a3.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Replace <code>&lt;local-image&gt;</code> with the name of your local Docker image, and <code>&lt;repository-uri&gt;</code> with the URI of your ECR repository. The repository URI follows the pattern <code>&lt;account-id&gt;.dkr.ecr.&lt;region&gt;.</code><a target="_blank" href="http://amazonaws.com/&lt;repository-name&gt;"><code>amazonaws.com/&lt;repository-name&gt;</code></a>. Ensure the repository name matches the ECR repository you want to push the image to.</p>
</li>
<li><p>Run the following command to push this image to your newly created AWS repository:</p>
<pre><code class="lang-bash">    docker push &lt;repository-uri&gt;
</code></pre>
</li>
</ul>
<p>After successfully pushing the docker image into AWS ECR you can see the image in the repository.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697371266783/e7a6a65e-0168-4363-8c37-ac0c270285f5.png" alt class="image--center mx-auto" /></p>
<p>By following these steps, you can authenticate your Docker client with AWS ECR and securely push your Docker image to the repository for seamless deployment in an AWS environment.</p>
<p>Remember to ensure that you have the necessary IAM permissions to interact with ECR and push images to the desired repository.</p>
<h3 id="heading-pulling-from-aws-ecr"><strong>Pulling from AWS ECR</strong></h3>
<p>You can pull the Docker image from the ECR repository using the following command:</p>
<pre><code class="lang-bash">docker pull &lt;repository-uri&gt;
</code></pre>
<p>Replace <code>&lt;repository-uri&gt;</code> with the URI of your ECR repository. The repository URI follows the format <code>&lt;account-id&gt;.dkr.ecr.&lt;region&gt;.</code><a target="_blank" href="http://amazonaws.com/&lt;repository-name&gt;"><code>amazonaws.com/&lt;repository-name&gt;</code></a>. Make sure to provide the correct repository name.</p>
<p>To verify that the image has been successfully pulled, you can run the <code>docker images</code> command, which lists all the images available on your local machine.</p>
<h3 id="heading-conclusion"><strong>Conclusion:</strong></h3>
<p>pushing Docker images to Amazon ECR is a strategic move in the containerization journey, offering a multitude of advantages.</p>
<p>ECR simplifies secure image management, seamlessly integrates with AWS services, reduces data transfer costs, and provides a strong foundation for robust, scalable, and secure container-based applications.</p>
<p>Embracing ECR ensures efficient image storage and management within the AWS ecosystem, contributing to the success of your containerized applications.</p>
<ul>
<li>Connect with me on LinkedIn: <a target="_blank" href="https://www.linkedin.com/in/prakash7078/">prakash7078</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Simplify Containerized Applications with 
                                                    Docker Compose]]></title><description><![CDATA[Introduction
Containerization has revolutionized application deployment, but managing multiple containers can be complex. Enter Docker Compose, a powerful tool for simplifying multi-container applications.
Docker Compose is a great approach when mult...]]></description><link>https://chandra-prakash-babu.hashnode.dev/simplify-containerized-applications-with-docker-compose</link><guid isPermaLink="true">https://chandra-prakash-babu.hashnode.dev/simplify-containerized-applications-with-docker-compose</guid><category><![CDATA[Docker]]></category><category><![CDATA[docker images]]></category><category><![CDATA[Docker compose]]></category><category><![CDATA[MERN Stack]]></category><dc:creator><![CDATA[PONDURI CHANDRA PRAKASH BABU]]></dc:creator><pubDate>Tue, 26 Sep 2023 09:13:28 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1695717369266/96f09681-12ec-4a06-ace1-05eca8314053.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="heading-introduction"><strong>Introduction</strong></h1>
<p>Containerization has revolutionized application deployment, but managing multiple containers can be complex. Enter Docker Compose, a powerful tool for simplifying multi-container applications.</p>
<p><strong>Docker Compose</strong> is a great approach when multiple containers need to work together in a multi-container application. It simplifies the management of multiple services and allows you to define the relationships and dependencies between them.</p>
<p>For a full-stack application with separate client and server components, using Docker Compose is a recommended approach. Here's how you can structure your project and create a Docker Compose configuration for it:</p>
<ol>
<li><p><strong>Project Structure</strong>:</p>
<pre><code class="lang-bash"> VVIT-clubs/
 ├── client/
 │   ├── Dockerfile
 │   ├── ... (other client files)
 ├── server/
 │   ├── Dockerfile
 │   ├── ... (other server files)
 ├── docker-compose.yml
 └── ...
</code></pre>
<ol>
<li><p>Client-side Dockerfile:  </p>
<ol>
<li><pre><code class="lang-bash"> <span class="hljs-comment"># Use an official Node runtime as a parent image</span>
 FROM node:18.0.0-alpine3.15

 <span class="hljs-comment"># Set the working directory to /app</span>
 WORKDIR /app

 <span class="hljs-comment"># Copy the package.json and package-lock.json to the working directory</span>
 COPY package*.json .

 <span class="hljs-comment"># Install the dependencies</span>
 RUN npm install

 <span class="hljs-comment"># Copy the remaining application files to the working directory</span>
 COPY . .

 <span class="hljs-comment"># Expose port 3000 for the application</span>
 EXPOSE 5173

 <span class="hljs-comment"># Start the application</span>
 CMD [<span class="hljs-string">"npm"</span>,<span class="hljs-string">"run"</span>,<span class="hljs-string">"dev"</span>]
</code></pre>
</li>
</ol>
</li>
</ol>
</li>
</ol>
<p>    2. Server-side Dockerfile:  </p>
<ol>
<li><pre><code class="lang-bash"> FROM node:14
 RUN npm install -g nodemon
 WORKDIR /server
 COPY package*.json .
 RUN npm install 
 COPY . .
 EXPOSE 5000
 CMD [<span class="hljs-string">"npm"</span>,<span class="hljs-string">"run"</span>,<span class="hljs-string">"dev"</span>]
</code></pre>
<p> For each folder, add a <code>.dockerignore</code> file. It plays a crucial role in optimizing Docker images. By specifying directories like <code>node_modules</code> and sensitive files like <code>.env</code> in this file, we ensure that these unnecessary or security-sensitive components are excluded from the Docker image.</p>
</li>
</ol>
<h3 id="heading-docker-compose-yaml">docker-compose. yaml :</h3>
<ol>
<li><pre><code class="lang-bash"> <span class="hljs-comment"># The version of the docker-compose.</span>
 version: <span class="hljs-string">"3"</span>
 <span class="hljs-comment"># Telling docker-compose to build the client and server images and run them in containers.</span>
 services:
   client:
     <span class="hljs-comment"># Telling docker-compose to build the client image using the Dockerfile in the client directory.</span>
     build: ./client
     <span class="hljs-comment"># Giving the container a name.</span>
     container_name: vvit-clubs
     <span class="hljs-comment"># Mapping the port 3000 on the host machine to the port 3000 on the container.</span>
     ports:
       - <span class="hljs-string">"5173:5173"</span>
     <span class="hljs-comment"># Mapping the client directory on the host machine to the /app directory on the container.</span>
     <span class="hljs-comment"># volumes:</span>
     <span class="hljs-comment"># - ./client:/app</span>
     <span class="hljs-comment"># - ./client/node_modules:/app/node_modules</span>
     <span class="hljs-comment"># Allows you to run container in detached mode.</span>
     stdin_open: <span class="hljs-literal">true</span>
     tty: <span class="hljs-literal">true</span>
   server:
     <span class="hljs-comment"># Telling docker-compose to build the client image using the Dockerfile in the server directory.</span>
     build: ./server
     container_name: vvit-clubs-serevr
     ports:
       - <span class="hljs-string">"5000:5000"</span>
     environment:
       - MONGODB_URI=mongodb_uri
     <span class="hljs-comment"># volumes:</span>
     <span class="hljs-comment"># - ./server:/app</span>
     <span class="hljs-comment"># - ./server/node_modules:/app/node_modules</span>
</code></pre>
</li>
</ol>
<h2 id="heading-build-docker-compose"><strong>Build Docker-Compose</strong></h2>
<p>Once the docker-compose is set up, your client/server application needs to be built. This step corresponds to the ‘docker build’ command but is applied to the different services.</p>
<pre><code class="lang-bash">$ docker-compose build
<span class="hljs-comment">#14 [client 5/5] COPY . .</span>
<span class="hljs-comment">#14 CACHED</span>

<span class="hljs-comment">#15 [client] exporting to image</span>
<span class="hljs-comment">#15 exporting layers done</span>
<span class="hljs-comment">#15 writing image sha256:ed4fa1d4e6d3e6d0a0703063230ef2dde67af686ad37c6205a06904ce639734a 0.0s done</span>
<span class="hljs-comment">#15 naming to docker.io/library/vvitclubs-client 0.0s done</span>
<span class="hljs-comment">#15 DONE 0.0s</span>
<span class="hljs-comment">#21 [server 6/6] COPY . .</span>
<span class="hljs-comment">#21 CACHED</span>

<span class="hljs-comment">#22 [server] exporting to image</span>
<span class="hljs-comment">#22 exporting layers done</span>
<span class="hljs-comment">#22 writing image sha256:f9aa272bcd456afc115571a18574ee15cd7a1e108e3750cd636c25164879a557 0.0s done</span>
<span class="hljs-comment">#22 naming to doc</span>
</code></pre>
<h2 id="heading-run-docker-compose"><strong>Run Docker-Compose</strong></h2>
<p>Your docker-compose is built! Now it’s time to start! This step corresponds to the ‘docker run’ command but applied to the different services.</p>
<pre><code class="lang-bash">$ docker-compose up
vvit-clubs         |   ➜  Local:   http://localhost:5173/
vvit-clubs         |   ➜  Network: http://172.18.0.2:5173/
vvit-clubs         |   ➜  press h to show <span class="hljs-built_in">help</span>
vvit-clubs-serevr  | Server is running on port 5001
vvit-clubs-serevr  | Connected to MongoDB
vvit-clubs-serevr  | Server listening on port 5000
</code></pre>
<p>Once the container is running, you should be able to view your React application by navigating to <a target="_blank" href="http://localhost:3000"><code>http://localhost:</code></a><code>5173</code> in your web browser.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1695718768782/7c1a8d7d-4217-4526-8756-5a166f2e16a6.png" alt /></p>
<ul>
<li>Stops containers and removes containers, and images by ‘<strong><em>docker-compose down</em></strong>’.</li>
</ul>
<pre><code class="lang-bash">$ docker-compose down
</code></pre>
<ul>
<li><p>Displays log output from services `<strong><em>docker-compose logs -f client*</em></strong>`*. Again you can see the application is running on <a target="_blank" href="http://localhost:3000"><code>http://localhost:</code></a><code>5173</code> in your web browser.</p>
<pre><code class="lang-bash">  $ docker-compose logs -f client
  vvit-clubs  |
  vvit-clubs  | &gt; client@0.0.0 dev
  vvit-clubs  | &gt; vite
  vvit-clubs  |   VITE v4.4.3  ready <span class="hljs-keyword">in</span> 4113 ms
  vvit-clubs  |
  vvit-clubs  |   ➜  Local:   http://localhost:5173/
  vvit-clubs  |   ➜  Network: http://172.18.0.2:5173/
  vvit-clubs  |   ➜  press h to show <span class="hljs-built_in">help</span>
  vvit-clubs  |
  vvit-clubs  | &gt; client@0.0.0 dev
  vvit-clubs  | &gt; vite
</code></pre>
<p>  <strong>Containers List:</strong></p>
<ul>
<li><pre><code class="lang-bash">  $ docker-compose ps
  NAME                IMAGE              COMMAND                              SERVICE   CREATED          STATUS         PORTS
  vvit-clubs          vvitclubs-client   <span class="hljs-string">"docker-entrypoint.sh npm run dev"</span>   client    4 hours ago      Up 9 minutes   0.0.0.0:5173-&gt;5173/tcp
  vvit-clubs-serevr   vvitclubs-server   <span class="hljs-string">"docker-entrypoint.sh npm run dev"</span>   server    47 minutes ago   Up 9 minutes   0.0.0.0:5000-&gt;5000/tcp
</code></pre>
</li>
</ul>
</li>
</ul>
<p>    <strong>List Images:</strong></p>
<ul>
<li><pre><code class="lang-bash">  $ docker-compose images
  CONTAINER           REPOSITORY          TAG                 IMAGE ID            SIZE
  vvit-clubs          vvitclubs-client    latest              ed4fa1d4e6d3        474MB
  vvit-clubs-serevr   vvitclubs-server    latest              f9aa272bcd45        980MB
</code></pre>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Data Structures]]></title><description><![CDATA[Data Structure is a way to store and organize data so that it can be used efficiently. The data structure name indicates organizing the data in memory.
Types of Data Structures :
There are two types of data structures:

Primitive data structure

Non-...]]></description><link>https://chandra-prakash-babu.hashnode.dev/data-structures</link><guid isPermaLink="true">https://chandra-prakash-babu.hashnode.dev/data-structures</guid><category><![CDATA[data structures]]></category><category><![CDATA[#linkedlists]]></category><category><![CDATA[Tree]]></category><category><![CDATA[graphs]]></category><category><![CDATA[array]]></category><dc:creator><![CDATA[PONDURI CHANDRA PRAKASH BABU]]></dc:creator><pubDate>Wed, 16 Aug 2023 17:55:15 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1692193640741/666833fa-c1f7-4a58-9607-bf94f921bcaa.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Data Structure is a way to store and organize data so that it can be used efficiently. The data structure name indicates organizing the data in memory.</p>
<p>Types of Data Structures :</p>
<p>There are two types of data structures:</p>
<ul>
<li><p>Primitive data structure</p>
</li>
<li><p>Non-primitive data structure</p>
</li>
</ul>
<p><strong>Primitive Data structure</strong></p>
<p>The primitive data structures are primitive data types. The int, char, float, double, and pointer are primitive data structures that can hold a single value.</p>
<p><strong>Non-Primitive Data structure</strong></p>
<p>The non-primitive data structure is divided into two types:</p>
<ul>
<li><p>Linear data structure</p>
</li>
<li><p>Non-linear data structure</p>
</li>
</ul>
<h3 id="heading-major-operations">Major Operations</h3>
<p>The major or common operations that can be performed on the data structures are:</p>
<ul>
<li><p><strong>Searching:</strong> We can search for any element in a data structure.</p>
</li>
<li><p><strong>Sorting:</strong> We can sort the elements of a data structure either in an ascending or descending order.</p>
</li>
<li><p><strong>Insertion:</strong> We can also insert a new element in a data structure.</p>
</li>
<li><p><strong>Updation:</strong> We can also update the element, i.e., we can replace the element with another element.</p>
</li>
<li><p><strong>Deletion:</strong> We can also perform the delete operation to remove the element from the data structure.</p>
</li>
</ul>
<h3 id="heading-advantages-of-data-structures">Advantages of Data structures</h3>
<p><strong>The following are the advantages of a data structure:</strong></p>
<ul>
<li><p><strong>Efficiency</strong></p>
</li>
<li><p><strong>Reusability</strong></p>
</li>
<li><p><strong>Abstraction</strong></p>
<h1 id="heading-1-array-in-data-structure">1. Array in Data Structure</h1>
</li>
</ul>
<p><img src="https://static.javatpoint.com/ds/images/ds-array.png" alt="Array in DS" /></p>
<p>As per the above illustration, there are some of the following important points -</p>
<ul>
<li><p>The index starts with 0.</p>
</li>
<li><p>The array's length is 10, which means we can store 10 elements.</p>
</li>
<li><p>Each element in the array can be accessed via its index.</p>
</li>
</ul>
<h3 id="heading-memory-allocation-of-an-array">Memory allocation of an array</h3>
<p>We can define the indexing of an array in the below ways -</p>
<ol>
<li><p>0 (zero-based indexing): The first element of the array will be arr[0].</p>
</li>
<li><p>1 (one-based indexing): The first element of the array will be arr[1].</p>
</li>
<li><p>n (n - based indexing): The first element of the array can reside at any random index number.</p>
</li>
</ol>
<h2 id="heading-basic-operations">Basic operations</h2>
<p>Now, let's discuss the basic operations supported in the array -</p>
<ul>
<li><p>Traversal - This operation is used to print the elements of the array.</p>
</li>
<li><p>Insertion - It is used to add an element at a particular index.</p>
</li>
<li><p>Deletion - It is used to delete an element from a particular index.</p>
</li>
<li><p>Search - It is used to search an element using the given index or by the value.</p>
</li>
<li><p>Update - It updates an element at a particular index.</p>
<h1 id="heading-2-linked-list">2. Linked list</h1>
<p>  A linked list is useful because -</p>
<ul>
<li><p>It allocates the memory dynamically. All the nodes of the linked list are non-contiguously stored in the memory and linked together with the help of pointers.</p>
</li>
<li><p>In the linked list, size is no longer a problem since we do not need to define its size at the time of declaration. The list grows as per the program's demand and is limited to the available memory space.</p>
</li>
</ul>
</li>
</ul>
<h3 id="heading-how-to-declare-a-linked-list">How to declare a linked list?</h3>
<ol>
<li><pre><code class="lang-c">  <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span>  
  {</span>  
  <span class="hljs-keyword">int</span> data;  
  <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">next</span>;</span>  
  }
</code></pre>
<p> One is data of integer type and another variable is next which is a pointer that contains the address of the next node.</p>
</li>
<li><h3 id="heading-types-of-linked-lists">Types of Linked Lists</h3>
<ul>
<li><p><strong>Singly-linked list</strong></p>
</li>
<li><p><strong>Doubly linked list</strong></p>
</li>
<li><p><strong>Circular singly linked list</strong></p>
</li>
<li><p><strong>Circular doubly linked list</strong></p>
</li>
</ul>
</li>
</ol>
<h3 id="heading-disadvantages-of-linked-list">Disadvantages of Linked list</h3>
<p>        The limitations of using the Linked list are given as follows -</p>
<ul>
<li><p><strong>Memory usage -</strong> In a linked list, a node occupies more memory than an array.</p>
</li>
<li><p><strong>Traversal -</strong> Traversal is not easy in the linked list. If we have to access an element in the linked list, we cannot access it randomly, while in case of an array, we can randomly access it by index.</p>
</li>
<li><p><strong>Reverse traversing -</strong> Backtracking or reverse traversing is difficult in a linked list. In a doubly-linked list, it is easier but requires more memory to store the back pointer.</p>
</li>
</ul>
<h1 id="heading-doubly-linked-list">Doubly linked list</h1>
<p>        Doubly linked list is a complex type of linked list in which a node contains a pointer to the previous as well as the next node in the sequence. Therefore, in a doubly linked list, a node consists of three parts: node data, pointer to the next node in sequence (next pointer) , pointer to the previous node (previous pointer).</p>
<p>        <img src="https://static.javatpoint.com/ds/images/doubly-linked-list.png" alt="Doubly linked list" /></p>
<ol>
<li><pre><code class="lang-c">  <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span>   
  {</span>  
      <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">prev</span>;</span>   
      <span class="hljs-keyword">int</span> data;  
      <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">next</span>;</span>   
  }
</code></pre>
</li>
</ol>
<p>        <strong>Problem: Remove Nth Node From End of List</strong></p>
<p>        Given a linked list, remove the n-th node from the end of the list and return its head.</p>
<p>        <strong>Example:</strong></p>
<p>        Input: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5, n = 2</p>
<p>        Output: 1 -&gt; 2 -&gt; 3 -&gt; 5</p>
<ol>
<li><pre><code class="lang-java">  <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ListNode</span> </span>{
      <span class="hljs-keyword">int</span> val;
      ListNode next;
      ListNode(<span class="hljs-keyword">int</span> val) {
          <span class="hljs-keyword">this</span>.val = val;
      }
  }

  <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RemoveNthNodeFromEnd</span> </span>{
      <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">removeNthFromEnd</span><span class="hljs-params">(ListNode head, <span class="hljs-keyword">int</span> n)</span> </span>{
          ListNode dummy = <span class="hljs-keyword">new</span> ListNode(<span class="hljs-number">0</span>);
          dummy.next = head;
          ListNode first = dummy;
          ListNode second = dummy;

          <span class="hljs-comment">// Advance the first pointer by n + 1 steps</span>
          <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt;= n; i++) {
              first = first.next;
          }

          <span class="hljs-comment">// Move both pointers until the first pointer reaches the end</span>
          <span class="hljs-keyword">while</span> (first != <span class="hljs-keyword">null</span>) {
              first = first.next;
              second = second.next;
          }

          <span class="hljs-comment">// Remove the nth node from the end</span>
          second.next = second.next.next;

          <span class="hljs-keyword">return</span> dummy.next;
      }

      <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
          <span class="hljs-comment">// Create the linked list: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5</span>
          ListNode head = <span class="hljs-keyword">new</span> ListNode(<span class="hljs-number">1</span>);
          head.next = <span class="hljs-keyword">new</span> ListNode(<span class="hljs-number">2</span>);
          head.next.next = <span class="hljs-keyword">new</span> ListNode(<span class="hljs-number">3</span>);
          head.next.next.next = <span class="hljs-keyword">new</span> ListNode(<span class="hljs-number">4</span>);
          head.next.next.next.next = <span class="hljs-keyword">new</span> ListNode(<span class="hljs-number">5</span>);

          RemoveNthNodeFromEnd solution = <span class="hljs-keyword">new</span> RemoveNthNodeFromEnd();
          <span class="hljs-keyword">int</span> n = <span class="hljs-number">2</span>;
          ListNode newHead = solution.removeNthFromEnd(head, n);

          <span class="hljs-comment">// Print the modified linked list</span>
          ListNode current = newHead;
          <span class="hljs-keyword">while</span> (current != <span class="hljs-keyword">null</span>) {
              System.out.print(current.val + <span class="hljs-string">" -&gt; "</span>);
              current = current.next;
          }
      }
  }
</code></pre>
</li>
</ol>
<p>        <strong>Applications of LinkedLists:</strong></p>
<ol>
<li><p><strong>Blockchain Technology</strong></p>
</li>
<li><p><strong>Graph Representations</strong></p>
</li>
<li><p><strong>Garbage Collection</strong></p>
</li>
<li><p><strong>Navigation Systems</strong></p>
</li>
<li><p><strong>Music and Video Playlists</strong></p>
</li>
<li><p><strong>Dynamic Memory Allocation</strong></p>
</li>
</ol>
<ul>
<li><h1 id="heading-3-stack">3. Stack</h1>
<p>  A Stack is a linear data structure that follows the <strong>LIFO (Last-In-First-Out)</strong> principle. Stack has one end. It contains only one pointer <strong>top pointer</strong> pointing to the topmost element of the stack. Whenever an element is added to the stack, it is added on the top of the stack, and the element can be deleted only from the stack. In other words, a <strong><em>stack can be defined as a container in which insertion and deletion can be done from one end known as the top of the stack.</em></strong></p>
<p>  <img src="https://static.javatpoint.com/ds/images/ds-stack.png" alt="DS Stack Introduction" /></p>
<h3 id="heading-standard-stack-operations">Standard Stack Operations</h3>
<p>  <strong>The following are some common operations implemented on the stack:</strong></p>
<ul>
<li><p><strong>push():</strong> When we insert an element in a stack then the operation is known as a push. If the stack is full then the <strong>overflow</strong> condition occurs.</p>
</li>
<li><p><strong>pop():</strong> When we delete an element from the stack, the operation is known as a pop. If the stack is empty means that no element exists in the stack, this state is known as an <strong>underflow</strong> state.</p>
</li>
<li><p><strong>isEmpty():</strong> It determines whether the stack is empty or not.</p>
</li>
<li><p><strong>isFull():</strong> It determines whether the stack is full or not.'</p>
</li>
<li><p><strong>peek():</strong> It returns the element at the given position.</p>
</li>
<li><p><strong>count():</strong> It returns the total number of elements available in a stack.</p>
</li>
<li><p><strong>change():</strong> It changes the element at the given position.</p>
</li>
<li><p><strong>display():</strong> It prints all the elements available in the stack.</p>
</li>
</ul>
</li>
</ul>
<p>    <strong>Applications of Stack:</strong></p>
<ol>
<li><p><strong>Expression Evaluation</strong></p>
</li>
<li><p><strong>Undo/Redo Operations</strong></p>
</li>
<li><p><strong>Function Call Management</strong></p>
</li>
<li><p><strong>Balancing Parentheses and Symbols</strong></p>
</li>
<li><p><strong>Backtracking Algorithms</strong></p>
</li>
<li><p><strong>Browser History</strong></p>
</li>
<li><p><strong>Task Management</strong></p>
</li>
</ol>
<p>    <strong>Problem: Valid Parentheses</strong></p>
<p>    Given a string containing only characters <code>'('</code>, <code>')'</code>, <code>'{'</code>, <code>'}'</code>, <code>'['</code>, and <code>']'</code>, determine if the input string is valid. An input string is valid if:</p>
<ol>
<li><p>Open brackets must be closed by the same type of brackets.</p>
</li>
<li><p>Open brackets must be closed in the correct order.</p>
</li>
</ol>
<p>    <strong>Example:</strong></p>
<p>    Input: <code>"(){}[]"</code> Output: <code>true</code></p>
<p>    Input: <code>"{[()]()}"</code> Output: <code>true</code></p>
<p>    Input: <code>"{[(])}"</code> Output: <code>false</code></p>
<p>    <strong>Solution:</strong></p>
<ol>
<li><pre><code class="lang-java">  <span class="hljs-keyword">import</span> java.util.Stack;

  <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ValidParentheses</span> </span>{
      <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">isValid</span><span class="hljs-params">(String s)</span> </span>{
          Stack&lt;Character&gt; stack = <span class="hljs-keyword">new</span> Stack&lt;&gt;();

          <span class="hljs-keyword">for</span> (<span class="hljs-keyword">char</span> c : s.toCharArray()) {
              <span class="hljs-keyword">if</span> (c == <span class="hljs-string">'('</span> || c == <span class="hljs-string">'{'</span> || c == <span class="hljs-string">'['</span>) {
                  stack.push(c);
              } <span class="hljs-keyword">else</span> {
                  <span class="hljs-keyword">if</span> (stack.isEmpty()) {
                      <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
                  }
                  <span class="hljs-keyword">char</span> top = stack.pop();
                  <span class="hljs-keyword">if</span> ((c == <span class="hljs-string">')'</span> &amp;&amp; top != <span class="hljs-string">'('</span>) || (c == <span class="hljs-string">'}'</span> &amp;&amp; top != <span class="hljs-string">'{'</span>) || (c == <span class="hljs-string">']'</span> &amp;&amp; top != <span class="hljs-string">'['</span>)) {
                      <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
                  }
              }
          }

          <span class="hljs-keyword">return</span> stack.isEmpty();
      }

      <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
          ValidParentheses solution = <span class="hljs-keyword">new</span> ValidParentheses();
          String input = <span class="hljs-string">"(){}[]"</span>;
          System.out.println(<span class="hljs-string">"Is input valid? "</span> + solution.isValid(input));
      }
  }
</code></pre>
</li>
</ol>
<h1 id="heading-4-queue">4. Queue</h1>
<p>    A queue can be defined as an ordered list that enables insert operations to be performed at one end called <strong>REAR</strong> and delete operations to be performed at another end called <strong>FRONT</strong>.</p>
<p>    The queue is referred to as the First In First Out list.</p>
<p>    <img src="https://static.javatpoint.com/ds/images/queue.png" alt="ds Queue" /></p>
<h3 id="heading-types-of-queue">Types of Queue</h3>
<ul>
<li><p>Simple Queue or Linear Queue</p>
<p>  <img src="https://static.javatpoint.com/ds/images/ds-types-of-queue3.png" alt="Types of Queue" /></p>
</li>
<li><p>Circular Queue</p>
<p>  <img src="https://static.javatpoint.com/ds/images/ds-types-of-queue4.png" alt="Types of Queue" /></p>
</li>
<li><p>Priority Queue</p>
<p>  <img src="https://static.javatpoint.com/ds/images/ds-types-of-queue5.png" alt="Types of Queue" /></p>
</li>
<li><p>Double Ended Queue (or Deque)</p>
<p>  <img src="https://static.javatpoint.com/ds/images/ds-types-of-queue6.png" alt="Types of Queue" /></p>
</li>
</ul>
<h2 id="heading-operations-performed-on-queue">Operations performed on queue</h2>
<ul>
<li><p><strong>Enqueue:</strong> The Enqueue operation is used to insert the element at the rear end of the queue. It returns void.</p>
</li>
<li><p><strong>Dequeue:</strong> It performs the deletion from the front end of the queue. It also returns the element which has been removed from the front end. It returns an integer value.</p>
</li>
<li><p><strong>Peek:</strong> This is the third operation that returns the element, which is pointed by the front pointer in the queue but does not delete it.</p>
</li>
<li><p><strong>Queue overflow (isfull):</strong> It shows the overflow condition when the queue is completely full.</p>
</li>
<li><p><strong>Queue underflow (isempty):</strong> It shows the underflow condition when the Queue is empty, i.e., no elements are in the Queue.</p>
</li>
</ul>
<p>    <strong>Applications Of Queue:</strong></p>
<ol>
<li><p><strong>Task Scheduling</strong></p>
</li>
<li><p><strong>Breadth-First Search (BFS)</strong></p>
</li>
<li><p><strong>Call Center Systems</strong></p>
</li>
<li><p><strong>Printing Documents in Order</strong></p>
</li>
<li><p><strong>Request Handling</strong></p>
</li>
<li><p><strong>Background Tasks</strong></p>
</li>
</ol>
<h1 id="heading-5-trees">5. Trees</h1>
<ul>
<li><p>A tree data structure is defined as a collection of objects or entities known as nodes that are linked together to represent or simulate hierarchy.</p>
</li>
<li><p>Each node contains some data and the link or reference of other nodes that can be called children.</p>
<p>  <img src="https://static.javatpoint.com/ds/images/tree4.png" alt="Tree" /></p>
</li>
</ul>
<ol>
<li><pre><code class="lang-c">  <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span>  
  {</span>  
    <span class="hljs-keyword">int</span> data;  
  <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">left</span>;</span>  
  <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">right</span>;</span>   
  }
</code></pre>
</li>
</ol>
<h3 id="heading-types-of-tree-data-structure">Types of Tree data structure</h3>
<ol>
<li><p><strong>General tree:</strong> The general tree is one of the types of tree data structure. In the general tree, a node can have either 0 or maximum n number of nodes.</p>
</li>
<li><p><a target="_blank" href="https://www.javatpoint.com/binary-tree"><strong>Binary tree</strong></a><strong>:</strong> Here, binary name itself suggests two numbers, i.e., 0 and 1. In a binary tree, each node in a tree can have utmost two child nodes.</p>
</li>
<li><p><strong>Binary Search Tree</strong>: A binary search tree follows some order to arrange the elements. In a Binary search tree, the value of left node must be smaller than the parent node, and the value of right node must be greater than the parent node.</p>
</li>
<li><p><strong>AVL Tree</strong> can be defined as height balanced binary search tree in which each node is associated with a balance factor which is calculated by subtracting the height of its right sub-tree from that of its left sub-tree.</p>
</li>
</ol>
<p>    <strong>DrawBack in Binary Search Tree:</strong></p>
<p>    The skewed binary search tree is a disadvantage in BST.</p>
<p>    For a skewed binary search tree o(n) is the height of this tree so no comparisons is n.</p>
<ul>
<li><p>To reduce o(n) to logn we can use the Balance tree(AVL).</p>
</li>
<li><p><img src="https://static.javatpoint.com/ds/images/avl-tree.jpg" alt="AVL Rotations" /></p>
<p>  <strong>Applications of Trees:</strong></p>
<ol>
<li><p><strong>Hierarchical Data Representation</strong></p>
</li>
<li><p><strong>Binary Search Trees (BST):</strong> Binary search trees enable efficient searching, insertion, and deletion of elements. They are used in databases, dynamic sets, and maintaining ordered data.</p>
</li>
<li><p><strong>Balanced Binary Trees (AVL, Red-Black Trees):</strong> Balanced binary trees ensure that the height of the tree remains logarithmic, leading to efficient operations. They are used in databases, compilers, and file systems.</p>
</li>
<li><p><strong>Heap Data Structures:</strong> Binary heaps, which are a type of tree, are used to implement priority queues, scheduling algorithms, and efficient sorting algorithms like heapsort.</p>
</li>
<li><p>**Expression Parsing and Evaluation:**They are essential in compilers, calculators, and symbolic computation systems.</p>
</li>
<li><p><strong>Decision Trees:</strong> Decision trees are used in machine learning and artificial intelligence for classification, regression, and decision-making processes.</p>
</li>
<li><p><strong>Spanning Trees in Graphs:</strong> Minimum spanning trees are used to find the most efficient way to connect all vertices in a graph, which is useful in network design and clustering.</p>
</li>
<li><p><strong>XML and JSON Parsing</strong></p>
</li>
<li><p><strong>Network Routing Algorithms:</strong> Trees are used in network routing protocols to determine the optimal path for data packets to travel through a network.</p>
</li>
</ol>
</li>
</ul>
<p>    <strong>Problem: Binary Tree Maximum Path Sum</strong></p>
<ul>
<li><p>Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree and does not need to go through the root.</p>
<p>  <strong>Solution:</strong></p>
<p>  To solve this problem, you can use a recursive approach. At each node, calculate the maximum path sum that includes the current node and consider the following three cases:</p>
<ol>
<li><p>The path only includes the current node.</p>
</li>
<li><p>The path goes through the current node and the left subtree.</p>
</li>
<li><p>The path goes through the current node and the right subtree.</p>
</li>
<li><pre><code class="lang-java">  <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TreeNode</span> </span>{
      <span class="hljs-keyword">int</span> val;
      TreeNode left;
      TreeNode right;
      TreeNode(<span class="hljs-keyword">int</span> val) {
          <span class="hljs-keyword">this</span>.val = val;
      }
  }

  <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BinaryTreeMaxPathSum</span> </span>{
      <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> maxPathSum;

      <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">maxPathSum</span><span class="hljs-params">(TreeNode root)</span> </span>{
          maxPathSum = Integer.MIN_VALUE;
          calculateMaxPathSum(root);
          <span class="hljs-keyword">return</span> maxPathSum;
      }

      <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> <span class="hljs-title">calculateMaxPathSum</span><span class="hljs-params">(TreeNode node)</span> </span>{
          <span class="hljs-keyword">if</span> (node == <span class="hljs-keyword">null</span>) {
              <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
          }

          <span class="hljs-keyword">int</span> leftSum = Math.max(calculateMaxPathSum(node.left), <span class="hljs-number">0</span>);
          <span class="hljs-keyword">int</span> rightSum = Math.max(calculateMaxPathSum(node.right), <span class="hljs-number">0</span>);

          <span class="hljs-keyword">int</span> currentPathSum = node.val + leftSum + rightSum;
          maxPathSum = Math.max(maxPathSum, currentPathSum);

          <span class="hljs-keyword">return</span> node.val + Math.max(leftSum, rightSum);
      }

      <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
          TreeNode root = <span class="hljs-keyword">new</span> TreeNode(<span class="hljs-number">1</span>);
          root.left = <span class="hljs-keyword">new</span> TreeNode(<span class="hljs-number">2</span>);
          root.right = <span class="hljs-keyword">new</span> TreeNode(<span class="hljs-number">3</span>);

          BinaryTreeMaxPathSum solution = <span class="hljs-keyword">new</span> BinaryTreeMaxPathSum();
          <span class="hljs-keyword">int</span> result = solution.maxPathSum(root);
          System.out.println(<span class="hljs-string">"Maximum Path Sum: "</span> + result);
      }
  }
</code></pre>
</li>
</ol>
</li>
<li><h1 id="heading-6-graphs">6. Graphs</h1>
<p>  A graph G can be defined as an ordered set G(V, E) where V(G) represents the set of vertices and E(G) represents the set of edges which are used to connect these vertices.</p>
<h2 id="heading-directed-and-undirected-graph">Directed and Undirected Graph</h2>
<p>  <img src="https://static.javatpoint.com/ds/images/graph-definition.png" alt="Graph" /></p>
<p>  <img src="https://static.javatpoint.com/ds/images/directed-and-undirected-graph.png" alt="Graph" /></p>
<p>  A graph is a data structure that consist a sets of vertices (called nodes) and edges. There are two ways to store Graphs into the computer's memory:</p>
<ul>
<li><p><strong>Sequential representation</strong> (or, Adjacency matrix representation)</p>
</li>
<li><p><strong>Linked list representation</strong> (or, Adjacency list representation)</p>
</li>
</ul>
</li>
</ul>
<h3 id="heading-sequential-representation">Sequential representation</h3>
<p>    <img src="https://static.javatpoint.com/ds/images/graph-representation.png" alt="Graph Representation" /></p>
<p>    <strong>Adjacency matrix for a weighted directed graph</strong></p>
<p>    <img src="https://static.javatpoint.com/ds/images/graph-representation3.png" alt="Graph Representation" /></p>
<h2 id="heading-linked-list-representation">Linked list representation</h2>
<p>    <img src="https://static.javatpoint.com/ds/images/graph-representation4.png" alt="Graph Representation" /></p>
<p>    <img src="https://static.javatpoint.com/ds/images/graph-representation6.png" alt="Graph Representation" /></p>
<p>    <strong><em>A spanning tree</em></strong> of a connected, undirected graph is a subgraph that includes all the vertices of the original graph while forming a tree (a connected acyclic graph). Spanning trees are essential concepts in graph theory and have numerous applications, including network design, optimization problems, and more. Here are two important types of spanning trees and their applications:</p>
<p>    A spanning tree of a connected, undirected graph is a subgraph that includes all the vertices of the original graph while forming a tree (a connected acyclic graph). Spanning trees are essential concepts in graph theory and have numerous applications, including network design, optimization problems, and more. Here are two important types of spanning trees and their applications:</p>
<ol>
<li><p><strong>Minimum Spanning Tree (MST):</strong> A minimum spanning tree of a weighted, connected graph is a spanning tree with the minimum possible total edge weight. MSTs have various real-world applications, such as designing efficient networks and minimizing costs in various scenarios.</p>
<p> <strong>Applications:</strong></p>
<ul>
<li><p>Network Design: Building communication networks (telephone, internet) while minimizing cable installation costs.</p>
</li>
<li><p>Cluster Analysis: Grouping data points to minimize the sum of distances between cluster members.</p>
</li>
<li><p>Transportation: Constructing efficient road or railway systems to minimize travel distances.</p>
</li>
<li><p>Power Distribution</p>
</li>
</ul>
</li>
</ol>
<p>        <img src="https://www.codingeek.com/wp-content/uploads/2017/01/kruskal.png" alt="Minimum Spanning Tree-MST Kruskal and Prims algorithm Explanation |  Codingeek" /></p>
<p>        To construct a spanning tree for weighted graph with minimum cost we can use</p>
<ol>
<li><p><strong>Kruskal's Algorithm:</strong> It uses a disjoint-set data structure (usually implemented with union-find) to keep track of connected components and avoid cycles. Kruskal's algorithm is greedy, as it always selects the smallest edge that doesn't create a cycle.</p>
</li>
<li><p><strong>Prim's Algorithm:</strong> Prim's algorithm starts with an arbitrary vertex and grows the MST by iteratively adding the minimum-weight edge that connects a vertex in the MST to a vertex outside of it. It maintains a set of vertices in the MST and selects edges that connect the MST to the rest of the graph.</p>
</li>
</ol>
<p>        <strong>Graphs applications:</strong></p>
<ol>
<li><p><strong>Social Networks</strong></p>
</li>
<li><p><strong>Transportation Networks</strong></p>
</li>
<li><p><strong>Computer Networks</strong></p>
</li>
<li><p><strong>Recommendation Systems</strong></p>
</li>
<li><p><strong>Fraud Detection</strong></p>
</li>
<li><p><strong>Knowledge Representation</strong></p>
</li>
<li><p><strong>Route Planning</strong></p>
</li>
<li><p><strong>Semantic Web:</strong> Graphs model data with rich semantics, enabling better data interoperability, integration, and search.</p>
</li>
<li><p><strong>Machine Learning:</strong> Graph-based learning models capture data dependencies and relationships for various tasks like recommendation and link prediction.</p>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[File Upload & Download with AWS S3, 
                                                  React, Node.js]]></title><description><![CDATA[Introduction
In today's fast-paced digital landscape, efficiently managing data is a key challenge for businesses and developers alike. Handling large files, such as images, videos, or documents, demands a robust and scalable solution.
In this blog, ...]]></description><link>https://chandra-prakash-babu.hashnode.dev/file-upload-download-with-aws-s3-react-nodejs</link><guid isPermaLink="true">https://chandra-prakash-babu.hashnode.dev/file-upload-download-with-aws-s3-react-nodejs</guid><category><![CDATA[S3]]></category><category><![CDATA[S3-bucket]]></category><category><![CDATA[AWS s3]]></category><category><![CDATA[React]]></category><category><![CDATA[Node.js]]></category><dc:creator><![CDATA[PONDURI CHANDRA PRAKASH BABU]]></dc:creator><pubDate>Tue, 08 Aug 2023 09:36:05 GMT</pubDate><content:encoded><![CDATA[<p><img src="https://miro.medium.com/v2/resize:fit:1050/0*ubW4IKPsoQ5adnPV.jpeg" alt class="image--right mx-auto mr-0" /></p>
<h2 id="heading-introduction"><strong>Introduction</strong></h2>
<p>In today's fast-paced digital landscape, efficiently managing data is a key challenge for businesses and developers alike. Handling large files, such as images, videos, or documents, demands a robust and scalable solution.</p>
<p>In this blog, we explore how to leverage the capabilities of ReactJS, AWS S3, and MongoDB to effortlessly upload files to an S3 bucket and persist the file information in a MongoDB database. We'll guide you through the process, step- by-step, from setting up the required infrastructure to implementing the code for a smooth user experience.</p>
<h2 id="heading-prerequisites"><strong>Prerequisites</strong></h2>
<p>Before we begin, make sure you have the following prerequisites in place:</p>
<ol>
<li><p>Basic knowledge of JavaScript, React, and Node.js and MongoDB.</p>
</li>
<li><p>An AWS account with access to S3.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1691475560483/71feda4b-ddb8-4030-adc5-78519432a737.png" alt class="image--center mx-auto" /></p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1691476053300/3240076f-a881-49c5-a943-532bbb58174f.png" alt class="image--center mx-auto" /></p>
<p> Allow public access and apply this bucket policy:</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1691476150354/91a56ec2-b724-43b7-90af-a5bf4a667093.png" alt class="image--center mx-auto" /></p>
</li>
</ol>
<h2 id="heading-setting-up-the-backend"><strong>Setting Up the Backend</strong></h2>
<ol>
<li><p>Start by setting up a new Node.js project with Express.</p>
<pre><code class="lang-javascript">  npm init -y
  npm install express
  npm install mongoose
</code></pre>
</li>
<li><p>Install the necessary dependencies, including the AWS SDK, using npm or yarn.</p>
<pre><code class="lang-javascript">  npm install multer @aws-sdk/client-s3 dotenv cors
</code></pre>
</li>
<li><p>Configure the AWS SDK with your AWS access key and secret key.</p>
<pre><code class="lang-javascript"> <span class="hljs-keyword">const</span> s3Client = <span class="hljs-keyword">new</span> S3Client({
    <span class="hljs-attr">region</span>: process.env.AWS_ACCOUNT_REGION,
    <span class="hljs-attr">credentials</span>: {
      <span class="hljs-attr">accessKeyId</span>: process.env.AWS_ACCOUNT_ACCESS_KEY,
      <span class="hljs-attr">secretAccessKey</span>: process.env.AWS_ACCOUNT_SECRET_ACCESS_KEY,
    },
  });
</code></pre>
</li>
<li><p>Implement the backend API routes for handling file upload and download.</p>
</li>
<li><p>We can write this code within the function like uploadImage, and we can use it as middleware.</p>
</li>
</ol>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { S3Client, PutObjectCommand } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'@aws-sdk/client-s3'</span>);
<span class="hljs-keyword">const</span> express=<span class="hljs-built_in">require</span>(<span class="hljs-string">'express'</span>);
<span class="hljs-keyword">const</span> dotenv=<span class="hljs-built_in">require</span>(<span class="hljs-string">'dotenv'</span>);
dotenv.config();
<span class="hljs-keyword">const</span> s3Client = <span class="hljs-keyword">new</span> S3Client({
    <span class="hljs-attr">region</span>: process.env.AWS_ACCOUNT_REGION,
    <span class="hljs-attr">credentials</span>: {
      <span class="hljs-attr">accessKeyId</span>: process.env.AWS_ACCOUNT_ACCESS_KEY,
      <span class="hljs-attr">secretAccessKey</span>: process.env.AWS_ACCOUNT_SECRET_ACCESS_KEY,
    },
  });

<span class="hljs-keyword">const</span> uploadImage=<span class="hljs-keyword">async</span> (folderName,file) =&gt; {
    <span class="hljs-keyword">try</span> {
      <span class="hljs-keyword">const</span> contentType = file.mimetype;

      <span class="hljs-keyword">const</span> command =<span class="hljs-keyword">new</span> PutObjectCommand({
        <span class="hljs-attr">Bucket</span>: process.env.AWS_BUCKET_NAME,
        <span class="hljs-attr">Key</span>:<span class="hljs-string">`<span class="hljs-subst">${folderName}</span>/<span class="hljs-subst">${file.originalname}</span>`</span>,
        <span class="hljs-attr">Body</span>: file.buffer,
        <span class="hljs-attr">ContentType</span>: contentType,
      });

      <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> s3Client.send(command);
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"File uploaded to S3:"</span>, response);

    } <span class="hljs-keyword">catch</span> (error) {
      <span class="hljs-built_in">console</span>.error(<span class="hljs-string">"Failed to upload file to S3:"</span>, error);
    }
  };

<span class="hljs-built_in">module</span>.exports=uploadImage;
</code></pre>
<ol>
<li><p>Use the AWS SDK to interact with S3, including uploading and downloading files.</p>
</li>
<li><p>Certainly! Here's the complete code for the <strong>backend</strong>:</p>
<p> <code>server.js</code></p>
<pre><code class="lang-javascript"> <span class="hljs-keyword">import</span> express <span class="hljs-keyword">from</span> <span class="hljs-string">"express"</span>;
 <span class="hljs-keyword">import</span> multer <span class="hljs-keyword">from</span> <span class="hljs-string">"multer"</span>;
 <span class="hljs-keyword">import</span> { S3Client, PutObjectCommand } <span class="hljs-keyword">from</span> <span class="hljs-string">"@aws-sdk/client-s3"</span>;
 <span class="hljs-keyword">import</span> dotenv <span class="hljs-keyword">from</span> <span class="hljs-string">"dotenv"</span>;
 <span class="hljs-keyword">import</span> cors <span class="hljs-keyword">from</span> <span class="hljs-string">"cors"</span>;
 dotenv.config();

 <span class="hljs-keyword">const</span> app = express();
 <span class="hljs-keyword">const</span> port = process.env.PORT || <span class="hljs-number">5000</span>;

 <span class="hljs-comment">// Enable CORS</span>
 app.use(cors());

 <span class="hljs-comment">// Parse JSON request bodies</span>
 app.use(express.json());

 <span class="hljs-comment">// Create an S3 client</span>
 <span class="hljs-keyword">const</span> s3Client = <span class="hljs-keyword">new</span> S3Client({
   <span class="hljs-attr">region</span>: process.env.AWS_ACCOUNT_REGION,
   <span class="hljs-attr">credentials</span>: {
     <span class="hljs-attr">accessKeyId</span>: process.env.AWS_ACCOUNT_ACCESS_KEY,
     <span class="hljs-attr">secretAccessKey</span>: process.env.AWS_ACCOUNT_SECRET_ACCESS_KEY,
   },
 });

 <span class="hljs-comment">// Configure multer for file uploads</span>
 <span class="hljs-keyword">const</span> storage = multer.memoryStorage();
 <span class="hljs-keyword">const</span> upload = multer({
   storage,
   <span class="hljs-attr">limits</span>: {
     <span class="hljs-attr">fileSize</span>: <span class="hljs-number">25</span> * <span class="hljs-number">1024</span> * <span class="hljs-number">1024</span>, <span class="hljs-comment">// 25MB in bytes</span>
   },
 });

 app.post(<span class="hljs-string">"/api/upload"</span>, upload.single(<span class="hljs-string">"file"</span>), <span class="hljs-keyword">async</span> (req, res) =&gt; {
   <span class="hljs-keyword">try</span> {
     <span class="hljs-keyword">const</span> file = req.file;

     <span class="hljs-keyword">const</span> contentType = file.mimetype;

     <span class="hljs-keyword">const</span> params = {
       <span class="hljs-attr">Bucket</span>: process.env.AWS_BUCKET_NAME,
       <span class="hljs-attr">Key</span>: file.originalname,
       <span class="hljs-attr">Body</span>: file.buffer,
       <span class="hljs-attr">ContentType</span>: contentType,
     };

     <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> s3Client.send(<span class="hljs-keyword">new</span> PutObjectCommand(params));
     <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"File uploaded to S3:"</span>, response);

     <span class="hljs-keyword">const</span> fileUrl = <span class="hljs-string">`https://<span class="hljs-subst">${process.env.AWS_BUCKET_NAME}</span>.s3.amazonaws.com/<span class="hljs-subst">${file.originalname}</span>`</span>;

     res.status(<span class="hljs-number">200</span>).json({ fileUrl });
   } <span class="hljs-keyword">catch</span> (error) {
     <span class="hljs-built_in">console</span>.error(<span class="hljs-string">"Failed to upload file to S3:"</span>, error);
     res.status(<span class="hljs-number">500</span>).json({ <span class="hljs-attr">error</span>: <span class="hljs-string">"Failed to upload file to S3"</span> });
   }
 });

 app.listen(port, <span class="hljs-function">() =&gt;</span> {
   <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Server started on port <span class="hljs-subst">${port}</span>`</span>);
 });
</code></pre>
</li>
<li><p>So the uploaded image has a specific URL, we can push that format URL into our mongo db collection.</p>
<p> <em>clubController.js</em></p>
<ol>
<li><pre><code class="lang-javascript"> <span class="hljs-keyword">const</span> addClub=(expressAsyncHandler(<span class="hljs-keyword">async</span>(req,res)=&gt;{
     <span class="hljs-keyword">const</span> clubs=<span class="hljs-keyword">new</span> Clubs({
         <span class="hljs-attr">name</span>:req.body.name,
         <span class="hljs-attr">desc</span>:req.body.desc,
     });
     <span class="hljs-keyword">if</span> (req.file) {
         <span class="hljs-comment">// Uploading the profile image to AWS S3</span>
         <span class="hljs-keyword">await</span> uploadImage(<span class="hljs-string">"clubs"</span>,req.file);

         <span class="hljs-comment">// Setting the cover image URL in the book model</span>
         clubs.image = <span class="hljs-string">`https://<span class="hljs-subst">${process.env.AWS_BUCKET_NAME}</span>.s3.amazonaws.com/clubs/<span class="hljs-subst">${req.file.originalname}</span>`</span>;
     }
     <span class="hljs-keyword">const</span> res1=<span class="hljs-keyword">await</span> clubs.save();
     <span class="hljs-built_in">console</span>.log(res1);
     <span class="hljs-keyword">return</span> res.status(StatusCodes.OK).json({<span class="hljs-attr">message</span>:<span class="hljs-string">"club added succesfully"</span>});
 }));
</code></pre>
</li>
</ol>
</li>
</ol>
<h2 id="heading-building-the-frontend"><strong>Building the Frontend</strong></h2>
<ol>
<li><p>Create a new React project:</p>
<pre><code class="lang-javascript">  npm create vite@latest appname
</code></pre>
</li>
<li><p>Install Axios for making HTTP requests:</p>
<pre><code class="lang-javascript">  npm install axios
</code></pre>
</li>
<li><p>Start the React server:</p>
<pre><code class="lang-javascript">  npm run dev
</code></pre>
</li>
<li><p>Certainly! Here's the complete code for the <strong>frontend</strong>:</p>
<p> App.js</p>
<pre><code class="lang-javascript"> <span class="hljs-keyword">import</span> { useState } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
 <span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">"axios"</span>;

 <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
   <span class="hljs-keyword">const</span> [selectedFile, setSelectedFile] = useState(<span class="hljs-literal">null</span>);
   <span class="hljs-keyword">const</span> [uploadedFileUrl, setUploadedFileUrl] = useState(<span class="hljs-string">""</span>);

   <span class="hljs-keyword">const</span> handleFileChange = <span class="hljs-function">(<span class="hljs-params">event</span>) =&gt;</span> {
     setSelectedFile(event.target.files[<span class="hljs-number">0</span>]);
   };

   <span class="hljs-keyword">const</span> handleUpload = <span class="hljs-keyword">async</span> () =&gt; {
     <span class="hljs-keyword">try</span> {
       <span class="hljs-keyword">const</span> formData = <span class="hljs-keyword">new</span> FormData();
       formData.append(<span class="hljs-string">"file"</span>, selectedFile);

       <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> axios.post(
         <span class="hljs-string">"http://localhost:5000/api/upload"</span>,
         formData,
         {
           <span class="hljs-attr">headers</span>: {
             <span class="hljs-string">"Content-Type"</span>: <span class="hljs-string">"multipart/form-data"</span>,
           },
         }
       );

       setUploadedFileUrl(response.data.fileUrl);
     } <span class="hljs-keyword">catch</span> (error) {
       <span class="hljs-built_in">console</span>.error(<span class="hljs-string">"Failed to upload file:"</span>, error);
     }
   };

   <span class="hljs-keyword">const</span> handleDownload = <span class="hljs-function">() =&gt;</span> {
     <span class="hljs-keyword">if</span> (uploadedFileUrl) {
       <span class="hljs-built_in">window</span>.open(uploadedFileUrl, <span class="hljs-string">"_blank"</span>);
     }
   };

   <span class="hljs-keyword">return</span> (
     <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"mx-auto flex flex-col justify-center items-center mt-8"</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-3xl font-bold mb-4"</span>&gt;</span>File Upload &amp; Download<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex flex-col items-start space-y-4"</span>&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">htmlFor</span>=<span class="hljs-string">"fileInput"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"font-semibold"</span>&gt;</span>
           Select a file:
         <span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">input</span>
           <span class="hljs-attr">type</span>=<span class="hljs-string">"file"</span>
           <span class="hljs-attr">id</span>=<span class="hljs-string">"fileInput"</span>
           <span class="hljs-attr">className</span>=<span class="hljs-string">"border border-gray-300 rounded p-2"</span>
           <span class="hljs-attr">onChange</span>=<span class="hljs-string">{handleFileChange}</span>
         /&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
           <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">bg-blue-500</span> <span class="hljs-attr">text-white</span> <span class="hljs-attr">py-2</span> <span class="hljs-attr">px-4</span> <span class="hljs-attr">rounded</span> ${
             !<span class="hljs-attr">selectedFile</span> ? "<span class="hljs-attr">cursor-not-allowed</span>" <span class="hljs-attr">:</span> ""
           }`}
           <span class="hljs-attr">disabled</span>=<span class="hljs-string">{!selectedFile}</span>
           <span class="hljs-attr">onClick</span>=<span class="hljs-string">{handleUpload}</span>
         &gt;</span>
           Upload
         <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
         {uploadedFileUrl &amp;&amp; (
           <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex items-center space-x-2"</span>&gt;</span>
             <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"font-semibold"</span>&gt;</span>File uploaded!<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
             <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
               <span class="hljs-attr">className</span>=<span class="hljs-string">"bg-green-500 text-white py-2 px-4 rounded"</span>
               <span class="hljs-attr">onClick</span>=<span class="hljs-string">{handleDownload}</span>
             &gt;</span>
               Download
             <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
           <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
         )}
       <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
     <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
   );
 }

 <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1691486907196/3a249518-8e1a-4b1e-8d42-cd3b986b12cf.png" alt class="image--center mx-auto" /></p>
<p> The file has been uploaded and this is the URL:</p>
<p> <a target="_blank" href="https://clubs-bucket.s3.ap-south-1.amazonaws.com/MERN.png">https://clubs-bucket.s3.ap-south-1.amazonaws.com/MERN.png</a></p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1691487138008/212c0fa3-be5e-46f9-b6db-3629972aaac9.png" alt class="image--center mx-auto" /></p>
</li>
</ol>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>Congratulations! You have successfully built a file upload and download system using AWS S3, React, and Node.js. You've learned how to set up the backend API routes, interact with AWS S3 for file storage, and create a seamless frontend experience. This system can be further enhanced with additional features, such as file validation, security measures, and advanced error handling.</p>
<p>We also encourage you to check out our GitHub repository for more code samples and projects: <a target="_blank" href="https://github.com/Prakash7078">https://github.com/Prakash7078</a></p>
<p>And don't forget to connect with us on social media to stay updated with the latest tips, tutorials, and guides:</p>
<ul>
<li>Connect with us on LinkedIn: <a target="_blank" href="https://www.linkedin.com/in/prakash7078/"><strong>prakash7078</strong></a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Deploy a React Application on AWS EC2(IAAS Service)]]></title><description><![CDATA[Amazon Elastic Compute Cloud (Amazon EC2) is a web service provided by Amazon Web Services (AWS) that allows users to run virtual machines (known as instances) in the cloud.
EC2 provides scalable computing capacity in the cloud, allowing users to eas...]]></description><link>https://chandra-prakash-babu.hashnode.dev/deploy-a-react-application-on-aws-ec2iaas-service</link><guid isPermaLink="true">https://chandra-prakash-babu.hashnode.dev/deploy-a-react-application-on-aws-ec2iaas-service</guid><category><![CDATA[ec2]]></category><category><![CDATA[EC2 instance]]></category><category><![CDATA[AWS]]></category><category><![CDATA[aws ec2]]></category><category><![CDATA[Deploy ]]></category><dc:creator><![CDATA[PONDURI CHANDRA PRAKASH BABU]]></dc:creator><pubDate>Mon, 07 Aug 2023 18:35:41 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1691429783109/44e94b1b-3fa3-4e0c-b9a8-f2a09f40ec5a.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Amazon Elastic Compute Cloud (Amazon EC2) is a web service provided by Amazon Web Services (AWS) that allows users to run virtual machines (known as instances) in the cloud.</p>
<p>EC2 provides scalable computing capacity in the cloud, allowing users to easily launch, manage, and scale virtual servers to meet their computing needs.</p>
<p><strong>It mainly consists of the capability of :</strong></p>
<ol>
<li><p>Rental virtual machines(EC2).</p>
</li>
<li><p>Storing data on virtual drivers(EBS).</p>
</li>
<li><p>Distributing load across machines.</p>
</li>
<li><p>Scaling the services using an auto-scaling group(ASG).</p>
</li>
</ol>
<p><strong>EC2 sizing and configuration options:</strong></p>
<ol>
<li><p>Operating System(OS)</p>
</li>
<li><p>Compute power and cores (CPU)</p>
</li>
<li><p>How much random access memory (RAM)</p>
</li>
<li><p>How much storage space 1. Network-attached : EBS &amp; EFS 2. hardware: EC2 instance store</p>
</li>
<li><p>Network card speed of card, public IP address</p>
</li>
<li><p>Firewall rules: Security groups</p>
</li>
<li><p>Bootstrap script: EC2 user data</p>
</li>
</ol>
<p><strong>Key features and concepts associated with AWS EC2:</strong></p>
<ol>
<li><p><strong>Instances</strong></p>
</li>
<li><p><strong>AMI (Amazon Machine Image) :</strong> An AMI is a template that contains the software configuration (operating system, application server, and applications) required to launch your instance. Search or Browse for AMIs if you don’t see what you are looking for below</p>
</li>
<li><p><strong>Instance Types:</strong> t2.micro</p>
</li>
<li><p><strong>Regions and Availability Zones</strong></p>
</li>
<li><p><strong>Security Groups and Network ACLs</strong></p>
</li>
<li><p><strong>Key Pairs :</strong> type: RSA, file-format: .pem(for use with OpenSSH</p>
</li>
<li><p><strong>Elastic IP Addresses</strong></p>
</li>
<li><p><strong>Amazon EBS (Elastic Block Store)</strong></p>
</li>
<li><p><strong>Auto Scaling</strong></p>
</li>
<li><p><strong>Load Balancing</strong></p>
</li>
<li><p><strong>Amazon CloudWatch</strong></p>
</li>
<li><p><strong>Instance Storage</strong></p>
</li>
<li><p><strong>IAM Roles</strong></p>
</li>
<li><p><strong>User Data</strong></p>
</li>
<li><p><strong>Spot Instances</strong></p>
</li>
</ol>
<p><strong>EC2 User Data:</strong></p>
<p>EC2 User data used to automate boot tasks such as :</p>
<ol>
<li><p>Installing updates</p>
</li>
<li><p>Installing software</p>
</li>
<li><p>downloading common files from the internet</p>
</li>
</ol>
<p>It runs with the root user.</p>
<p>AWS EC2 offers an exceptional platform for hosting web applications, boasting scalability, flexibility, and unwavering reliability. In this comprehensive beginner's guide, we'll lead you through the process of deploying your captivating React application on an EC2 instance within the vast landscape of Amazon Web Services.</p>
<h2 id="heading-prerequisites"><strong>Prerequisites:</strong></h2>
<ul>
<li><p>An AWS account</p>
</li>
<li><p>Installed and configured with AWS CLI</p>
</li>
<li><p>A React application that you want to deploy</p>
</li>
</ul>
<h2 id="heading-step-1-launch-an-ec2-instance"><strong>Step 1: Launch an EC2 instance</strong></h2>
<p>The first step is to launch an EC2 instance on AWS. Here's how you can do it:</p>
<ol>
<li><p>Log in to your AWS console and navigate to the EC2 service.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1691430258599/943754ea-a3d5-4a13-83db-ddc1b90c220d.png" alt class="image--center mx-auto" /></p>
</li>
<li><p>Click on the "Launch Instance" button to start the instance launch wizard.</p>
</li>
<li><p>Choose "Ubuntu Server" as the Amazon Machine Image (AMI).</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1691430477301/f64ee67c-dd88-43f1-ba49-3bddae08088e.png" alt class="image--center mx-auto" /></p>
<ol>
<li>Creating or Selecting a Key Pair for EC2 Instance:</li>
</ol>
</li>
</ol>
<p>    During the instance launch process, you will be prompted to either select an existing key pair or create a new one. A key pair is a set of public and private keys that are used to securely connect to the EC2 instance via SSH.</p>
<ol>
<li><p>If you already have a key pair, you can select it during the instance launch process. If not, you can create a new key pair by following these steps:</p>
<ol>
<li><p>In the "Configure Instance" step of the launch wizard, scroll down to the "Advanced Details" section.</p>
</li>
<li><p>Click on the "Select" button next to "Key Pair" to open the "Create a new key pair" dialog box.</p>
</li>
<li><p>Enter a name for the key pair and click on the "Create Key Pair" button.</p>
</li>
<li><p>The private key file will be downloaded automatically. Make sure to save it in a safe location on your local machine.</p>
</li>
</ol>
</li>
</ol>
<p>    Note: The private key file should be kept secure and should not be shared with anyone. You will need this key to connect to the EC2 instance via SSH.</p>
<p>    Once you have created or selected a key pair, you can continue with the instance launch process.</p>
<p>    <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1691430713394/4818a9b3-8e99-4574-8125-0b64f9094fae.png" alt class="image--center mx-auto" /></p>
<ol>
<li><p>Select an instance type and configure the instance details as per your requirements.</p>
</li>
<li><p>Review the instance details and launch the instance.</p>
</li>
</ol>
<h2 id="heading-step-2-connect-to-the-instance"><strong>Step 2: Connect to the instance</strong></h2>
<ol>
<li><p>Open a terminal window on your local machine.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1691431243000/2426cd3f-1b52-4258-bafa-5e1cb2e98381.png" alt class="image--center mx-auto" /></p>
</li>
<li><p>Use the SSH command to connect to your EC2 instance using the public IP address or DNS name of the instance.</p>
</li>
<li><p>Execute the below command in the same directory where we key(secret-key.pem) was downloaded.</p>
<pre><code class="lang-plaintext">  ssh -i &lt;path-to-private-key&gt; ubuntu@&lt;public-ip-address&gt;

  ssh -i "secret-key.pem" ubuntu@ec2-3-108-41-225.ap-south-1.compute.amazonaws.com
</code></pre>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1691431434953/5c0ec5d0-164e-4836-82bc-2eec480fa027.png" alt class="image--center mx-auto" /></p>
<p> Hurray, Now we are connected to the remote server(ec2 instance).</p>
</li>
</ol>
<h2 id="heading-step-3-install-nodejs-and-git"><strong>Step 3: Install Node.js and Git</strong></h2>
<ol>
<li><p>Now that you are connected to the instance, the next step is to install Node.js and Git. Here's how you can do it:</p>
<ol>
<li><p>Once you are connected to the instance, run the following command to update the package repository:</p>
<pre><code class="lang-plaintext">  sudo apt-get update
</code></pre>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683317632288/428575bc-03dd-4b51-b6db-50551dab04e4.png?auto=compress,format&amp;format=webp" alt /></p>
</li>
</ol>
</li>
<li><p>Install Node.js and Git using the following commands:</p>
<pre><code class="lang-plaintext">  sudo apt-get install nodejs
  sudo apt-get install npm
  sudo apt-get install git
</code></pre>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683317899852/83f4deae-0b04-40d7-bb7f-187143df5621.png?auto=compress,format&amp;format=webp" alt /></p>
</li>
</ol>
<h2 id="heading-step-4-clone-your-react-application"><strong>Step 4: Clone your React application</strong></h2>
<p>After installing Node.js and Git, the next step is to clone your React application from the Git repository. Here's how you can do it:</p>
<p>Clone your React application from the Git repository using the following command:</p>
<pre><code class="lang-plaintext">git clone https://github.com/Prakash7078/react-portfolio
</code></pre>
<p>Install the node packages:</p>
<pre><code class="lang-plaintext">npm install
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1691431909650/13bf45a9-9033-43cf-8ed6-c9c242a0c0c9.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-step-5-start-your-application"><strong>Step 5: Start your application</strong></h2>
<pre><code class="lang-plaintext">npm start
</code></pre>
<p>In your browser instead of <a target="_blank" href="http://localhost:3000"><code>localhost:3000</code></a> add this <code>3.108.41.225:3000</code></p>
<p>Click on <strong>security groups</strong>, then go-to <strong>inbound rules</strong> by clicking launch_wizard and add one like below and include the port in it, this is required.</p>
<p><strong>Inbound rules</strong> in Security Groups for EC2 are a set of network access rules that control incoming traffic to your EC2 instances. They allow you to specify which protocols, ports, and IP addresses are allowed to connect to your instances.</p>
<p>So with the public address for instance by placing your port number at the end you can access your application anywhere.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1691432472149/32b32f3b-1f3f-4a38-a4d9-53dc5a77ee5e.png" alt class="image--center mx-auto" /></p>
<p>The project is deployed on AWS 🎉</p>
<p>When you restart your machine the IP Address is going to be changed. So this IP Address might change. So for this, we have a service called Elastic IPs.</p>
<p>In this, we will create a permanent IP Address for our application. Click on Allocate Elastic IP address. Keep it by default and click Allocate.</p>
<p>After that, we have to associate with our EC2 instance by selecting your ec2 instance.</p>
<p>And now we can see our application with a fixed elastic IP address with the port number.</p>
<p>Don't forget to terminate that instance after work, or else you will be billed.</p>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>In conclusion, deploying a React application on AWS EC2 is an excellent way to showcase your web application to the world. With the scalable infrastructure and flexibility provided by AWS, you can ensure that your application is available and performing well for your users.</p>
<p>In this beginner's guide, we have walked through the step-by-step process of launching an Ubuntu EC2 instance, installing Node.js and Git, cloning your React application, and finally, testing your application. By following these instructions, you are now equipped to deploy your own React applications on AWS EC2.</p>
]]></content:encoded></item><item><title><![CDATA[Supervised Machine learning]]></title><description><![CDATA[Introduction
Supervised learning is a type of machine learning where the algorithm learns from labeled training data to make predictions or decisions on unseen data. In supervised learning, the model is provided with input-output pairs during the tra...]]></description><link>https://chandra-prakash-babu.hashnode.dev/supervised-machine-learning</link><guid isPermaLink="true">https://chandra-prakash-babu.hashnode.dev/supervised-machine-learning</guid><category><![CDATA[ML]]></category><category><![CDATA[Supervised learning]]></category><category><![CDATA[linearregression]]></category><dc:creator><![CDATA[PONDURI CHANDRA PRAKASH BABU]]></dc:creator><pubDate>Mon, 07 Aug 2023 16:54:09 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1690543672492/dbd8ca1c-fc42-45f1-9356-1613e12563d8.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-introduction"><strong>Introduction</strong></h3>
<p>Supervised learning is a type of machine learning where the algorithm learns from labeled training data to make predictions or decisions on unseen data. In supervised learning, the model is provided with input-output pairs during the training phase, and it learns to map inputs to the corresponding outputs. The goal is to approximate the underlying mapping function so that the model can accurately predict the output for new, unseen inputs.</p>
<h3 id="heading-steps-in-supervised-learning">Steps in Supervised Learning</h3>
<ol>
<li><p><strong>Data Collection:</strong> Gather a dataset with labeled examples, where the inputs (features) and their corresponding outputs (labels) are known.</p>
</li>
<li><p><strong>Data Preprocessing:</strong> Clean, transform, and preprocess the data to ensure it is in a suitable format for training the model.</p>
</li>
<li><p><strong>Model Selection:</strong> Choose an appropriate model or algorithm for the specific problem. The choice of model depends on the nature of the data and the task at hand (e.g., regression, classification).</p>
</li>
<li><p><strong>Model Training:</strong> Feed the labeled training data into the chosen model to learn the underlying patterns and relationships between inputs and outputs.</p>
</li>
<li><p><strong>Model Evaluation:</strong> Assess the performance of the trained model on a separate validation or test dataset. Common evaluation metrics include accuracy, precision, recall, F1 score, mean squared error (MSE), etc.</p>
</li>
<li><p><strong>Model Tuning:</strong> Adjust hyperparameters and model configurations to improve the model's performance on the validation set.</p>
</li>
<li><p><strong>Model Deployment:</strong> Once the model meets the desired performance, deploy it to make predictions on new, unseen data.</p>
</li>
</ol>
<h3 id="heading-types-of-algorithms">Types Of Algorithms:</h3>
<ol>
<li><p><strong>Regression</strong>: It addresses the problems like evaluating customer behavior analyzing market trends and implementing predictions. It consists of variables inputs and outputs. The input may be multiple parameters. But the result will be in one parameter.</p>
<p> <strong>Examples:</strong> include predicting house prices, stock prices, or a person's age based on various features.</p>
</li>
<li><p><strong>Classification:</strong> In classification tasks, the goal is to assign input data into specific categories or classes. The model learns decision boundaries to separate different classes.</p>
<p> <strong>Examples</strong>: include email spam detection, image recognition (identifying objects in images), and sentiment analysis.</p>
</li>
</ol>
<h3 id="heading-algorithms">Algorithms</h3>
<ol>
<li><h3 id="heading-linear-regressionregression">Linear Regression(Regression)</h3>
<p> Linear Regression is a simple and widely used supervised learning algorithm for solving regression tasks. It models the relationship between a dependent variable (also called the target or output variable) and one or more independent variables (also called predictors or features) by fitting a linear equation to the observed data.</p>
<p> <code>y = b0 + b1 * x</code></p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1690546159139/6ac2ff19-86df-4551-be24-1f4877feee55.png" alt class="image--center mx-auto" /></p>
<p>You can view the working of the algorithm from here <a target="_blank" href="https://github.com/Prakash7078/MLProjects/tree/main/LinearRegression">LinearRegression</a></p>
<p><strong>Applications of LinearRegression:</strong></p>
<p>1. Predicting house prices based on features like area, number of bedrooms, etc.</p>
<p>2. Estimating sales based on advertising expenditure.</p>
<p>3. Forecasting future stock prices based on historical data.</p>
<p>4. Predicting a student's test score based on study hours.</p>
<p>5. Analyzing the relationship between two continuous variables.</p>
<ol>
<li><h3 id="heading-logistic-regressionclassification"><strong>Logistic Regression(Classification)</strong></h3>
<p> Logistic Regression is a popular supervised learning algorithm used for binary classification tasks. Despite its name, it is a classification algorithm and not a regression algorithm. It is especially effective when the target variable is binary, meaning it has only two classes or categories.</p>
<p> <strong>Key characteristics and aspects of logistic regression include:</strong></p>
<p> 1. <strong>Sigmoid Function</strong>: Logistic regression uses a sigmoid (logistic) function to transform the linear combination of input features into a probability score between 0 and 1. The sigmoid function "squashes" the input values and maps them to a probability range.</p>
<p> 2. <strong>Decision Boundary</strong>: The decision boundary is a threshold value that separates the two classes. It is determined by the sigmoid function output, and data points with a probability score above the threshold are classified as one class, while those below are classified as the other class.</p>
<p> 3. <strong>Training Objective</strong>: Logistic regression is trained using optimization techniques to find the best model parameters that minimize a loss function, often the cross-entropy loss.</p>
</li>
</ol>
<p><strong>Applications of logistic regression include:</strong></p>
<p>1. <strong>Email Spam Detection</strong></p>
<p>2. <strong>Medical Diagnosis</strong></p>
<p>3. <strong>Sentiment Analysis</strong></p>
<p>Logistic regression is still widely used for specific applications in which the problem is well-suited to its strengths. However, in many cases, more advanced machine learning algorithms, including deep learning models have surpassed logistic regression in terms of performance and flexibility.</p>
<p><strong>When to use logistic regression:</strong></p>
<p>1. <strong>Binary Classification with Linear Separability</strong>: Logistic regression is effective when the problem involves binary classification and the data can be separated reasonably well by a linear decision boundary.</p>
<p>2. <strong>Interpretability</strong>: Logistic regression provides interpretable results, making it suitable for cases where model transparency and understanding the impact of individual features are crucial.</p>
<p>3. <strong>Low-Dimensional Data</strong>: For datasets with a relatively low number of features, logistic regression can be a simple and efficient choice.</p>
<ol>
<li><h3 id="heading-decision-trees"><strong><em>Decision Trees</em></strong></h3>
<p> Decision Trees are a popular and interpretable machine learning algorithm that is still widely used today due to its simplicity and effectiveness in certain applications. While more advanced algorithms have been developed, Decision Trees continue to be valuable in specific scenarios.</p>
</li>
</ol>
<p><strong>Strengths of Decision Trees:</strong></p>
<p>1. <strong>Interpretability:</strong> Decision Trees provide easily interpretable models, represented as a hierarchical structure of if-else conditions, making it easy to understand the decision-making process.</p>
<ol>
<li><strong>Handling Non-linearity:</strong> Decision Trees can handle non-linear relationships in the data, and they do not require the data to be preprocessed or transformed into a linear form.</li>
</ol>
<p>3. <strong>Feature Importance</strong></p>
<p>4. <strong>Scalability:</strong> Decision Trees can handle large datasets and can be efficiently trained on both small and large-scale problems.</p>
<p>Decision Trees are still valuable for their interpretability and non-linear modeling capabilities. However, more advanced algorithms and ensemble methods like Random Forests and GBMs often provide better predictive performance and are preferred choices for complex and high-dimensional data. Deep learning models, in particular, have become the state-of-the-art in various fields, especially computer vision and natural language processing, where they have demonstrated remarkable performance and outpaced traditional algorithms like Decision Trees.</p>
]]></content:encoded></item><item><title><![CDATA[Deploying React With Docker]]></title><description><![CDATA[Introduction
Before we begin, there are a few prerequisites that you should have installed on your machine. First, you'll need to have Docker installed. If you don't already have Docker installed, you can download it from the Docker website.
You'll a...]]></description><link>https://chandra-prakash-babu.hashnode.dev/deploying-react-with-docker</link><guid isPermaLink="true">https://chandra-prakash-babu.hashnode.dev/deploying-react-with-docker</guid><category><![CDATA[Docker]]></category><category><![CDATA[React]]></category><category><![CDATA[Open Source]]></category><category><![CDATA[Devops]]></category><dc:creator><![CDATA[PONDURI CHANDRA PRAKASH BABU]]></dc:creator><pubDate>Sun, 16 Jul 2023 05:04:04 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1689482190976/c3113649-36ae-4b83-a517-c34db892eaa9.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction"><strong>Introduction</strong></h2>
<p>Before we begin, there are a few prerequisites that you should have installed on your machine. First, you'll need to have Docker installed. If you don't already have Docker installed, you can download it from the Docker website.</p>
<p>You'll also need to have Node.js and npm installed. If you don't have them installed, you can download them from the Node.js website.</p>
<p>To verify that Docker is installed, open your terminal and run the following command:</p>
<pre><code class="lang-plaintext">docker --version
</code></pre>
<p>This will display the version of Docker installed on your machine. If Docker is not installed, you will receive an error message.</p>
<p>React is a popular JavaScript library for building web applications. It provides a powerful and flexible framework for building user interfaces and is widely used by developers around the world. Deploying a React application can be a complex process, but Docker can simplify the process and make it easier to manage.</p>
<p>In this guide, we will walk you through the process of deploying a React web application with Docker. We'll start by creating a new React application using <code>create-react-app</code> and then we'll create a <code>Dockerfile</code> for our application. We'll then build a Docker image and run a Docker container to test our application. Finally, we'll push our Docker image to a container registry and deploy it to our production environment.</p>
<p>By the end of this guide, you'll have a better understanding of how to deploy a React application with Docker and be able to manage your application more efficiently.</p>
<h2 id="heading-create-a-react-application"><strong>Create a React Application</strong></h2>
<p>The first step is to create a new React application using <code>create-react-app</code> command. This command will set up a new React project with some default configuration files, dependencies, and a basic application structure.</p>
<pre><code class="lang-javascript">npx create-react-app react-docker
cd react-docker
</code></pre>
<p>To run this react application execute the below command in your terminal:</p>
<pre><code class="lang-javascript">npm run start
</code></pre>
<h2 id="heading-create-a-dockerfile"><strong>Create a Dockerfile</strong></h2>
<p>The next step is to create a Dockerfile for our React application. A Dockerfile is a text file that contains instructions for building a Docker image.</p>
<p>In your terminal, navigate to the root directory of your React application and create a new file named <code>Dockerfile</code>.</p>
<p>Open the <code>Dockerfile</code> in your code editor and add the following contents:</p>
<pre><code class="lang-javascript"># Use an official Node runtime <span class="hljs-keyword">as</span> a parent image
FROM node:<span class="hljs-number">16</span>

# <span class="hljs-built_in">Set</span> the working directory to /app
WORKDIR /app

# Copy the package.json and package-lock.json to the working directory
COPY ./package*.json ./

# Install the dependencies
RUN npm install

# Copy the remaining application files to the working directory
COPY . .

# Build the application
RUN npm run build

# Expose port <span class="hljs-number">3000</span> <span class="hljs-keyword">for</span> the application
EXPOSE <span class="hljs-number">3000</span>

# Start the application
CMD [ <span class="hljs-string">"npm"</span>, <span class="hljs-string">"run"</span>, <span class="hljs-string">"start"</span> ]
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1689483006958/07d9e57a-cadf-403d-90f8-49240ff967ca.jpeg" alt class="image--center mx-auto" /></p>
<h2 id="heading-building-a-docker-image"><strong>Building a Docker Image</strong></h2>
<p>Now that we have a Dockerfile for our React application, we can build a Docker image.</p>
<p>Create a new file named <code>.dockerignore</code> in the root directory of your React application and add the following line:</p>
<pre><code class="lang-javascript">node_modules
</code></pre>
<p>In your terminal, navigate to the root directory of your React application and run the following command:</p>
<pre><code class="lang-javascript">docker build -t react-application .
</code></pre>
<p>This command builds a Docker image with the tag <code>react-application</code> using the <code>.</code> to specify the build context as the current directory.</p>
<p>The build process may take a few minutes to complete, depending on the size of your application and the speed of your internet connection.</p>
<p>Once the build is complete, you should see a message indicating that the image was successfully built.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1689483175317/030c2911-bb90-4238-8e66-847bfb07cc88.jpeg" alt class="image--center mx-auto" /></p>
<p>You can see image by doing this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1680942701739/0e4f9cce-9826-4d29-bbb7-a05b0e1a37e4.png?auto=compress,format&amp;format=webp" alt /></p>
<h2 id="heading-running-the-docker-container"><strong>Running the Docker Container</strong></h2>
<p>Now that we have a Docker image for our React application, we can run a Docker container.</p>
<p>In your terminal, run the following command to start a Docker container based on the image we just built:</p>
<pre><code class="lang-javascript">docker run -d -p <span class="hljs-number">3000</span>:<span class="hljs-number">3000</span> react-application
</code></pre>
<p>This command starts a Docker container and maps port 3000 from the container to port 3000 on your local machine. The <code>react-application</code> parameter specifies the name of the Docker image that we want to run.</p>
<p>With the <code>-d</code> flag, the command returns the ID of the container once it's started.</p>
<p>You can also use the <code>docker ps</code> command to see a list of running containers, including the ID of the container running your React application.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1680944158733/90854071-ac6f-4203-88d7-fc98a0fb87c6.png?auto=compress,format&amp;format=webp" alt /></p>
<p>Once the container is running, you should be able to view your React application by navigating to <a target="_blank" href="http://localhost:3000"><code>http://localhost:3000</code></a> in your web browser.</p>
<p>Congratulations! You have now successfully deployed your React web application with Docker.</p>
<p>To stop the container, you can use the <code>docker stop</code> command followed by the container ID</p>
<pre><code class="lang-javascript">docker stop &lt;container_id&gt;
docker stop <span class="hljs-number">004e442</span>ec862
</code></pre>
<h2 id="heading-pushing-the-docker-image-to-a-registry"><strong>Pushing the Docker Image to a Registry</strong></h2>
<p>If you want to share your Docker image with others or deploy it to a production environment, you can push it to a Docker registry such as Docker Hub or Amazon ECR.</p>
<p>Here's how to push your Docker image to Docker Hub:</p>
<ol>
<li><p>First, make sure you have a Docker Hub account. If you don't have one, you can sign up for free at <a target="_blank" href="https://hub.docker.com/signup"><strong>hub.docker.com/signup</strong></a><strong>.</strong></p>
</li>
<li><p>Log in to Docker Hub using the <code>docker login</code> command in your terminal:</p>
<pre><code class="lang-javascript">  docker login
</code></pre>
</li>
<li><p>Enter your Docker Hub username and password when prompted.</p>
</li>
<li><p>Tag your Docker image with your Docker Hub username and the name of the repository that you want to push the image to:</p>
<pre><code class="lang-javascript">  docker tag react-application &lt;your_dockerhub_username&gt;/<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">repository_name</span>&gt;</span>
 //docker tag react-application chandraprakashbabu/react-application</span>
</code></pre>
</li>
<li><p>Push your Docker image to Docker Hub using the <code>docker push</code> command:</p>
<pre><code class="lang-javascript">  docker push &lt;your_dockerhub_username&gt;/<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">repository_name</span>&gt;</span>
 //docker push chandraprakashbabu/react-application</span>
</code></pre>
<p> This command uploads your Docker image to Docker Hub, making it available for others to pull and use.</p>
<p> Now you can see Docker image in your account of docker hub.</p>
<ol>
<li><h2 id="heading-pulling-and-running-the-docker-image"><strong>Pulling and Running the Docker Image</strong></h2>
<p> To pull the Docker image that you pushed to a registry, you can use the <code>docker pull</code> command followed by the name of the repository and tag:</p>
<pre><code class="lang-javascript"> docker pull &lt;your_dockerhub_username&gt;/&lt;repository_name&gt;:&lt;tag&gt;
 docker pull chandraprakashbabu/react-application
</code></pre>
<p> Once the Docker image is downloaded, you can run it using the <code>docker run</code> command as we did earlier.</p>
<pre><code class="lang-javascript"> docker run -d -p <span class="hljs-number">3000</span>:<span class="hljs-number">3000</span> &lt;your_dockerhub_username&gt;/&lt;repository_name&gt;:&lt;tag&gt;
 docker run -d -p 3000:3000 chandraprakashbabu/react-application:latest
</code></pre>
<p> This will start a new Docker container based on the image you just pulled from the registry. You can access your React application by navigating to <a target="_blank" href="http://localhost:3000"><code>http://localhost:3000</code></a> in your web browser.</p>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p> In this guide, we have learned how to deploy a React web application with Docker. We started by creating a Dockerfile to package our application code and dependencies into a Docker image. Then, we built and ran the Docker container to verify that our application is running correctly.</p>
<p> We also covered how to bind the container port to the host port to make the application accessible from the outside. We discussed how to run the container in detached mode and how to push the Docker image to a registry like Docker Hub.</p>
<p> By following this guide, you can easily package and deploy your React application with Docker, making it easier to share and deploy to different environments.</p>
<p> By following the steps in this guide, you now have a solid understanding of how to deploy a React web application with Docker. We hope this guide has been helpful to you and that you are now ready to start deploying your own React applications with Docker. If you have any questions or feedback, feel free to leave a comment below. Happy deploying!</p>
</li>
</ol>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[A Beginner's Guide to Solidity]]></title><description><![CDATA[Introduction
Welcome to the world of Solidity and smart contract development on the Ethereum blockchain! Whether you're new to programming or have prior experience, this guide will serve as your comprehensive introduction to the basics of Solidity an...]]></description><link>https://chandra-prakash-babu.hashnode.dev/a-beginners-guide-to-solidity</link><guid isPermaLink="true">https://chandra-prakash-babu.hashnode.dev/a-beginners-guide-to-solidity</guid><category><![CDATA[Solidity]]></category><category><![CDATA[blog]]></category><category><![CDATA[Blockchain]]></category><dc:creator><![CDATA[PONDURI CHANDRA PRAKASH BABU]]></dc:creator><pubDate>Sat, 15 Jul 2023 18:32:09 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1689433968034/69f2063d-2e8a-4ea8-a384-2dce96903872.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction"><strong>Introduction</strong></h2>
<p>Welcome to the world of Solidity and smart contract development on the Ethereum blockchain! Whether you're new to programming or have prior experience, this guide will serve as your comprehensive introduction to the basics of Solidity and how to create smart contracts. We will start with the fundamental concepts and build upon them to give you a solid understanding of the language.</p>
<h2 id="heading-what-is-solidity"><strong>What is Solidity?</strong></h2>
<p>Solidity is a contract-oriented, high-level programming language used for writing smart contracts on the Ethereum blockchain. It was developed to enable developers to create decentralized applications (dApps) and automate processes on the Ethereum network.</p>
<p>Smart contracts are self-executing agreements with the terms of the agreement between buyer and seller being directly written into lines of code. They provide a secure, transparent, and tamper-proof way of executing transactions on the blockchain.</p>
<h2 id="heading-data-types-in-solidity"><strong>Data Types in Solidity</strong></h2>
<p>Solidity supports several data types that can be used to store and manipulate values in smart contracts.</p>
<p>Here are some of the most commonly used data types in Solidity with examples:</p>
<ol>
<li><p><code>bool</code>: Represents a boolean value, either <code>true</code> or <code>false</code>.</p>
<p> Example: <code>bool isTrue = true;</code></p>
</li>
<li><p><code>int</code>: Represents a signed integer value.</p>
<p> Example: <code>int age = 25;</code></p>
</li>
<li><p><code>uint</code>: Represents an unsigned integer value.</p>
<p> Example: <code>uint balance = 100;</code></p>
</li>
<li><p><code>address</code>: Represents an Ethereum address.</p>
<p> Example: <code>address owner = 0x123456789abcdef0123456789abcdef0123456789;</code></p>
</li>
<li><p><code>string</code>: Represents a string of Unicode characters.</p>
<p> Example: <code>string name = "Nawaz";</code></p>
</li>
<li><p><code>bytes</code>: Represents a dynamic-length byte array.</p>
<p> Example: <code>bytes32 hash = 0x123456789abcdef0123456789abcdef0123456789;</code></p>
</li>
<li><p><code>enum</code>: Represents a named set of values.</p>
<p> Example:</p>
<pre><code class="lang-solidity">  <span class="hljs-keyword">enum</span> <span class="hljs-title">Color</span> {Red, Green, Blue}
  Color myColor <span class="hljs-operator">=</span> Color.Red;
</code></pre>
</li>
<li><p><code>mapping</code>: Represents an unordered collection of key-value pairs.</p>
<p> Example:</p>
<pre><code class="lang-solidity"> <span class="hljs-keyword">mapping</span>(<span class="hljs-keyword">address</span> <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> <span class="hljs-keyword">uint</span>) balances;
  balances[owner] <span class="hljs-operator">=</span> <span class="hljs-number">100</span>;
</code></pre>
</li>
<li><p><code>array</code>: Represents an ordered collection of values of a specified type.</p>
<p> Example:</p>
<pre><code class="lang-solidity"> <span class="hljs-keyword">uint</span>[] numbers;
  numbers.<span class="hljs-built_in">push</span>(<span class="hljs-number">1</span>);
  numbers.<span class="hljs-built_in">push</span>(<span class="hljs-number">2</span>);
</code></pre>
<p> These are the most commonly used data types in Solidity, but there are many more types and features available for you to use. It's important to choose the right data type for your use case to ensure the efficiency and security of your smart contract.</p>
</li>
</ol>
<h2 id="heading-variables-in-solidity"><strong>Variables in Solidity</strong></h2>
<p>Variables in Solidity are used to store values in your smart contract.</p>
<p>In Solidity, there are 3 types of variables: state, local, and global variables.</p>
<ol>
<li><p><strong>State Variables:</strong> State variables are declared at the contract level and are stored on the blockchain, making them available to all functions in the contract.</p>
<pre><code class="lang-solidity">  <span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
  <span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

  <span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
    <span class="hljs-keyword">uint</span> <span class="hljs-keyword">public</span> balance;

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setBalance</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> newBalance</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
      balance <span class="hljs-operator">=</span> newBalance;
    }
  }
</code></pre>
<p> In this example, <code>balance</code> is a state variable declared as <code>public</code>, meaning it can be accessed from outside the contract. The <code>setBalance</code> function allows a caller to set the value of <code>balance</code>. Since <code>balance</code> is a state variable and is available to all functions in the contract.</p>
</li>
<li><p><strong>Local Variables:</strong> Local variables are declared within a function and are stored in memory. They are automatically cleared when the function returns and are only accessible within the function. Local variables are also referred to as "temporary" or "stack" variables.</p>
<pre><code class="lang-solidity">  <span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
  <span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

  <span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">calculateSum</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> a, <span class="hljs-keyword">uint</span> b</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
      <span class="hljs-keyword">uint</span> sum;
      sum <span class="hljs-operator">=</span> a <span class="hljs-operator">+</span> b;
      <span class="hljs-keyword">return</span> sum;
    }
  }
</code></pre>
</li>
</ol>
<p>In this example, <code>sum</code> is a local variable declared within the <code>calculateSum</code> function. It is used to store the result of adding <code>a</code> and <code>b</code> and is returned as the function's output. Since <code>sum</code> is a local variable, it is stored in memory and will be cleared when the function returns.</p>
<ol>
<li><p><strong>Global Variables:</strong> Global variables are variables declared in a global context, outside of any contract or function. These variables are shared by all contracts in the same source code file and are accessible from anywhere in the code. Global variables are rare in Solidity and are generally not recommended for use in smart contracts.</p>
<pre><code class="lang-solidity"> <span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
  <span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

  <span class="hljs-keyword">uint</span> globalCount;

  <span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">incrementGlobalCount</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
      globalCount<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
    }
  }
</code></pre>
<p> In this example, <code>globalCount</code> is a global variable declared outside of any contract or function. This variable is accessible from anywhere in the code and can be shared by multiple contracts in the same source code file. The <code>incrementGlobalCount</code> function in the <code>HelloWorld</code> contract increments the value of <code>globalCount</code> by one.</p>
</li>
</ol>
<p>In general, it is recommended to use state or local variables instead of global variables, unless there is a specific requirement that can only be met by using a global variable.</p>
<h2 id="heading-operators-and-expressions"><strong>Operators and Expressions</strong></h2>
<p><strong>Operators:</strong> Operators are symbols that perform specific operations on variables and values. There are various types of operators in Solidity, including:</p>
<ul>
<li><p>Arithmetic operators: +, -, *, /, %</p>
</li>
<li><p>Comparison operators: ==, !=, &gt;, &gt;=, &lt;, &lt;=</p>
</li>
<li><p>Logical operators: &amp;&amp;, ||, !</p>
</li>
<li><p>Bitwise operators: &amp;, |, ^, ~, &lt;&lt;, &gt;&gt;​</p>
</li>
<li><p>Assignment operator: =</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="lang-solidity"><span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-keyword">uint</span> a <span class="hljs-operator">=</span> <span class="hljs-number">2</span>;
  <span class="hljs-keyword">uint</span> b <span class="hljs-operator">=</span> <span class="hljs-number">3</span>;
  <span class="hljs-keyword">uint</span> c;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">performOperations</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
    c <span class="hljs-operator">=</span> a <span class="hljs-operator">+</span> b;
    c <span class="hljs-operator">=</span> c <span class="hljs-operator">*</span> <span class="hljs-number">2</span>;
    <span class="hljs-keyword">return</span> c;
  }
}
</code></pre>
<p>In this example, the <code>performOperations</code> function uses arithmetic and assignment operators to perform operations on variables <code>a</code>, <code>b</code>, and <code>c</code>. The function returns the value of <code>c</code> after performing the operations, which is equal to <code>(a + b) * 2 = (2 + 3) * 2 = 10</code>.</p>
<p><strong>Expressions:</strong> Expressions are combinations of variables, values, and operators that evaluate to a single value. Expressions are used in various parts of Solidity code, including conditional statements, loops, and function return values.</p>
<p>Example:</p>
<pre><code class="lang-solidity"><span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-keyword">uint</span> a <span class="hljs-operator">=</span> <span class="hljs-number">2</span>;
  <span class="hljs-keyword">uint</span> b <span class="hljs-operator">=</span> <span class="hljs-number">3</span>;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkGreater</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">bool</span></span>) </span>{
    <span class="hljs-keyword">return</span> a <span class="hljs-operator">&gt;</span> b;
  }
}
</code></pre>
<p>In this example, the <code>checkGreater</code> function returns a Boolean value that is the result of a comparison expression <code>a &gt; b</code>. The expression evaluates to <code>false</code> since <code>a</code> (2) is not greater than <code>b</code> (3).</p>
<h2 id="heading-conditional-statements"><strong>Conditional Statements</strong></h2>
<p>Conditional statements are used to execute a block of code only if a specified condition is true. The two main types of conditional statements in Solidity are <code>if</code> statements and <code>switch</code> statements.</p>
<p>Here's an example of an <code>if</code> statement in Solidity:</p>
<pre><code class="lang-solidity"><span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-keyword">uint</span> value;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setValue</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> _value</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
    value <span class="hljs-operator">=</span> _value;
    <span class="hljs-keyword">if</span> (value <span class="hljs-operator">&gt;</span> <span class="hljs-number">100</span>) {
      <span class="hljs-comment">// code to execute if value is greater than 100</span>
    }
  }
}
</code></pre>
<p>In this example, the <code>if</code> statement checks if the <code>value</code> is greater than 100. If the condition is true, the code inside the <code>if</code> statement will be executed.</p>
<p>Here's an example of a <code>switch</code> statement in Solidity:</p>
<pre><code class="lang-solidity"><span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-keyword">uint</span> value;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setValue</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> _value</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
    value <span class="hljs-operator">=</span> _value;
    switch (value) {
      case <span class="hljs-number">10</span>:
        <span class="hljs-comment">// code to execute if value is equal to 10</span>
        <span class="hljs-keyword">break</span>;
      case <span class="hljs-number">20</span>:
        <span class="hljs-comment">// code to execute if value is equal to 20</span>
        <span class="hljs-keyword">break</span>;
      default:
        <span class="hljs-comment">// code to execute if value does not match any case</span>
    }
  }
}
</code></pre>
<p>In this example, the <code>switch</code> statement checks the value of <code>value</code> and executes the code inside the matching case. If no case matches, the code inside the <code>default</code> case will be executed.</p>
<h2 id="heading-loops"><strong>Loops</strong></h2>
<p>Loops are used to repeat a block of code a specified number of times. The two main types of loops in Solidity are <code>for</code> loops and <code>while</code> loops.</p>
<p>Here's an example of a <code>for</code> loop in Solidity:</p>
<pre><code class="lang-solidity"><span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-keyword">uint</span> value;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setValue</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> _value</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">uint</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&lt;</span> _value; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>) {
      value<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
    }
  }
}
</code></pre>
<p>In this example, the <code>for</code> loop repeats the code inside the loop <code>_value</code> times. The loop starts with <code>i = 0</code>, continues as long as <code>i &lt; _value</code>, and increments <code>i</code> by 1 each time.</p>
<p>Here's an example of a <code>while</code> loop in Solidity:</p>
<pre><code class="lang-solidity"><span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-keyword">uint</span> value;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setValue</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> _value</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
    <span class="hljs-keyword">while</span> (value <span class="hljs-operator">&lt;</span> _value) {
      value<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
    }
  }
}
</code></pre>
<p>In this example, the <code>while</code> loop repeats the code inside the loop as long as <code>value &lt; _value</code>. The loop will continue to execute until <code>value</code> is equal to or greater than <code>_value</code>.</p>
<h2 id="heading-functions"><strong>Functions</strong></h2>
<p>Functions are blocks of code that perform a specific task. Functions in Solidity can take input parameters, perform operations, and return output values. Functions can be called from within the same contract or from other contracts.</p>
<p>Example:</p>
<pre><code class="lang-solidity"><span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-keyword">uint</span> a <span class="hljs-operator">=</span> <span class="hljs-number">2</span>;
  <span class="hljs-keyword">uint</span> b <span class="hljs-operator">=</span> <span class="hljs-number">3</span>;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addNumbers</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> c, <span class="hljs-keyword">uint</span> d</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
    <span class="hljs-keyword">return</span> c <span class="hljs-operator">+</span> d;
  }
}
</code></pre>
<p>In this example, the <code>addNumbers</code> function takes two input parameters <code>c</code> and <code>d</code> and returns their sum. The function can be called from within the contract or from other contracts to perform the addition operation.</p>
<h2 id="heading-view-and-pure"><strong>View and Pure</strong></h2>
<p>In Solidity, <code>view</code> and <code>pure</code> are two keywords used to define the state-read and state-write access of functions.</p>
<p><strong>view:</strong> Functions marked as <code>view</code> can only read the state, but cannot modify it. They do not modify the state, and therefore do not require any gas to be executed.</p>
<p>Example:</p>
<pre><code class="lang-solidity"><span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-keyword">uint</span> a <span class="hljs-operator">=</span> <span class="hljs-number">2</span>;
  <span class="hljs-keyword">uint</span> b <span class="hljs-operator">=</span> <span class="hljs-number">3</span>;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getSum</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
    <span class="hljs-keyword">return</span> a <span class="hljs-operator">+</span> b;
  }
}
</code></pre>
<p>In this example, the <code>getSum</code> function is marked as a <code>view</code> function. It only reads the values of <code>a</code> and <code>b</code> and returns their sum. Since it does not modify the state, it can be safely executed without consuming any gas.</p>
<p><strong>pure:</strong> Functions marked as <code>pure</code> can only read the state and input parameters, but cannot modify the state or access contract storage. They do not modify the state and do not interact with contract storage, and therefore do not require any gas to be executed.</p>
<p>Example:</p>
<pre><code class="lang-solidity"><span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-keyword">uint</span> a <span class="hljs-operator">=</span> <span class="hljs-number">2</span>;
  <span class="hljs-keyword">uint</span> b <span class="hljs-operator">=</span> <span class="hljs-number">3</span>;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getProduct</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> c</span>) <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
    <span class="hljs-keyword">return</span> c <span class="hljs-operator">*</span> (a <span class="hljs-operator">+</span> b);
  }
}
</code></pre>
<p>In this example, the <code>getProduct</code> function is marked as a <code>pure</code> function. It only reads the input parameter <code>c</code> and the values of <code>a</code> and <code>b</code>. It returns the product of <code>c</code> and the sum of <code>a</code> and <code>b</code>. Since it does not modify the state or access contract storage, it can be safely executed without consuming any gas.</p>
<p>Using <code>view</code> and <code>pure</code> functions can help optimize the gas consumption and performance of your contract, and can also increase the transparency and trust of your code by allowing users to verify its behavior without affecting the state.</p>
<h2 id="heading-access-modifiers"><strong>Access Modifiers</strong></h2>
<p>In Solidity, access modifiers are used to control the visibility and accessibility of functions and state variables. Solidity supports 4 access modifiers: <code>public</code>, <code>private</code>, <code>internal</code>, and <code>external</code>.</p>
<ul>
<li><p><code>public</code>: A <code>public</code> function or state variable is visible and accessible from outside the contract and can be called or read by anyone.</p>
</li>
<li><p><code>private</code>: A <code>private</code> function or state variable is only visible and accessible within the contract and cannot be called or read from outside the contract.</p>
</li>
<li><p><code>internal</code>: An <code>internal</code> function or state variable is visible and accessible within the contract and its derived contracts but not from outside the contract.</p>
</li>
<li><p><code>external</code>: An <code>external</code> function is a function that can be called from outside the contract but cannot be called from within the contract.</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="lang-solidity"><span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-keyword">uint</span> <span class="hljs-keyword">public</span> counter <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;
  <span class="hljs-keyword">uint</span> <span class="hljs-keyword">private</span> secretCounter <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">incrementCounter</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
    counter<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
    secretCounter<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getCounter</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
    <span class="hljs-keyword">return</span> counter;
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">incrementSecretCounter</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">private</span></span> </span>{
    secretCounter<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
  }
}
</code></pre>
<p>In this example, a state variable <code>counter</code> is declared as <code>public</code> and another state variable <code>secretCounter</code> is declared as <code>private</code>. The <code>incrementCounter</code> function increments both the <code>counter</code> and <code>secretCounter</code>. The <code>getCounter</code> function returns the value of <code>counter</code> and is declared as <code>public</code> so it can be called from outside the contract. The <code>incrementSecretCounter</code> function increments the <code>secretCounter</code> and is declared as <code>private</code> so it can only be called from within the contract.</p>
<h2 id="heading-constructor"><strong>Constructor</strong></h2>
<p>A constructor in Solidity is a special type of function that is automatically executed when a smart contract is deployed to the blockchain. It is used to initialize the state of a contract, set default values for variables, and perform any other setup tasks that are required before the contract can be used.</p>
<p>A constructor is defined using the <code>constructor</code> keyword, and is called only once when the contract is deployed.</p>
<p>Example:</p>
<pre><code class="lang-solidity"><span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-keyword">uint</span> a;

  <span class="hljs-function"><span class="hljs-keyword">constructor</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> _a</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
    a <span class="hljs-operator">=</span> _a;
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getA</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
    <span class="hljs-keyword">return</span> a;
  }
}
</code></pre>
<p>In this example, the <code>constructor</code> function takes an input parameter <code>_a</code> and sets the value of the <code>a</code> state variable to this value. The constructor is called only once when the contract is deployed, and the value of <code>a</code> is set at this time. The <code>getA</code> function is used to retrieve the value of <code>a</code> and return it to the caller.</p>
<h2 id="heading-storage-and-memory">Storage and Memory</h2>
<p>In Solidity, <code>storage</code> and <code>memory</code> are two locations used to store data in a smart contract. They are used to keep track of the state of the contract and to pass data between functions.</p>
<p><strong>storage:</strong> The <code>storage</code> location is a persistent data storage that is part of the contract's state. Values stored in <code>storage</code> persist between function calls and are stored on the blockchain, making them available to all users of the contract.</p>
<p>Example:</p>
<pre><code class="lang-solidity"><span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-keyword">uint</span> a;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setA</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> _a</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
    a <span class="hljs-operator">=</span> _a;
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getA</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
    <span class="hljs-keyword">return</span> a;
  }
}
</code></pre>
<p>In this example, the <code>a</code>variable is stored in <code>storage</code>. The <code>setA</code> function takes an input parameter <code>_a</code> and sets the value of <code>a</code> to this value. The value of <code>a</code> is stored in <code>storage</code> and persists between function calls, allowing it to be retrieved later using the <code>getA</code> function.</p>
<p><strong>memory:</strong> The <code>memory</code> location is a temporary data storage that is used to store data that is only needed within the scope of a single function. Values stored in <code>memory</code> are lost when the function returns and are not stored on the blockchain.</p>
<p>Example:</p>
<pre><code class="lang-solidity"><span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-keyword">uint</span> a;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setA</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> _a</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
    <span class="hljs-keyword">uint</span> b <span class="hljs-operator">=</span> _a <span class="hljs-operator">*</span> <span class="hljs-number">2</span>;
    a <span class="hljs-operator">=</span> b;
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getA</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
    <span class="hljs-keyword">return</span> a;
  }
}
</code></pre>
<p>In this example, the <code>b</code> variable is stored in <code>memory</code>. The <code>setA</code> function takes an input parameter <code>_a</code> and calculates a new value <code>b</code> as the double of <code>_a</code>. The value of <code>b</code> is stored in <code>memory</code> and used to set the value of <code>a</code>. The value of <code>b</code> is not stored on the blockchain and is lost when the function returns.</p>
<h2 id="heading-arrays">Arrays</h2>
<p>In Solidity, arrays are used to store multiple values of a specified data type. There are two types of arrays: <code>static arrays</code> and <code>dynamic arrays</code>.</p>
<ol>
<li><p><strong>Static Arrays:</strong> Static arrays have a fixed size, meaning the number of elements in the array is set when the array is declared and cannot be changed during the life of the contract.</p>
<p> Example:</p>
<pre><code class="lang-solidity">  <span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
  <span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

  <span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
    <span class="hljs-keyword">uint</span>[<span class="hljs-number">3</span>] array;

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setArray</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> _a, <span class="hljs-keyword">uint</span> _b, <span class="hljs-keyword">uint</span> _c</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
      array[<span class="hljs-number">0</span>] <span class="hljs-operator">=</span> _a;
      array[<span class="hljs-number">1</span>] <span class="hljs-operator">=</span> _b;
      array[<span class="hljs-number">2</span>] <span class="hljs-operator">=</span> _c;
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getArray</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> index</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
      <span class="hljs-keyword">return</span> array[index];
    }
  }
</code></pre>
<p> In this example, a static array <code>array</code> with 3 elements is declared. The <code>setArray</code> function takes 3 input parameters <code>_a</code>, <code>_b</code>, and <code>_c</code> and sets the values of <code>array[0]</code>, <code>array[1]</code>, and <code>array[2]</code> to these values, respectively. The <code>getArray</code> function takes an input parameter <code>index</code> and returns the value of <code>array[index]</code>.</p>
</li>
<li><p><strong>Dynamic arrays:</strong> Dynamic arrays have a variable size, meaning the number of elements in the array can be changed during the life of the contract. Dynamic arrays are declared using the <code>[]</code> operator.</p>
<p> Example:</p>
<pre><code class="lang-solidity"> <span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
  <span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

  <span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
    <span class="hljs-keyword">uint</span>[] array;

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addElement</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> element</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
      array.<span class="hljs-built_in">push</span>(element);
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getArrayLength</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
      <span class="hljs-keyword">return</span> array.<span class="hljs-built_in">length</span>;
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getArrayElement</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> index</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
      <span class="hljs-keyword">return</span> array[index];
    }
  }
</code></pre>
<p> In this example, a dynamic array <code>array</code> is declared. The <code>addElement</code> function takes an input parameter <code>element</code> and adds this value to the end of the array using the <code>push</code> method. The <code>getArrayLength</code> function returns the length of the array using the <code>length</code> property. The <code>getArrayElement</code> function takes an input parameter <code>index</code> and returns the value of <code>array[index]</code>.</p>
</li>
</ol>
<h2 id="heading-mappings"><strong>Mappings</strong></h2>
<p>In Solidity, mappings are used to store key-value pairs. The keys and values can be of any data type. Mappings are declared using the <code>mapping</code> keyword.</p>
<p>Example:</p>
<pre><code class="lang-solidity"><span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-keyword">mapping</span>(<span class="hljs-keyword">address</span> <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> <span class="hljs-keyword">uint</span>) balances;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addBalance</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> _address, <span class="hljs-keyword">uint</span> _balance</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
    balances[_address] <span class="hljs-operator">=</span> _balance;
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getBalance</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> _address</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
    <span class="hljs-keyword">return</span> balances[_address];
  }
}
</code></pre>
<p>In this example, a mapping <code>balances</code> is declared with <code>address</code> as the key and <code>uint</code> as the value. The <code>addBalance</code> function takes two input parameters <code>_address</code> and <code>_balance</code> and sets the value of <code>balances[_address]</code> to <code>_balance</code>. The <code>getBalance</code> function takes an input parameter <code>_address</code> and returns the value of <code>balances[_address]</code>.</p>
<h2 id="heading-structs"><strong>Structs</strong></h2>
<p>In Solidity, structs are used to define custom data types. Structs can contain multiple data types, including other structs. Structs are declared using the <code>struct</code> keyword.</p>
<p>Example:</p>
<pre><code class="lang-solidity"><span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-keyword">struct</span> <span class="hljs-title">Person</span> {
    <span class="hljs-keyword">string</span> name;
    <span class="hljs-keyword">uint</span> age;
    <span class="hljs-keyword">address</span> <span class="hljs-keyword">address</span>;
  }

  <span class="hljs-keyword">mapping</span>(<span class="hljs-keyword">address</span> <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> Person) people;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addPerson</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> _address, <span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _name, <span class="hljs-keyword">uint</span> _age</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
    people[_address].<span class="hljs-built_in">name</span> <span class="hljs-operator">=</span> _name;
    people[_address].age <span class="hljs-operator">=</span> _age;
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getPerson</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> _address</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span>, <span class="hljs-keyword">uint</span></span>) </span>{
    <span class="hljs-keyword">return</span> (people[_address].<span class="hljs-built_in">name</span>, people[_address].age);
  }
}
</code></pre>
<p>In this example, a struct <code>Person</code> is declared with 3 data types: <code>string</code>, <code>uint</code>, and <code>address</code>. A mapping <code>people</code> is declared with <code>address</code> as the key and <code>Person</code> as the value. The <code>addPerson</code> function takes 3 input parameters <code>_address</code>, <code>_name</code>, and <code>_age</code> and sets the values of <code>people[_address].name</code> to <code>_name</code> and <code>people[_address].age</code> to <code>_age</code>. The <code>getPerson</code> function takes an input parameter <code>_address</code> and returns the values of <code>people[_address].name</code> and <code>people[_address].age</code>.</p>
<h2 id="heading-enums"><strong>Enums</strong></h2>
<p>In Solidity, enums are used to define a set of named constants. Enums can be used to model a set of possible states or values for a particular field in a contract. Enums are declared using the <code>enum</code> keyword.</p>
<p>Example:</p>
<pre><code class="lang-solidity"><span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-keyword">enum</span> <span class="hljs-title">ActionType</span> { BUY, SELL, HOLD }

  ActionType <span class="hljs-keyword">public</span> currentAction;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setAction</span>(<span class="hljs-params">ActionType _action</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
    currentAction <span class="hljs-operator">=</span> _action;
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getAction</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params">ActionType</span>) </span>{
    <span class="hljs-keyword">return</span> currentAction;
  }
}
</code></pre>
<p>In this example, an enum <code>ActionType</code> is declared with 3 constants: <code>BUY</code>, <code>SELL</code>, and <code>HOLD</code>. A state variable <code>currentAction</code> of type <code>ActionType</code> is declared and public. The <code>setAction</code> function takes an input parameter <code>_action</code> of type <code>ActionType</code> and sets the value of <code>currentAction</code> to <code>_action</code>. The <code>getAction</code> function returns the value of <code>currentAction</code>.</p>
<h2 id="heading-events">Events</h2>
<p>In Solidity, events are used to emit logs that can be read from outside the contract. Events are declared using the <code>event</code> keyword.</p>
<p>Example:</p>
<pre><code class="lang-solidity"><span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">event</span> <span class="hljs-title">NewPerson</span>(<span class="hljs-params"><span class="hljs-keyword">string</span> name, <span class="hljs-keyword">uint</span> age</span>)</span>;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addPerson</span>(<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _name, <span class="hljs-keyword">uint</span> _age</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
    <span class="hljs-keyword">emit</span> NewPerson(_name, _age);
  }
}
</code></pre>
<p>In this example, an event <code>NewPerson</code> is declared with 2 parameters: <code>name</code> and <code>age</code>. The <code>addPerson</code> function takes 2 input parameters <code>_name</code> and <code>_age</code> and emits the event <code>NewPerson</code> with these parameters. The event can be read from outside the contract using an Ethereum client like Remix, Geth, or Parity.</p>
<h2 id="heading-payable">Payable</h2>
<p>In Solidity, the <code>payable</code> keyword is used to indicate that a function can receive Ether (the cryptocurrency of the Ethereum network). To make a function <code>payable</code>, simply add the <code>payable</code> keyword before the function definition.</p>
<p>Example:</p>
<pre><code class="lang-solidity"><span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">HelloWorld</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">deposit</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">payable</span></span> <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
    <span class="hljs-comment">// code to handle deposit</span>
  }
}
</code></pre>
<p>In this example, the <code>deposit</code> function is declared as <code>payable</code> which means it can receive Ether. When this function is called, the caller can send Ether along with the function call, and the contract will receive the Ether. It's important to note that not all functions in a contract need to be <code>payable</code>. Functions that do not need to receive Ether do not need the <code>payable</code> keyword.</p>
<h2 id="heading-object-oriented-programming-oop">Object-oriented programming (OOP)</h2>
<p>Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code that operates on the data. Solidity is a contract-oriented language and does not have full OOP support. However, Solidity does support some OOP concepts such as inheritance, encapsulation, and polymorphism.</p>
<ul>
<li>Inheritance: Solidity supports single inheritance, meaning a contract can inherit from a single base contract. The derived contract inherits the functions, state variables, and events of the base contract.</li>
</ul>
<p>Example:</p>
<pre><code class="lang-solidity"><span class="hljs-comment">// SPDX-License-Identifier: GPL-3.0</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> &gt;=0.4.16 &lt;0.9.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">Base</span> </span>{
  <span class="hljs-keyword">uint</span> <span class="hljs-keyword">public</span> counter <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">incrementCounter</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
    counter<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
  }
}

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">Derived</span> <span class="hljs-keyword">is</span> <span class="hljs-title">Base</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">incrementCounterTwice</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
    incrementCounter();
    incrementCounter();
  }
}
</code></pre>
<p>In this example, the <code>Derived</code> contract inherits from the <code>Base</code> contract. The <code>Derived</code> contract has access to the <code>counter</code> state variable and the <code>incrementCounter</code> function defined in the <code>Base</code> contract. The <code>Derived</code> contract also defines its own <code>incrementCounterTwice</code> function.</p>
<ul>
<li><p>Encapsulation: Solidity supports encapsulation through the use of access modifiers. Access modifiers control the visibility and accessibility of state variables and functions. This allows you to hide internal implementation details and restrict access to certain functions and state variables.</p>
</li>
<li><p>Polymorphism: Polymorphism is not explicitly supported in Solidity, but similar functionality can be achieved through function overloading and function overriding. Function overloading is not supported in Solidity, but function overriding is supported through inheritance. In inheritance, a derived contract can override a function defined in the base contract to provide a different implementation.</p>
</li>
</ul>
<p>Overall, while Solidity is not a fully object-oriented language, it does support some OOP concepts that can be used to write more organized and maintainable code.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In conclusion, we have covered the basics of Solidity, a contract-oriented programming language used to write decentralized applications on the Ethereum platform. We discussed setting up your development environment, creating a "Hello World" program, data types and variables, operators and expressions, functions and modifiers, events, and more. By learning Solidity, you are taking the first step towards creating your own decentralized applications and contributing to the growth of the Ethereum ecosystem.</p>
<p>To continue your journey with Solidity, I recommend exploring advanced topics such as smart contract testing, security, and optimization. There are many resources available online, including the Solidity documentation, forums, and online courses, to help you deepen your knowledge and skills in this exciting field.</p>
<p>Stay updated on the latest developments in the world of Solidity and Ethereum. Follow me on <a target="_blank" href="https://www.linkedin.com/in/prakash7078/"><strong>LinkedIn</strong></a> for the latest tips, resources, and exciting projects in the decentralized space!</p>
]]></content:encoded></item></channel></rss>