<?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: Tommy</title>
    <description>The latest articles on DEV Community by Tommy (@attommy98).</description>
    <link>https://dev.to/attommy98</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F537694%2Fcc34a287-1c0b-49f7-b111-2fd04c4ac858.png</url>
      <title>DEV Community: Tommy</title>
      <link>https://dev.to/attommy98</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/attommy98"/>
    <language>en</language>
    <item>
      <title>CS50 Week 6 - Readability in Python </title>
      <dc:creator>Tommy</dc:creator>
      <pubDate>Thu, 18 Mar 2021 12:57:46 +0000</pubDate>
      <link>https://dev.to/attommy98/cs50-week-6-readability-in-python-49an</link>
      <guid>https://dev.to/attommy98/cs50-week-6-readability-in-python-49an</guid>
      <description>&lt;h3&gt;
  
  
  The Problem
&lt;/h3&gt;

&lt;p&gt;The task here is to write some code in Python that will determine the "grade" (or reading level) of a passage of text using a calculation called the Cole-Liauman index.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Solution
&lt;/h3&gt;

&lt;p&gt;First things first, we're going to want to import the &lt;strong&gt;get_string&lt;/strong&gt; function from cs50 and also import the library &lt;strong&gt;string&lt;/strong&gt; from Python. This will be useful later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from cs50 import get_string
import string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We want to get the user input and declare variables. Words is declared as one because we count spaces to find how many words there are in a passage of text, but of course it would be unusual to have a space before the first word so the amount of words in some text will be amount of spaces + 1, generally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Get user input
sentence = get_string("Text: ")

# Declare variables
letters = 0
words = 1
sentences = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To find how many sentences there are in our text we can just check to see if the current letter in the sentence is an exclamation mark, question mark or full stop.&lt;br&gt;
If the letter is any other punctuation (such as a comma), we don't want to increment anything so we can just continue. We do this so that when it reaches the &lt;strong&gt;else&lt;/strong&gt; block, our program won't count a comma as a letter.&lt;br&gt;
To find the amount of words we can count the spaces as mentioned before.&lt;br&gt;
Finally, the else block because anything else is going to be a letter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Loop through string, identifying each letter
for letter in sentence:
    if letter == "!" or letter == "?" or letter == ".":
        sentences += 1
    elif letter in string.punctuation:
        continue
    elif letter in string.whitespace:
        words += 1
    else:
        letters += 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We first need to find the &lt;strong&gt;word factor&lt;/strong&gt; before we do any calculations. This is important because it allows us to find how many letters and sentences there are &lt;strong&gt;per 100 words&lt;/strong&gt;.&lt;br&gt;
The Coleman-Liau index calculation is rounded to an integer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Calculations for liau index
wordFactor = words / 100
lettersPer100 = letters / wordFactor
sentencesPer100 = sentences / wordFactor
liauIndex = round((0.0588 * lettersPer100) - (0.296 * sentencesPer100) - 15.8)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we just have to print the grade based on what the Coleman-Liau index is! Above 16 we print "Grade 16+", below 1 is "Before Grade 1" and anything else is going to be whatever the grade is so we can just inject the Coleman-Liau value into a string that says "Grade {liauIndex}".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Print correct grade based on liau indx
if liauIndex &amp;gt; 16:
    print("Grade 16+")
elif liauIndex &amp;lt; 1:
    print("Before Grade 1")
else:
    print(f"Grade {liauIndex}")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Jobs a goodun!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>CS50 Week 6 - Cash in Python (Less Comfortable)</title>
      <dc:creator>Tommy</dc:creator>
      <pubDate>Wed, 17 Mar 2021 12:58:27 +0000</pubDate>
      <link>https://dev.to/attommy98/cs50-week-6-cash-in-python-less-comfortable-bpi</link>
      <guid>https://dev.to/attommy98/cs50-week-6-cash-in-python-less-comfortable-bpi</guid>
      <description>&lt;h3&gt;
  
  
  The Problem
&lt;/h3&gt;

&lt;p&gt;The user will input some amount of change and we have to take away the largest posssible coin (¢1, ¢5, ¢10, ¢25) until there is no change left and return how many coins it took to reach 0.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Solution
&lt;/h3&gt;

&lt;p&gt;Firstly we're going to need the get_float function from cs50.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from cs50 import get_float
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we need to make sure the number being input by the user is valid which we will need a variable for. We also need a variable to track how many coins have been used so I decided to declare these first&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Declare variables
change = 0
count = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make sure the input is valid, I used a while loop to check if &lt;strong&gt;change&lt;/strong&gt; is greater than 0. If not, simply re-assign the variable with another prompt. After the input I rounded the value and multiplied by 100.&lt;br&gt;
This makes sure that the input is always possible to work with. If it was 0.00001 for example, multiplying this by 100 would give you 0.001 which is not a possible coin. Rounding it would round to 0 in this case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# While change is NAN, prompt user
while change &amp;lt;= 0:
    change = get_float("Change owed: ")
    cents = round(change * 100)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking back I probably could've put the &lt;code&gt;cents = round(change * 100)&lt;/code&gt; after the while loop because it's just making the computer do unnecessary work. Oh well ¯_(ツ)_/¯&lt;/p&gt;

&lt;p&gt;All we need to do now is check if cents is greater than the coin values, if it is, take away the value from &lt;strong&gt;cents&lt;/strong&gt; and increment the count.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while cents &amp;gt; 0:
    if cents &amp;gt;= 25:
        cents = cents - 25
        count = count + 1
    elif cents &amp;gt;= 10:
        cents = cents - 10
        count = count + 1
    elif cents &amp;gt;= 5:
        cents = cents - 5
        count = count + 1
    elif cents &amp;gt;= 1:
        cents = cents - 1
        count = count + 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'm 99% sure the order is important here. We want to check if the largest coin is possible &lt;strong&gt;FIRST&lt;/strong&gt; before checking the others.&lt;/p&gt;

&lt;p&gt;Once this is done we can simply print the number of coins!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(count)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Full code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from cs50 import get_float

# Declare variables
change = 0
count = 0

# While change is NAN, prompt user
while change &amp;lt;= 0:
    change = get_float("Change owed: ")
    cents = round(change * 100)

# While cents is greater than 0, take away largest coin possible  and increment count
while cents &amp;gt; 0:
    if cents &amp;gt;= 25:
        cents = cents - 25
        count = count + 1
    elif cents &amp;gt;= 10:
        cents = cents - 10
        count = count + 1
    elif cents &amp;gt;= 5:
        cents = cents - 5
        count = count + 1
    elif cents &amp;gt;= 1:
        cents = cents - 1
        count = count + 1

print(count)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>cs50</category>
      <category>cash</category>
    </item>
    <item>
      <title>CS50 Week 6 - Mario in Python (More Comfortable)</title>
      <dc:creator>Tommy</dc:creator>
      <pubDate>Tue, 16 Mar 2021 20:44:01 +0000</pubDate>
      <link>https://dev.to/attommy98/cs50-week-6-mario-in-python-more-comfortable-3b4b</link>
      <guid>https://dev.to/attommy98/cs50-week-6-mario-in-python-more-comfortable-3b4b</guid>
      <description>&lt;p&gt;I've been wanting to start writing how I solve problems for a while, and the best time to start was yesterday, the second best time is now. So here we go.&lt;/p&gt;

&lt;p&gt;If you're familiar with CS50 and the Mario problem then you probably understand what I was trying to do, but here's a brief overview.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;The task was to print a pyramid of height specified by the user. The correct amount of whitespace, then the correct number of # blocks on each line, followed by a space, then the same amount of # blocks after the space creating something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  # #
 ## ## 
### ###
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;To tackle this problem, I first used the &lt;strong&gt;get_int&lt;/strong&gt; function from the cs50 library. Save me having to typecast things. I like convenience!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;from cs50 import get_int&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then we needed to make sure that the height is valid, and if it isn't we need to prompt the user again until a correct value is entered.&lt;/p&gt;

&lt;p&gt;I decided to define the variable height first. Then I could check if the value was correct, and if not re-prompt the user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;height = 0
while height &amp;gt; 8 or height &amp;lt; 1:
    height = get_int("Height: ")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All good so far but the next part was the toughest. We now have the height of the pyramid that will reject non-numeric heights and re-prompt if the height is invalid. Now to print the correct pyramid.&lt;/p&gt;

&lt;p&gt;I tried a few solutions but I landed on this one because it made the most sense to me.&lt;/p&gt;

&lt;p&gt;First, we need to enter a loop for each row of the pyramid&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(1, height + 1):
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I started at 1 and ended at height + 1 so that &lt;strong&gt;the value of i is equal to the row number&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Then, enter another loop for each column.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(1, height + 1):
    for j in range(1, height + 1):
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to print the correct amount of spaces before printing any #'s. I decided to do this by checking to see &lt;strong&gt;if j (the current column) is greater than the height - i (the current row number)&lt;/strong&gt; and if it is, then Python knows it's time to start printing #'s instead of whitespace!&lt;br&gt;
This logic works because on row i, we want i amount of hashtags and height - i amount of spaces.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Loop through rows and then columns
for i in range(1, height + 1):
    for j in range(1, height + 1):
        # Start printing # after correct number of spaces
        if j &amp;gt; (height - i):
            print("#", end="")
        else:
            print(" ", end="")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;end=""&lt;/strong&gt; makes sure a new line isn't printed after each iteration.&lt;/p&gt;

&lt;p&gt;Finally, we just need to print a space and the same amount of #'s after the space which is easily done.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Print middle space and remaining #
        if j == height:
            print(" ", "#" * i, end="")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's one key part I haven't mentioned. We need to print a new line after the column loops have finished so that a new row starts. Easily fixed, just &lt;code&gt;print()&lt;/code&gt; after the j loop and we're done!&lt;/p&gt;

&lt;p&gt;Full code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from cs50 import get_int

height = 0
while height &amp;gt; 8 or height &amp;lt; 1:
    height = get_int("Height: ")

# Loop through rows and then columns
for i in range(1, height + 1):
    for j in range(1, height + 1):
        # Start printing # after correct number of spaces
        if j &amp;gt; (height - i):
            print("#", end="")
        else:
            print(" ", end="")
        # Print middle space and remaining #
        if j == height:
            print(" ", "#" * i, end="")
    print()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There you have it. On the way to Bowser's castle.&lt;/p&gt;

</description>
      <category>python</category>
      <category>cs50</category>
      <category>mario</category>
    </item>
  </channel>
</rss>
