Skip to content

pset 6

hello

Implement a program that prints out a simple greeting to the user, per the below.

Terminal window
$ python hello.py
What is your name?
David
hello, David
hello.py
from cs50 import get_string
name = get_string("What is your name?\n")
print("hello,", name)

mario

Implement a program that prints out a double half-pyramid of a specified height, per the below.

Terminal window
$ ./mario
Height: 4
# #
## ##
### ###
#### ####
mario.py
from cs50 import get_int
while True:
height = get_int("Height: ")
if height > 0 and height < 9:
break
for i in range(height):
for j in range(height - i - 1):
print(" ", end="")
for j in range(i + 1):
print("#", end="")
print(" ", end="")
for j in range(i + 1):
print("#", end="")
print()

credit

Implement a program that determines whether a provided credit card number is valid according to Luhn’s algorithm.

Terminal window
$ python credit.py
Number: 378282246310005
AMEX
credit.py
from cs50 import get_int
dig = 0
def main():
num = get_int("Number: ")
if (fraudCheck(num)):
print(serviceDetect(num))
else:
print("INVALID")
def fraudCheck(num):
mulsum = regsum = 0
buf = False
global dig
while num > 0:
if (buf == False):
regsum += (num % 10)
num = int(num / 10)
buf = True
else:
k = 2 * (num % 10)
if (k >= 10):
mulsum += (k % 10)
k = int(k / 10)
mulsum += k
else:
mulsum += k
num = int(num / 10)
buf = False
dig += 1
if (mulsum + regsum) % 10:
return False
return True
def serviceDetect(num):
global dig
print(dig)
if (dig == 13 and int(num / 10 ** 12) == 4):
return "VISA"
elif (dig == 16):
if (int(num / 10 ** 15) == 4):
return "VISA"
elif (int(num / 10 ** 14) >= 51 and int(num / 10 ** 14) <= 55):
return "MASTERCARD"
else:
return "INVALID"
elif (dig == 15):
if (int(num / 10 ** 13) == 34 or int(num / 10 ** 13) == 37):
return "AMEX"
else:
return "INVALID"
else:
return "INVALID"
main()

readability

Implement a program that computes the approximate grade level needed to comprehend some text, per the below.

Terminal window
$ python readability.py
Text: Congratulations! Today is your day. You're off to Great Places! You're off and away!
Grade 3
readability.py
from cs50 import get_string
def main():
text = get_string("Text: ")
letters = count_letters(text)
words = count_words(text)
sentences = count_sentences(text)
L = (letters / words) * 100
S = (sentences / words) * 100
i = 0.0588 * L - 0.296 * S - 15.8
index = round(i)
if index < 1:
print("Before Grade 1")
elif index >= 16:
print("Grade 16+")
else:
print(f"Grade {index}")
def count_letters(s):
count = 0
for c in s:
if c.isalpha():
count += 1
return count
def count_words(s):
count = 0
for c in s:
if c.isspace():
count += 1
return count + 1
def count_sentences(s):
count = 0
for c in s:
if c == '.' or c == '!' or c == '?':
count += 1
return count
main()

dna

Implement a program that identifies a person based on their DNA Short Tandem Repeats (STRs), per the below.

database example:

name,AGAT,AATG,TATC
Alice,28,42,14
Bob,17,22,19
Charlie,36,18,25
Terminal window
$ python dna.py databases/large.csv sequences/5.txt
Lavender
  • Directorydatabases
    • large.csv
    • small.csv
  • Directorysequences
    • *.txt
  • dna.py
dna.py
import sys
import csv
import re
if len(sys.argv) != 3:
print("Usage: python dna.py database.csv sequence.txt")
sys.exit(1)
database = {}
with open(sys.argv[1], 'r') as csvfile:
csvreader = csv.reader(csvfile)
field = next(csvreader)
field.remove(field[0])
field = tuple(field)
for row in csvreader:
database[tuple(row[1:])] = row[0]
with open(sys.argv[2], 'r') as txtfile:
sequence = txtfile.read()
count = {}
for value in field:
# https://www.geeksforgeeks.org/python-maximum-consecutive-substring-occurrence/
# Maximum Consecutive Substring Occurrence
# Using max() + re.findall()
largest = max(re.findall('((?:' + re.escape(value) + ')*)', sequence), key=len)
# can also be done like below(source: https://stackoverflow.com/a/61131908/9177454)
# largest = max(re.findall(r'(?:' + value + ')+', sequence), key = len)
count[value] = len(largest) // len(value)
result = [str(count[key]) for key in count]
result = tuple(result)
if result in database:
print(database[result])
else:
print("No match")


© 2020-2025 Ucchas Muhury