Harvard CS50 Week6 Review
Welcome to CS50 again! This blog will contain my review for Week 6’s content about Python.
Lecture
- Nothing much to note down.
Section
- Nothing to note down. Try yourself and read the document is quite helpful.
Shorts
- Not much to note down. Just introduction to Python.
Problem Set 6
C-to-Python-Sentimental
Nothing to say
DNA
Divide and Conquer
\begin{algorithm} \caption{DNA} \begin{algorithmic} \STATE Check for command-line usage \STATE Read database file into a variable \STATE Read sequence file into a variable \STATE Find longest match of each STR in DNA sequence \STATE Check database for matching profiles \end{algorithmic} \end{algorithm}
Useful Snippets
- Check for command-line uasge
if not len(sys.argv) == 3: print("error") return
- Read database file into a variable
rows = [] with open(sys.argv[1]) as file: reader = csv.DictReader(file) for row in reader: rows.append(row)
- Read DNA sequence file into a variable
with open(sys.argv[2]) as file: sequence = file.read()
- Find longest match of each STR in DNA sequence
result = {} for STR in STRs: count = longest_match(sequence, STR) result[STR] = f"{count}"
- Check database for matching profiles
for row in rows: name = row.pop("name") if row == result: print(name) break else: print("No match.")
Take-aways
- The command-line arguments are stored in
sys.argv
, which is a list. The first argument, which issys.argv[0]
is the script’s name and the arguments after this are the actual arguments. - Using this code to read from the file,
with open("filename") as file: content = file.read()
The variable
content
can be accessed outside thewith
statement. - The
==
can be used to determine whether two dictionaries are equivalent or not. - To pop a specific key:value pair from the dictionary,
temp = dict.pop(key)`.
- To add add a key:value pair to the dictionary,
dict = {} dict[key] = value
Lecture - Week6.5
- In reinforcement learning, if the agent does well, you give them a reward, otherwise, punish them.