CS50 Problem Set 6

CS50 Problem Set 6

King Hua Lv2

CS50 Problem Set 6

Here’s my answer for the CS50 Problem Set 6. Hope that will help you a bit.

Problem 1: Hello, Again

1
2
3
name = input("What is your name? ")

print(f"hello, {name}")

Probelm 2: Mario

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
while True:
try:
h = int(input("Height: "))

if 1 <= h <= 8:
break
else:
print("", end='')
except ValueError:
print("", end='')

for i in range(1, h+1):
for j in range(0, h-i):
print(" ", end='')
# j += 1

for k in range(0, i):
print("#", end='')
# k += 1

print(" ", end='')

for m in range(0, i):
print("#", end='')
# m += 1

print()

Problem 3: Credit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
while True:
number = input("Number: ")
if number.isdigit() == True:
break
else:
number = input("Number: ")

reversed_number = number[::-1]

if len(number) == 15:
sum = 0
temp_sum_1 = 0
temp_sum_2 = 0
if number[:2] != "37" and number[:2] != "34":
print("INVALID")
else:
for i in range(1, 15, 2):
temp = int(reversed_number[i]) * 2
if temp > 9:
temp = temp // 10 + temp % 10
temp_sum_1 += temp

for i in range(0, 15, 2):
temp = int(reversed_number[i])
temp_sum_2 += temp

sum = temp_sum_1 + temp_sum_2
if sum % 10 == 0:
print("AMEX")
else:
print("INVALID")

elif len(number) == 13:
sum = 0
temp_sum_1 = 0
temp_sum_2 = 0
if number[:1] != "4":
print("INVALID")
else:
for i in range(1, 13, 2):
temp = int(reversed_number[i]) * 2
if temp > 9:
temp = temp // 10 + temp % 10
temp_sum_1 += temp

for i in range(0, 13, 2):
temp = int(reversed_number[i])
temp_sum_2 += temp

sum = temp_sum_1 + temp_sum_2
if sum % 10 == 0:
print("VISA")
else:
print("INVALID")

elif len(number) == 16:
sum = 0
temp_sum_1 = 0
temp_sum_2 = 0
if number[:1] != "4" and number[:2] != "51" and number[:2] != "52" and number[:2] != "53" and number[:2] != "54" and number[:2] != "55":
print("INVALID")
elif number[:1] == "4":
for i in range(1, 16, 2):
temp = int(reversed_number[i]) * 2
if temp > 9:
temp = temp // 10 + temp % 10
temp_sum_1 += temp

for i in range(0, 16, 2):
temp = int(reversed_number[i])
temp_sum_2 += temp

sum = temp_sum_1 + temp_sum_2
if sum % 10 == 0:
print("VISA")
else:
print("INVALID")
else:
for i in range(1, 16, 2):
temp = int(reversed_number[i]) * 2
if temp > 9:
temp = temp // 10 + temp % 10
temp_sum_1 += temp

for i in range(0, 16, 2):
temp = int(reversed_number[i])
temp_sum_2 += temp

sum = temp_sum_1 + temp_sum_2
if sum % 10 == 0:
print("MASTERCARD")
else:
print("INVALID")

else:
print("INVALID")

Problem 4: Readability

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def words(text):
words = 1
for i in range(0, len(text)):
if text[i] == ' ':
words += 1
return words


def letters(text):
letters = 0
for i in range(0, len(text)):
if text[i].isalpha() == True:
letters += 1
return letters


def sentences(text):
sentences = 0
for i in range(0, len(text)):
if text[i] == '.' or text[i] == '!' or text[i] == '?':
sentences += 1
return sentences


text = input("Text: ")

index = round(0.0588 * letters(text) / words(text) * 100 -
0.296 * sentences(text) / words(text) * 100 - 15.8)

if index < 1:
print("Before Grade 1")

elif index >= 16:
print("Grade 16+")
else:
print(f"Grade {index}")

Problem 5: DNA

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import csv
import sys


def main():

# TODO: Check for command-line usage
if len(sys.argv) != 3:
print("Missing command line arguments! ")
sys.exit()

# TODO: Read database file into a variable
database = []
with open(sys.argv[1], "r") as database_file:
reader = csv.DictReader(database_file)
for row in reader:
database.append(row)

# TODO: Read DNA sequence file into a variable
sequence = []
with open(sys.argv[2], "r") as sequence_file:
sequence = sequence_file.read()

# TODO: Find longest match of each STR in DNA sequence
str_count = {} # Initlize a list
for key in database[0].keys():
if key == "name":
continue
# When you insert a value but the key doesn't exist, python will create the key automatically and insert the value
str_count[key] = longest_match(sequence, key)

# TODO: Check database for matching profiles
for row in database:
match = True
# 遍历所有的key,并去除'name'
for key in row.keys():
if key == "name":
continue
# 当row[key]与str_count[key]不相等时,返回false,继续下一个key
if str_count[key] != int(row[key]):
match = False
break
if match:
print(row["name"])
return
print("No match")


def longest_match(sequence, subsequence):
"""Returns length of longest run of subsequence in sequence."""

# Initialize variables
longest_run = 0
subsequence_length = len(subsequence)
sequence_length = len(sequence)

# Check each character in sequence for most consecutive runs of subsequence
for i in range(sequence_length):

# Initialize count of consecutive runs
count = 0

# Check for a subsequence match in a "substring" (a subset of characters) within sequence
# If a match, move substring to next potential match in sequence
# Continue moving substring and checking for matches until out of consecutive matches
while True:

# Adjust substring start and end
start = i + count * subsequence_length
end = start + subsequence_length

# If there is a match in the substring
if sequence[start:end] == subsequence:
count += 1

# If there is no match in the substring
else:
break

# Update most consecutive matches found
longest_run = max(longest_run, count)

# After checking for runs at each character in sequence, return longest run found
return longest_run


main()
  • Title: CS50 Problem Set 6
  • Author: King Hua
  • Created at : 2026-02-16 20:44:57
  • Updated at : 2026-02-16 21:02:35
  • Link: https://kinghua0629.github.io/2026/02/16/CS50-Problem-Set-6/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments