<?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: guachilimbo</title>
    <description>The latest articles on DEV Community by guachilimbo (@guachilimbo).</description>
    <link>https://dev.to/guachilimbo</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%2F738495%2Feab1a6a7-9ea7-4a94-834f-f8c2e49977c7.png</url>
      <title>DEV Community: guachilimbo</title>
      <link>https://dev.to/guachilimbo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/guachilimbo"/>
    <language>en</language>
    <item>
      <title>Using Binary Search Tree and Trie data structures to store and retrieve a Movie Catalogue</title>
      <dc:creator>guachilimbo</dc:creator>
      <pubDate>Thu, 27 Jan 2022 19:25:17 +0000</pubDate>
      <link>https://dev.to/guachilimbo/using-binary-search-tree-and-trie-data-structures-to-store-and-retrieve-a-movie-catalogue-56lh</link>
      <guid>https://dev.to/guachilimbo/using-binary-search-tree-and-trie-data-structures-to-store-and-retrieve-a-movie-catalogue-56lh</guid>
      <description>&lt;p&gt;I created this code to have a go at data structure and algorithms on Python after taking the data structure &amp;amp; algorithms module in Codecademy.  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;GitHub link to code: &lt;a href="https://github.com/guachilimbo/movie_recommendation"&gt;https://github.com/guachilimbo/movie_recommendation&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Data Structures Used
&lt;/h2&gt;

&lt;p&gt;The program uses a database of the top 250 movies on IMDb which I found in Kaggle (&lt;a href="https://www.kaggle.com/ramjasmaurya/top-250s-in-imdb"&gt;https://www.kaggle.com/ramjasmaurya/top-250s-in-imdb&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;It stores this information in two different data structures: A Binary Search Tree, and a Trie. &lt;/p&gt;

&lt;h3&gt;
  
  
  Binary Search Tree
&lt;/h3&gt;

&lt;p&gt;When the data base is read, each movie entry is stored as a Binary Search Tree (BST). The tree is sorted alphabetically depending on the title of each movie. This allows for quick storage and retrieval with a running time performance of &lt;code&gt;O(logN)&lt;/code&gt;, where N is the number of levels in the tree.&lt;/p&gt;

&lt;p&gt;However, in order to implement an autocomplete functionality, I thought this was not a good data structure, since for a given prefix &lt;code&gt;(str)&lt;/code&gt; of length M, the runtime to find all the movies that match the string will be &lt;code&gt;O(MlogN)&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This is why as the data was read from the database, a &lt;code&gt;Trie()&lt;/code&gt; structure containing the titles of each movie is created. &lt;/p&gt;

&lt;h3&gt;
  
  
  Trie
&lt;/h3&gt;

&lt;p&gt;There are M + 1 &lt;code&gt;Trie()&lt;/code&gt; classes created in this program, where M is the number of unique genres detected in the database. This allows to apply the autocomplete function to all the catalogue, or just the movies within an user chosen genre. &lt;/p&gt;

&lt;p&gt;The nice thing about applying the autocomplete function, which searches all movies that start with a prefix input by the user, to a Trie is that search complexities are brought to optimal limit, this is, the prefix length. &lt;/p&gt;

&lt;h2&gt;
  
  
  Program Execution:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Download folder and run &lt;code&gt;python main.py&lt;/code&gt; on the command line.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gpP7XQVW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l91rc2qie5cius42pl9l.png" alt="First step of running the program." width="880" height="154"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The program will ask for choice between two running modes:&lt;/p&gt;

&lt;p&gt;a) &lt;strong&gt;Genre search&lt;/strong&gt;: The program will display the genres found in the data base. The user will then choose one of the genres. The films that belong to this category will be displayed. The user will then be asked to enter part of the title of one of those films and a autocomplete method will be invoked on that string. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--361t13kv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lkz6vebafj44qgerglao.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--361t13kv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lkz6vebafj44qgerglao.gif" alt="Genre choice" width="880" height="480"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;b) &lt;strong&gt;General search&lt;/strong&gt;: The user will be asked to input the beginning of a movie title and the autocomplete method will be called on that string. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TkWhBJrc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/60bdtskk4vaoj9eempx1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TkWhBJrc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/60bdtskk4vaoj9eempx1.gif" alt="Autocomplete function" width="880" height="478"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If a match is found, the movie's Title, Year, Genre and Duration will be displayed alongside the option to display a description of the movie. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Cqa80A35--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oxwg2wox8g5ilulayhmt.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Cqa80A35--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oxwg2wox8g5ilulayhmt.gif" alt="Movie card display" width="880" height="478"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Perform another search or exit the program.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;Please any suggestions or improvements are greatly appreciated since this is my first time dealing with data structures and algorithms!&lt;/p&gt;

</description>
      <category>python</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Battleship!... On Command Line</title>
      <dc:creator>guachilimbo</dc:creator>
      <pubDate>Sat, 30 Oct 2021 17:11:35 +0000</pubDate>
      <link>https://dev.to/guachilimbo/battleship-on-command-line-57fh</link>
      <guid>https://dev.to/guachilimbo/battleship-on-command-line-57fh</guid>
      <description>&lt;p&gt;As part of the course I am taking to improve my programming skills, I have decided to code a Battleship game to be played using the command line. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Github link to code: &lt;a href="https://github.com/guachilimbo/battleship"&gt;https://github.com/guachilimbo/battleship&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;What is Battleship?&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R2dcnOGE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ytxpnj5grkywc43afkvo.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R2dcnOGE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ytxpnj5grkywc43afkvo.jpg" alt="Battleship Game box" width="646" height="324"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Battleship is a two player board game where the objective is to sink all of your opponent's ships. Each turn, the players will "bomb" a grid cell (e.g. "C7"). If a boat is occupying the cell, the boat will be "hit". Otherwise it will be a "miss". &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Game set up&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ApAQCGBA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/poz760bdh0ig9hpsitjr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ApAQCGBA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/poz760bdh0ig9hpsitjr.png" alt="Game grid" width="423" height="433"&gt;&lt;/a&gt;&lt;br&gt;
For my game, I have decided to create a 10x10 alphanumerical grid. Each player will have 5 different boats with different lengths:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Carrier - 5 cells&lt;/li&gt;
&lt;li&gt;Battleship - 4 cells&lt;/li&gt;
&lt;li&gt;Cruiser - 3 cells&lt;/li&gt;
&lt;li&gt;Submarine - 3 cells&lt;/li&gt;
&lt;li&gt;Destroyer - 2 cells&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The game is played against the CPU. Upon inisialisation, the user will be given a prompt to choose the initial cell the boat will occupy, and whether you want to position the boat vertically or horizontally. This process is repeated for each boat:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8004s2LW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/klp6hblokdbkztn98jjv.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8004s2LW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/klp6hblokdbkztn98jjv.gif" alt="GIF showing game set up on command line" width="880" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The CPU board is set up using Python's random library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;columns = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
rows = list(range(10))
def random_cell(self):
  return "".join([random.choice(Play.columns),str(random.choice(Play.rows))])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Playing the game&lt;/strong&gt;&lt;br&gt;
Using a similar logic as the set up, the CPU will randomly choose a cell to bomb. If there is a boat on that cell of the player's grid, the cell will be updated with an "x" to show that the boat has been hit. Otherwise, the cell will be updated with a "~" to show the bomb missed. &lt;/p&gt;

&lt;p&gt;The player logic is the same but the user has to input the desired cell. The code will warn the user if an already bombed cell has been chosen.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uP2Yzlzc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/urafmt4itn5h42y6r2r2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uP2Yzlzc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/urafmt4itn5h42y6r2r2.gif" alt="GIF showing game play on command line" width="880" height="475"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;A list keeps track of the number of hits a boat has received. When the number of hits equals the length of the boat (i.e. number of lives), the number of boats left displayed under the grids, changes. Allowing the user to know how many (and which) boats are left. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;End game&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2mbYHe4P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c7ujj0jjl33lco2jqrbp.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2mbYHe4P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c7ujj0jjl33lco2jqrbp.gif" alt="GIF showing game end" width="880" height="475"&gt;&lt;/a&gt;&lt;br&gt;
The game end when one side has lost all of the boats. The code terminates and outputs a message to restart if the user wishes to. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Future improvements&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The user experience can be easily improved by user-proofing the code. Right now certain inputs will cause the code to crash. Wanted to move on from this project to learn more stuff, so will improve it in the future. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The game's difficulty is too easy. This is due to the computer randomly choosing a cell from a decreasing list of 100. It would be fun to implement different algorithms to increase the difficulty once I learn a bit more. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>python</category>
    </item>
  </channel>
</rss>
