<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dhruv Kumar Jha</title>
    <description>The latest articles on DEV Community by Dhruv Kumar Jha (@dhrvjha).</description>
    <link>https://dev.to/dhrvjha</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F681326%2F93a87100-6f16-43b0-9025-8074dcc372ef.jpeg</url>
      <title>DEV Community: Dhruv Kumar Jha</title>
      <link>https://dev.to/dhrvjha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dhrvjha"/>
    <language>en</language>
    <item>
      <title>Designing a Next Level Parking Lot System in Python</title>
      <dc:creator>Dhruv Kumar Jha</dc:creator>
      <pubDate>Tue, 17 May 2022 11:48:31 +0000</pubDate>
      <link>https://dev.to/dhrvjha/designing-a-next-level-parking-lot-system-in-python-37ok</link>
      <guid>https://dev.to/dhrvjha/designing-a-next-level-parking-lot-system-in-python-37ok</guid>
      <description>&lt;p&gt;Hi, today we will be building a Parking Lot System which has multiple features and supports different types of vehicles to be parked, and the most important feature will be its extendibility.&lt;br&gt;
Before designing a software lets first write down its features.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Parking Lot should have floors on which vehicles are parked.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On parking floors there can be different parking space for different types of vehicles.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A vehicle can only be parked in its own type of parking space. For example, A bike cannot be parked in parking space of a car, since that will be waste of space.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Tickets should be generated after vehicle is parked.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Later these tickets can be used to unpark or retrieve the parked vehicle.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Also for convenience a vehicle should be parked at the closest parking space possible.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are the basic features that we need to implement in our parking system, but we will need to design the software in such a way that new features can be introduced without any problem, like introducing a new vehicle type that is allowed by parking lot or a limitation on a certain type of vehicle category.&lt;/p&gt;

&lt;p&gt;The approach I am choosing for this problem is factory design pattern (I will explain later in implementation section).&lt;br&gt;
In our case vehicles and parking space are two different entity which are bound by vehicle's type.&lt;/p&gt;

&lt;p&gt;First lets start by designing parking space, I will call it slot.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Here I have made different classes for different types of parking slots, this will help us if we would later want a certain parking slot should behave different than others. For example, a truck slot can have higher ticket cost than a car or bike slot and this can be defined separately in its own class without needing to change other classes.

&lt;p&gt;Now to vehicles which will be parked in parking slot,&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
It is the same case with vehicle class too, if we would later want to add additional properties to a vehicle type than it would be lot easier to change its own class than to make a mess of if and else statements.

&lt;p&gt;How do we make sure a vehicle is parked in its own category of parking slot?&lt;br&gt;
We will use factory design pattern for that.&lt;br&gt;
What is factory design pattern?&lt;br&gt;
Well it is the highlight of this problem,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. &lt;a href="https://en.wikipedia.org/wiki/Factory_method_pattern"&gt;- Wikipedia&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So here we will create object of vehicle and map to proper slot using dictionary.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 &lt;br&gt;
&lt;code&gt;VEHICLES_MAP&lt;/code&gt; maps input string to its vehicle class and &lt;code&gt;SLOTS_MAP&lt;/code&gt; maps vehicle class to its slot class.

&lt;p&gt;&lt;code&gt;DEFAULT_FLOOR_MAP&lt;/code&gt; is location of where each type of vehicle will be store on parking floor.&lt;br&gt;
So with parking floor in discussion lets first see the floor class.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Floor covers a lot of functionalities for our parking lot. Lets start exploring each

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;park_vehicle()&lt;/code&gt; should park vehicle in the first empty slot of its type.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;unpark_vehicle()&lt;/code&gt; should take a ticket and remove the vehicle and empty the slot.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;floor_map&lt;/code&gt; is dictionary of vehicle types mapped to its starting and end location of slots on floor.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;busy_slot_map&lt;/code&gt; is a dictionary which keeps track of all the busy parking slots of a vehicle type.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By now we can add or remove a vehicle type without needing to change anything in floor, hence we have decoupled vehicles and parking slot with floor.&lt;/p&gt;

&lt;p&gt;At last we need to design parking lot which will be made up of floors.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;park_vehicle()&lt;/code&gt; will park a vehicle in the first empty floor it will find.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;unpark_vehicle()&lt;/code&gt; will remove vehicle from floor and close the ticket.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Interesting part is that &lt;code&gt;park_vehicle()&lt;/code&gt; of parking lot finds the first empty floor and &lt;code&gt;park_vehicle()&lt;/code&gt; finds the first empty slot to park.&lt;br&gt;
Also &lt;code&gt;ParkingLot&lt;/code&gt; is decoupled from the entire parking system it just manages floors and that is how it should be. No feature or entity should be least dependent on others.&lt;/p&gt;

&lt;p&gt;Now about Tickets which I skipped.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Static variable of class is property of a class not of an instance. I use this property to manage all the Ticket instances.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;tickets&lt;/code&gt; is static variable of type dictionary which will store all the instances created.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;generateTicket()&lt;/code&gt; generates a string based on parking lot, floor and slot the vehicle is parked in.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;openTicket()&lt;/code&gt; adds ticket to &lt;code&gt;tickets&lt;/code&gt; variable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;getTicket()&lt;/code&gt; is class method which returns the Ticket instance if the ticket string passed is in &lt;code&gt;tickets&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;closeTicket()&lt;/code&gt; removes ticket from &lt;code&gt;tickets&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are the basic features that a ticket manager needs to have,&lt;br&gt;
being able to open, close and find a ticket.&lt;/p&gt;

&lt;p&gt;Up to this point we have created a parking system which parks and unparks a vehicle at the closest point and generates a ticket.&lt;/p&gt;

&lt;p&gt;Taking the input and managing Parking Lot does not have a lot of new things to discuss so I am skipping that part but here is the &lt;a href="https://github.com/dhrvjha/ParkingLotSystem"&gt;link&lt;/a&gt; to source which takes input from console.&lt;/p&gt;

&lt;p&gt;Thank you for reading till the end, I am new to writing articles so let me know about the mistakes that I made.&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Best way to LeetCode</title>
      <dc:creator>Dhruv Kumar Jha</dc:creator>
      <pubDate>Sun, 27 Mar 2022 07:25:02 +0000</pubDate>
      <link>https://dev.to/dhrvjha/best-way-to-leetcode-28pn</link>
      <guid>https://dev.to/dhrvjha/best-way-to-leetcode-28pn</guid>
      <description>&lt;p&gt;Getting ready for algorithmic questions for interview requires a lot of time and dedication. The questions are tough and often makes you give up.&lt;/p&gt;

&lt;p&gt;Two months ago a reddit user &lt;a href="https://www.reddit.com/user/newgradsmackindab/"&gt;u/newgradsmackindab&lt;/a&gt; made a &lt;a href="https://www.reddit.com/r/cscareerquestions/comments/sgktuv/the_definitive_way_on_how_to_leetcode_properly/"&gt;post&lt;/a&gt; describing a solution to this problem.&lt;/p&gt;

&lt;p&gt;I recommend going through his post and to starting making notes of every new thing that you learn &lt;strong&gt;it helps&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>career</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
