Question

3. Implement the function read_matrix_file that consumes a file name (string) as its parameter and returns...

3. Implement the function read_matrix_file that consumes a file name (string) as its parameter and returns a nested list of ints (4x4 matrix) associated with the scores aligning G, A, T, and C.

a. The nested list for the provided alignment_matrix text file would be: [[10,5,3,-5],[5,9,6,7], [3,6,12,8],[-5,7,8,10]].

Python language, txt file, sorry

Homework Answers

Answer #1

PYTHON CODE

def read_matrix_file(filename:str):
file = open(filename,'r')

#All lines in # are for your understanding
##this is a nested list, let me break it up for you to understand
#file.readlines() returns a list of elements in the file where each element is a line in the file
#for x in file.readlines() traverses each line in the file, x contains char '\n' at the end
#x.split('\n')[0] gets the string without the char '\n' now split using ' ' gives a list of all numbers as string
#now this list of strings should form another inner list which is one element of the outer list
#in this step the the string number are traversed and converted to int and we have our nested list!
outList = [[int(i) for i in x.split('\n')[0].split(' ')] for x in file.readlines()]
#return the outList
return outList
#calling the function
read_matrix_file('alignment_text.txt')

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Python Write function stringCount() that takes two string inputs—a file name and a target string— and...
Python Write function stringCount() that takes two string inputs—a file name and a target string— and returns the number of occurrences of the target string in the file. >>> stringCount('example.txt', 'line') 4
Write a Python function that takes a filename as a parameter and returns the string 'rows'...
Write a Python function that takes a filename as a parameter and returns the string 'rows' if a file was written row by row, and 'columns' if the file was written column by column. You would call the function with a command like filetype = myfunc(filename).
using python Write function stringCount() that takes two string inputs—a file name and a target string—...
using python Write function stringCount() that takes two string inputs—a file name and a target string— and returns the number of occurrences of the target string in the file. >>> stringCount('example.txt', 'line') 4
Using Python, Implement a decryption function: Implement a function named decrypt that opens a file named...
Using Python, Implement a decryption function: Implement a function named decrypt that opens a file named input_file and decrypts its content using the opposite of the dictionary created using a key (which is a parameter of the function) and stores the decrypted file to a file named output_file. The input and output file names will be positional parameters. The key parameter should be a keyword-only optional parameter where the default value is “IT RAINS A LOT IN MILWAUKEE IN THE...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first,...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first, third, fifth (and so on) characters of the strings, with each character both preceded and followed by the ^ symbol, and with a newline appearing after the last ^ symbol. The function returns no value; its goal is to print its output, but not to return it. Q2) Write a Python function called lines_of_code that takes a Path object as a parameter, which is...
In PYTHON please: Write a function named word_stats that accepts as its parameter a string holding...
In PYTHON please: Write a function named word_stats that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report the total number of words (as an integer) and the average word length (as an un-rounded number). For example, suppose the file tobe.txt contains the following text: To be or not to be, that is the...
Implement in python a function avg_val(lst), which returns the average value of the elements in list....
Implement in python a function avg_val(lst), which returns the average value of the elements in list. For example, given a list lst: [19, 2, 20, 1, 0, 18], the function should return 10. The name of the method should be avg_val and the method should take one parameter which is the list of values to test. Here is an example call to the function print(avg_val([19, 2, 20, 1, 0, 18]))
Python Write function words() that takes one input argument—a file name—and returns the list of actual...
Python Write function words() that takes one input argument—a file name—and returns the list of actual words (without punctuation symbols !,.:;?) in the file. >>> words('example.txt') ['The', '3', 'lines', 'in', 'this', 'file', 'end', 'with', 'the', 'new', 'line', 'character', 'There', 'is', 'a', 'blank', 'line', 'above', 'this', 'line']
The file named world_series_winners.txt contains a chronological list of the World Series winning teams from 1903...
The file named world_series_winners.txt contains a chronological list of the World Series winning teams from 1903 through 2009. (The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2009. Note that the World Series was not played in 1904 or 1994.) Write a program q3.py that lets the user enter the name of a team and then displays to the screen...
Write a function called fun which has an object as its parameter and returns the name...
Write a function called fun which has an object as its parameter and returns the name of properties of that object in an array. For instance, if it receives {a:1,b:3}, as its parameter, it should return [a, b], or if it receives {u:4, k:3, h:5}, it should return [u, k, h]. Answer:(penalty regime: 10, 20, ... %)