Cs50 Pset6 2021: Hello, Mario, Cash and Readability Solution with explanation

This problem is not new though, we’ve implemented it in the past using C language, but now that we are transitioning from C programming to Python programming language, we are going to implement the Python version of the Problem.

Python is easy, so I believed the implement of these codes should not be long like C program, since there is enough module in python that will help us minimize time, so let’s dive into my solutions and how-to guides

In this blog post, I will talk about the Hello, Mario and Cash Python-based solutions, you can check out the C language-based solution here

so let’s dive in.

CS50 Hello Python Solution

In this problem set, we are to implement a simple program that asks for a name and print the name like “Hello Collins”.

Pseudocode

  • Prompt the user for their name
  • Then print the name of the user with “hello name”
name = input("What's your name?\n")
print("Hello, "+ name)

CS50 Mario Python Solution

This is where things begin to look more Pythonic, now we are going to implement a program in Python that looks like Mario steps with “#” just like the above picture

Pseudocode

  • Prompt the user for height’s number
  • Make sure the number is not greater than 8 and not less than 1
  • Iterate through the height
  • On iteration then print the Height “#”
from cs50 import get_int

while True:
    height = get_int("Height: ")
    if height > 0 and height < 9:
        break
for row in range(height):
    for space in range(height - row - 1, 0, -1):
        print(" ", end="")
    for hash in range(row + 1):
        print("#", end ="")
    print("\n", end ="")

A brief Code explanation

  • We import the Cs50 module that has the get_int function
  • Then we use the while True to make sure the program continues asking for the user’s Height if it is not of the required range (greater than 0 and less than 8)
  • then we add the break function to make sure when the user cooperates, the program should terminate move to the next execution
  • Then iterate through the height by using range
  • Then I iterate through the space in range (height – row – 1, 0, – 1), and print space. In this part, the program sets the start of the program to height – row – 1, then starting point should be 0, and it decrements the code by -1.
  • Then iterate through range(row + 1), to print “#”. Notice that end = “” simply means remove default line in python’s print function.
  • Then print(“\n”, end =””) prints new line after all process have taking place on each iteration

CS50 Mario Python more Solution

This program was meant to print two opposite super Mario steps with hash “#”. We just need to modify the first Mario problem by adding right align hashes just like the image above

Pseudocode: The same thing for Mario less, the only thing we are adding is right-aligned Hash.

from cs50 import get_int

while True:
    height = get_int("Height: ")
    if height > 0 and height < 9:
        break
for row in range(height):
    for space in range(height - row - 1, 0, -1):
        print(" ", end="")
    for hash in range(row + 1):
        print("#", end ="")
    print("  ", end="")
    for right_hash in range(row+1):
        print("#", end ="")
    print("\n", end ="")
  • we will start from where we stop the solution at Mario less Comfortable solution.
  • then print two space print(” “) and making sure the end is set to “”, in order to override print default new line
  • after then iterate another for loop (right_hash) with a row to print the right-align hash.

Cs50 Cash Python Solution

This Problem set was meant to help Sellers make a change for buyers easily, It’s design to help the cashier collate the lowest possible number of coins to help make a change for Customers.

Pseudocode

  • Prompt user for an amount of change
  • if the change is less than 0, re-prompt the user until cooperate.
  • Due to floating impression, round the cent to the nearest penny
  • Then use the largest coins possible while keeping track of coins used.
  • Print the number of coins.
from cs50 import get_float

count = 0  # to keep track of coins used
while True:
    change = get_float("Change owed: ")
    if change > 0:
        break
cent  = round(change * 100)  #round up to the nearest whole number

while cent >= 25:
    cent = cent - 25
    count += 1

while cent >= 10:
    cent = cent - 10
    count += 1

while cent >= 5:
    cent = cent - 5
    count += 1

while cent >=1:
    cent = cent - 1
    count += 1

print(count)

More explanation

  • we use The while True loop to make sure the user did not type integer less than 0
  • Then I round the cent to the nearest whole to make sure the calculation is precised
  • Then I keep checking
    • if changes available is greater than or equal to the list of available coin.
    • if so, reduce the change by the coins by substracting, thereby keeping track of the coin being used.

Cs50 Readability Python Solution

What we are told to do in this problem set is to write a Python program of the previously written “C program” that computes/takes text from the user and determines its reading level i.e like Grade 1, 2, 3, and so on.

Pseudocode

  • First Prompt user for text to check
  • Iterate through the length of the text to count
    • the number of letter in the text
    • the number of words
    • the number of sentences
  • Then implement the index calculation and round it up
  • Then print the Grade of the Text

count_letter = 0
count_word = 1
count_sentence = 0

#Collecting input from user
text = input("Text: ")
text_length = len(text)

#count the number of letters in the text
for i in range(text_length):
    if(text[i].isalpha()):
        count_letter+=1

#count the number of words in text
    if (text[i].isspace()):
        count_word +=1

#count the number of sentence
    if(text[i] == '.' or text[i] == '?' or text[i] == '!'):
        count_sentence +=1

calculation = (0.0588 * count_letter / count_word * 100) - (0.296 * count_sentence / count_word * 100) - 15.8 #to calculate Index

index = round(calculation)

if index < 1:
    print("Before Grade 1")
elif index > 16:
    print("Grade 16+")
else:
    print(f"Grade {index}")

Conclusion

While this Pset is about transitioning from the previously written version of C Pset, it has been interesting so far. The only problem I faced is understanding the loop Syntax at first, but after I’m done with the Mario solution, things start to make sense.

After finishing the Pset, I got to realize that Python is easier, requires less code compare to the C programming language.

Thanks for reading..

Any suggestion or Want to say something, Don’t hesitate to leave a Comment.

Leave a Reply