Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


CS 22A: Introduction to Python

Fall 2021


Homework Two: Chapters 4 & 5

Please submit, via Canvas, your solutions to the following problems by 8:59am on the due date. Remember to answer all questions with code!


Problem One:

Write and run a Python program, animals.py, that does the following:

1) Create a list with the following animals (has to be in the same order): kangaroo,rabbit,jaguar,dolphin,panda,sloth,cat,dog

2) Print the number of animals in the list

3) Print the 4th and 5th elements on two different lines on the Python shell

4) Sort and print the elements of the list in alphabetical order

5) Replace the third, fifth, and last element of the list with three animals of your choice

6) Print every other element of your list


Problem Two:

You are given four files: Ms_Thompson.txt, Mr_Burns.txt, Mrs_Deal.txt, Ms_Nita.txt

Each file contains names of students in each line who have failed their class. For example, Ms_Thompson.txt contains Samuel on the first line, Richard on the second line, and Emily on the third line.

Write a program called teachers_students.py to perform the following tasks:

1) Create and print a list, named names_list, whose four elements are the different teachers separated by commas using the following string: file_names = “Ms_Thompson,Mr_Burns,Mrs_Deal,Ms_Nita”

2) Use names_list to create a file named failed_students.txt, which contains the name of the teacher followed by the name of the student with a dash in between. failed_students.txt should look like this:

Ms_Thompson - Samuel
Ms_Thompson - Richard
Ms_Thompson - Emily
Mr_Burns - Pat
Mr_Burns - Jackson
Mr_Burns - Sammy
Mrs_Deal - Erica
Mrs_Deal - Miles
Ms_Nita - Thomas
Ms_Nita - Linda


Extra Credit!: If you use for loop(s) in your program for problem 2, make a trace chart with the appropriate variables used in your for-loop(s).


Problem Three:

Write a program called indented_students.py to perform the following tasks:

1) Using the names_list from problem 2, print each teacher’s name once, then print the student names, indented. The output on the Python shell should look like this:

Ms_Thompson
Samuel
Richard
Emily
Mr_Burns
Pat
Jackson
Sammy
Mrs_Deal
Erica
Miles
Ms_Nita
Thomas
Linda


Problem Four:

The file, input.txt, contains five short DNA sequences, one per line.

Complete the code for part a in a file called GC_content.py.

Part a:

1) Create and print a list, dna_list, whose elements are the DNA sequences from the file input.txt. Hint: Use split() to create dna_list with five elements.

2) Iterate over the elements of dna_list to:

a) Remove the first 10 bases (adapter sequence)

b) Count the occurrences of A’s, C’s, G’s, and T’s, saved in a_count, c_count, g_count, and t_count

c) Calculate GC content for each sequence & print it to the Python shell


Complete the code for part b in a file called GC_content_function.py.

Part b:

1) Write a function that takes a_count, c_count, g_count, and t_count as arguments and returns the GC content for each of the five DNA sequences.

Write these GC contents into a file called “GC_content.txt” (use a for-loop).

The GC_content.txt file should look like this:

TCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATC : 50.0
ACTGATCGATCGATCGATCGATCGATGCTATCGTCGT : 48.6
GTCGATCACGATCTATCGTACGTATGCATATCGATATCGATCGTAGTC : 43.8
ACTATCGATGATCTAGCTACGATCGTAGCTGTA : 42.4
ACTAGCTAGTCTCGATGCATGATCAGCTTAGCTGATGATGCTATGCA : 44.7