Answers to CS50x 2019 Problem Set 6: Python.
Bleep.py
from cs50 import get_string
from sys import argv
import sys
def main():
# Only receive two inputs.
if len(argv) != 2:
print("Usage: python bleep.py dictionary")
exit(1)
txt_file = argv[1]
words = set()
file = open(txt_file, "r")
# we use readlines() to split the file into a list where each element is a line
for line in file.readlines():
words.add(line.rstrip("\n"))
file.close()
# Get user input and convert to a list
s = get_string("What message would you like to censor? \n")
word_list = s.split()
for i in range(len(word_list)):
if word_list[i].lower() in words:
word_list[i] = '*' * len(word_list[i])
print(" ".join(word_list))
if __name__ == "__main__":
main()
Caesar.py
if len(argv) != 2:
print("Usage: ./caesar key")
k = int(argv[1])
s = get_string("plaintext: ")
print("ciphertext: ", end="")
for c in s:
if ord(c)>64 and ord(c)<91:
j = ord(c) + k
while j > 90:
j = j - 90 + 64
print(f"{chr(j)}", end="")
elif ord(c)>96 and ord(c)<123:
j = ord(c) + k
while j > 122:
j = j - 122 + 96
print(f"{chr(j)}", end="")
else:
print(f"{c}", end="")
print()
Cash.py
from cs50 import get_float
while True:
f = get_float("Change to be converted into coins: ")
if f > 0:
break
cents = f * 100
# while quarters can be used
quarters = 0
while (cents >= 25):
quarters += 1
cents = cents - 25
# while dimes can be used
dimes = 0
while (cents >= 10):
dimes += 1
cents = cents - 10
# while nickels can be used
nickels = 0
while (cents >= 5):
nickels += 1
cents = cents - 5
# while pennies can be used
pennies = 0
while (cents >= 1):
pennies += 1
cents = cents - 1
print("Number of coins used = ", quarters + dimes + nickels + pennies)
Mario.py
from cs50 import get_int
# get prompt
while True:
n = get_int("Positive Integer: ")
if n > 0 and n < 9:
break
hashes = 0
# create rows
for i in range(n):
hashes += 1
# create hashes per row
for j in range(n - hashes):
print(" ", end="")
for k in range(hashes):
print("#", end="")
print(" ", end="")
for k in range(hashes):
print("#", end="")
print()