<?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: Xiu</title>
    <description>The latest articles on DEV Community by Xiu (@xiaowenyuan).</description>
    <link>https://dev.to/xiaowenyuan</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%2F664308%2Fc9ee39b6-94b6-442a-b8cb-805556d5f442.jpeg</url>
      <title>DEV Community: Xiu</title>
      <link>https://dev.to/xiaowenyuan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xiaowenyuan"/>
    <language>en</language>
    <item>
      <title>Codecademy CS102 Portfolio Project: Recommendation Program</title>
      <dc:creator>Xiu</dc:creator>
      <pubDate>Sat, 23 Oct 2021 09:47:19 +0000</pubDate>
      <link>https://dev.to/xiaowenyuan/codecademy-cs102-portfolio-project-recommendation-program-2h1</link>
      <guid>https://dev.to/xiaowenyuan/codecademy-cs102-portfolio-project-recommendation-program-2h1</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Codecademy wants us to create a "basic recommendation program" using Python for the Portfolio Project in Codecademy's CS102: Data Structures and Algorithms. The program must contain the following features:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Implementing an autocomplete that, based on a user’s input, returns a list of possible categories based on the beginning of a word. You should use the data created in the last step to create the autocomplete. It is up to you how to properly store and retrieve the data.&lt;/p&gt;

&lt;p&gt;Retrieving and displaying all of the data related to the category selected by the user. It is up to you how to properly store and retrieve the data.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I decided to make a program that recommends workout classes to a user depending on the type of workouts the user wants to do and what time they are free. &lt;/p&gt;

&lt;p&gt;The code can be found &lt;a href="https://github.com/xiaowenyuan/cs102_recommendation"&gt;on Github&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running the Program
&lt;/h2&gt;

&lt;p&gt;First, run &lt;code&gt;studios.py&lt;/code&gt;. This will randomly generate random studios and classes which will be inserted into a binary search tree (BST). The resulting tree is pickled, ready for the next step.&lt;/p&gt;

&lt;p&gt;Next, run &lt;code&gt;script.py&lt;/code&gt;. The program will prompt for both the activities the user is looking for, and the timing for said classes. The program's final payload is a list of classes from the database (if any) that satisfy the user's above conditions.  &lt;/p&gt;

&lt;h2&gt;
  
  
  The Database
&lt;/h2&gt;

&lt;p&gt;Each time &lt;code&gt;studios.py&lt;/code&gt; is run, random classes will be generated for 22 fictional studios. The studios already have a set of possible classes and the associated tags for said classes. Each class's timing, price, and instructor, however, is randomly generated each time. &lt;/p&gt;

&lt;p&gt;I used &lt;a href="https://www.fakeaddressgenerator.com/World_Address/popular_city/city/New%20York"&gt;Fake Address Generator&lt;/a&gt; to generate the addresses for the studios. &lt;a href="https://fossbytes.com/tools/random-name-generator"&gt;Fossbytes&lt;/a&gt; was also used to generate 500 random names for the instructors.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Data Structure
&lt;/h2&gt;

&lt;p&gt;A self-balancing Binary Search Tree is used to store the unique tags from the generated classes. Each tag is a node on the tree containing as payload a list of all the classes that have this tag. &lt;/p&gt;

&lt;p&gt;This BST is self-implemented for the purpose of practice. As many of the methods in this tree differ from the BST taught by Codecademy, I am incredibly grateful to online resources such as &lt;a href="https://runestone.academy/runestone/books/published/pythonds/Trees/SearchTreeImplementation.html"&gt;Runestone Academy&lt;/a&gt; and &lt;a href="https://algorithmtutor.com/Data-Structures/Tree/AVL-Trees/"&gt;Algorithm Tutor&lt;/a&gt; that help me implement them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Searching the Data Structure
&lt;/h2&gt;

&lt;p&gt;Initially, a breadth-first search was employed to look for keys that contain the search term. This resulted in a linear complexity of &lt;code&gt;O(n)&lt;/code&gt;, where n is the number of tags in the data structure. &lt;/p&gt;

&lt;p&gt;The program uses an "autocomplete" search function that should run faster as it takes advantage of the BST property. The search function has the following parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Look for the smallest term that is greater than or equal to the search term. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Traverse the tree for the next largest key until the search term no longer matches the relevant part of the search term (ie &lt;code&gt;node.key[:len(search_term)]&lt;/code&gt;). &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Return the list of nodes that contain these keys. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This search function should have a complexity of &lt;code&gt;O(log(n) + t)&lt;/code&gt;, where &lt;code&gt;n&lt;/code&gt; is the number of nodes in the tree and &lt;code&gt;t&lt;/code&gt; is the number of nodes that contain the search term. &lt;/p&gt;

</description>
      <category>python</category>
      <category>codecademy</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Codecademy CS101 Final Project: Blackjack</title>
      <dc:creator>Xiu</dc:creator>
      <pubDate>Fri, 09 Jul 2021 15:58:05 +0000</pubDate>
      <link>https://dev.to/xiaowenyuan/codecademy-cs101-final-project-blackjack-2bom</link>
      <guid>https://dev.to/xiaowenyuan/codecademy-cs101-final-project-blackjack-2bom</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;I have been taking the Codecademy CS101 course in the past month or so. Prior to this, I had no background in Python.&lt;/p&gt;

&lt;p&gt;Codecademy requires us to make a basic terminal program using Python in order to complete its CS101 course. The objectives of the project are as follows:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* Build a terminal program using Python
* Add at least one interactive feature using input()
* Use Git version control
* Use the command line and file navigation
* Write a technical blog post on the project
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Blackjack was the first suggestion listed by Codecademy in the project write-up. I was leaning towards an original idea, but my mind started wondering what kind of logic I would write for a Blackjack program. I ended up writing the code in about a day.&lt;/p&gt;

&lt;p&gt;The code can be found &lt;a href="https://github.com/xiaowenyuan/cs101_blackjack.git"&gt;here at Github&lt;/a&gt;. &lt;/p&gt;

&lt;h1&gt;
  
  
  Design
&lt;/h1&gt;

&lt;p&gt;I made a &lt;code&gt;Card&lt;/code&gt; class that takes in the following parameters for its constructor: &lt;code&gt;self, id, name, suit, value, visibility = True&lt;/code&gt;. Using a &lt;code&gt;for&lt;/code&gt; loop, I generated a deck of 13 cards for each of the four suits (Diamond, Club, Spade, and Hearts).&lt;/p&gt;

&lt;p&gt;The deck of cards is randomly shuffled using &lt;code&gt;random.sample()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When the game starts up, the user is prompted to play by typing "Hit me" into the terminal. The user's input prompts&lt;br&gt;
a &lt;code&gt;deal_card&lt;/code&gt; function that pops the first card of the deck into the user's hands. &lt;/p&gt;

&lt;p&gt;The user has two options from then on: to continue receiving cards ("Hit me") or to stand ("stand"). &lt;/p&gt;

&lt;h2&gt;
  
  
  Adding the Dealer
&lt;/h2&gt;

&lt;p&gt;After writing the basic logic of the game for a single user, I added a Dealer, which is played by the computer. The Dealer will be dealt a single card after the player's initial choice to be hit. The next card the Dealer receives is the Hole--it is hidden from the Player. Only at the end of the game does the Dealer reveal the Hole.&lt;/p&gt;

&lt;p&gt;The logic behind the Dealer's decision to deal to itself or to stand is very simple--no card counting involved! The Dealer simply calculates if its hand is equal to or more than 18--if so, the Dealer stands.&lt;/p&gt;

&lt;h1&gt;
  
  
  The Rules of the Game
&lt;/h1&gt;

&lt;p&gt;I personally have never played "official" Blackjack played in casinos. For the purposes of this game, I have stuck to the following very simple rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The user/Dealer is dealt a card, and can choose to continue playing and having a card dealt to them, or stand, which holds on to the current hand. &lt;/li&gt;
&lt;li&gt;The value of the card follows the number on the card (ie 8 of hearts has a value of 9=8, and 5 of spades have a value of 5). Face cards (Jack, Queen, King) all have a value of 10. Ace has a value of either 1 or 11.&lt;/li&gt;
&lt;li&gt;If the value of the user's or Dealer's hand sums up to 21, the user or Dealer wins.&lt;/li&gt;
&lt;li&gt;If both players stand, the player with the bigger hand value wins.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Future improvements
&lt;/h1&gt;

&lt;p&gt;There are many different outcomes that currently are somewhat clumsily rendered in a dozen or so &lt;code&gt;if&lt;/code&gt; statements. This should be refactored. &lt;/p&gt;

&lt;p&gt;It would be great to have a more intelligent AI for the Dealer. This is however out-of-scope in my study syllabus for now and the near-future. &lt;/p&gt;

</description>
      <category>codecademy</category>
      <category>python</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
