Question

In: Computer Science

using python One measure of “unsortedness” in a sequence is the number of pairs of entries...

using python

One measure of “unsortedness” in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence “DAABEC”, this measure is 5, since D is greater than four letters to its right and E is greater than one letter to its right. This measure is called the number of inversions in the sequence. The sequence “AACEDGG” has only one inversion (E and D)--it is nearly sorted--while the sequence “ZWQM” has 6 inversions. Formally speaking, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j.

So in the sequence 2, 4, 1, 3, 5, there are three inversions (2, 1), (4, 1), (4, 3).

You are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of ``sortedness'', from ``most sorted'' (lowest inversions) to ``least sorted'’ (highest inversions). All the strings are of the same length.

Input: These are m character sequences given each of fixed length. If the input is “#”, stop the input.

Output: Output the list of input strings, arranged from ``most sorted'' to ``least sorted''. If two or more strings are equally sorted, list them in the same order they are in the input file.

Sample Input: AACATGAAGG TTTTGGCCAA TTTGGCCAAA GATCAGATTT CCCGGGGGGA ATCGATGCAT

Sample Output: CCCGGGGGGA AACATGAAGG GATCAGATTT ATCGATGCAT TTTTGGCCAA TTTGGCCAAA

Solutions

Expert Solution

Please let me know if anything is required. Please follow the indentation as shown in the screenshot.

Code:

import collections
DNA=[] #list to store the strings
m=input("Enter the length of each string : ") #taking the string length from the user
s=input("Enter the string continue or # to stop : ") #taking the string from the user

#condition to continue or stop taking the input
while(s!='#'):
DNA.append(s) #appending the strings into the list
s=input("Enter the string continue or # to stop : ")

inversion={} #dictornary to store the the inversions values

#loop to calculate the inversions
for i in DNA:
c=0
for k in range(int(m)):
for j in range(int(m)):
if(i[k]>i[j] and k<j):
c=c+1
inversion[i]=c
  

#sorting the inversions dictornary according to values
inversion_sorted = (sorted(inversion.items(), key = lambda kv:(kv[1], kv[0])))   

#printing the results from most sorted to least sorted
print("\nstrings arranged from most sorted to least sorted ")
for data in inversion_sorted:
print(data[0])

Code Screenshot :

Test case :


Related Solutions

Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs...
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs and prints the number of even and odd inputs in the sequence. please explain. Thanks
Python #Remember that Fibonacci's sequence is a sequence of numbers #where every number is the sum...
Python #Remember that Fibonacci's sequence is a sequence of numbers #where every number is the sum of the previous two numbers. # #Joynernacci numbers are similar to Fibonacci numbers, but #with two differences: # # - Fibonacci numbers are famous, Joynernacci numbers are # not (yet). # - In Joynernacci numbers, even-indexed numbers are the # sum of the previous two numbers, while odd-indexed # numbers are the absolute value of the difference # between the previous two numbers. #...
Using Python: The Fibonacci sequence is a famous series of numbers with the following rules: The...
Using Python: The Fibonacci sequence is a famous series of numbers with the following rules: The first number in the sequence is 0 - The second number in the sequence is 1 - The other numbers in the sequence are composed by adding up the two previous numbers in the sequence. We therefore have the following sequence: 1 st number: 0 2nd number: 1 3 rd number: 0 + 1 = 1 4 th number: 1+1 =2 5 th number:...
I have a one strand DNA sequence that I am trying to determine the base pairs...
I have a one strand DNA sequence that I am trying to determine the base pairs for. The gene is CYP1A2 and the primers and sequence are below. Below is the sequence of part of the CYP1A2 gene (you are given only 1 strand, written in the 5’ – 3’ direction). 5’TGGGCTAGGTGTAGGGGTCCTGAGTTCCGGGCTTTGCTACCCAGCTCTTGACTTCTGTTTCCCGATTTTA AATGAGCAGTTTGGACTAAGCCATTTTTAAGGAGAGCGATGGGGAGGGCTTCCCCCTTAGCACAAGGGCA GCCCTGGCCCTGGCTGAAGCCCAACCCCAACCTCCAAGACTGTGAGAGGATGGGGACTCATCCCTGGAGG AGGTGCCCCTCCTGGTATTGATAAAGAATGCCCTGGGGAGGGGGCATCACAGGCTATTTGAACCAGCCCT GGGACCTTGGCCACCTCAGTGTCACTGGGTAGGGGGAACTCCTGGTCCCTTGGGTATATGGAAGGTATCA GCAGAAAGCCAGCACTGGCAGGGACTCTTTGGTACAATACCCAGCATGCATGCTGTGCCAGGGGCTGACA AGGGTGCTGTCCTTGGCTTCCCCATTTTGGAGTGGTCACTTGCCTCTACTCCAGCCCCAGAAGTGGAAAC TGAGATGATGTGTGGAGGAGAGAGCCAGCGTTCATGTTGGGAATCTTGAGGCTCCTTTCCAGCTCTCAGA TTCTGTGATGCTCAAAGGGTGAGCTCTGTGGGCCCAGGACGCATGGTAGATGGAGCTTAGTCTTTCTGGT ATCCAGCTGGGAGCCAAGCACAGAACACGCATCAGTGTTTATCAAATGACTGAGGAAATGAATGAATGAA TGTCTCCATCTCAACCCTCAGCCTGGTCCCTCCTTTTTTCCCTGCAGTTGGTACAGATGGCATTGTCCCA GTCTGTTCCCTTCTCGGCCACAGAGCTTCTCCTGGCCTCTGCCATCTTCTGCCTGGTATTCTGGGTGCTC AAGGGTTTGAGGCCTCGGGTCCCCAAAGGCCTGAAAAGTCCACCAGAGCCATGGGGCTGGCCCTTGCTCG GGCATGTGCTGACCCTGGGGAAGAACCCGCACCTGGCACTGTCAAGGATGAGCCAGCGCTACGGGGACGT CCTGCAGATCCGCATTGGCTCCACGCCCGTGCTGGTGCTGAGCCGCCTGGACACCATCCGGCAGGCCCTG 3’ The sequences of the primers used to amplify part of the CYP1A2 gene...
Using python coding, test for convergence of an infinite sequence or series. keep the coding at...
Using python coding, test for convergence of an infinite sequence or series. keep the coding at beginner level please!
How would you determine the number of proton-proton pairs, the number of neutron-neutron pairs, and the...
How would you determine the number of proton-proton pairs, the number of neutron-neutron pairs, and the number of neutron-neutron pairs in an atom given its atomic number and mass number? Hence show that Carbon has 15 proton-proton pairs and 10 neutron-neutron pairs.
python: ask the user to input a sequence of positive numbers. the end of the sequence...
python: ask the user to input a sequence of positive numbers. the end of the sequence is determined when the user enters a negative number. print out the maximum number from the sequence. output: keep entering positive numbers. to quit, input a negative number. enter a number: 67 enter a number: 5 enter a number: 8 enter a number: -3 largest number entered: 67 (note: i do not want to ask the user how many numbers they will input)
How can you find the number of pairs of alleles involved in polygenic inheritance by using...
How can you find the number of pairs of alleles involved in polygenic inheritance by using the number of phenotypic forms of the trait they condition?
A Fibonacci sequence, is a sequence of numbers with the property that each number is the...
A Fibonacci sequence, is a sequence of numbers with the property that each number is the sum of the two preceding Fibonacci numbers, starting from 0 and 1. Fibonacci number are usually denoted by Fn, where Fn is the nth Fibonacci number. Thus Fn = 0, and Fn = 1, and Fn = Fn-1 + Fn-2, n ≥ 2. Here are the first few Fibonacci numbers: F0=0 (by definition) F1=1 (by definition) F2 = F1 + F0 = 1 +...
One Problem/Question (Four Parts) Write a python program which prints a sequence of integers that are...
One Problem/Question (Four Parts) Write a python program which prints a sequence of integers that are multiples of 10, starting at 0 and ending at 100. Write a python program that computes the average of 10 random odd integers, ranging from 0 to 500. Write a python program that prints whatever the user enters, and stops when the user types “done”, using an infinite loop and a break statement. Same as number three but use a condition. The loop terminates...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT