Advertisement
duquesne9

Michael's no notes

Oct 6th, 2020
1,828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.60 KB | None | 0 0
  1. from sys import argv
  2.  
  3.  
  4. def main():
  5.     check_args(argument=argv)
  6.     letters = get_letters()
  7.     lst = get_dna()
  8.     dna_repeated_list = get_repeat_list(letters=letters, lst=lst)
  9.     get_match(lst=lst, dna_repeated_list=dna_repeated_list)
  10.  
  11.  
  12. def check_args(argument=[]):
  13.     ''' checks argv to confirm proper input from user '''
  14.     if len(argv) != 3:  # asks for a valid input
  15.         print(f"Usage: python dna.py data.csv sequence.txt")
  16.         exit(1)
  17.  
  18.  
  19. def get_letters():
  20.     ''' reads in DNA sequence from .txt file '''
  21.     with open(argv[2], newline='') as f:
  22.         text = f.read()
  23.     letters = [letter for word in text for letter in word]
  24.     return letters
  25.  
  26.  
  27. def get_dna():
  28.     ''' reads in people/SRT counts from .csv file'''
  29.     with open(argv[1], newline='') as f:
  30.         people = f.read()
  31.     dna_list = [line for line in people]
  32.  
  33.     peoples_dna = ''
  34.     for _, val in enumerate(dna_list):  # makes the dna_list into a string
  35.         peoples_dna += val
  36.     peoples_dna = peoples_dna.strip()
  37.  
  38.     lst = [sentence.split(',') for sentence in peoples_dna.splitlines()]
  39.     return lst
  40.  
  41.  
  42. def get_repeat_list(letters=[], lst=[]):
  43.     ''' determines longest count of repeating DNA sequences '''
  44.     dna_repeated_list = []
  45.  
  46.     for m in range(len(lst[0]) - 1):
  47.         total_appear_time_list = []
  48.         m = m + 1
  49.  
  50.         # checks the number of times something appeared in the list
  51.         for i in range(len(letters) - 3):
  52.             total_appear_times = 0
  53.             word = ""  # makes it go be nothing
  54.  
  55.             try:  # if it gets to the end of the dna line it will make a list of range not found
  56.                 for idx, _ in enumerate(lst[0][m]):
  57.                     word += letters[idx + i]
  58.             except:
  59.                 break
  60.  
  61.             while word == lst[0][m]:  # does it until the word is not the dna part
  62.                 total_appear_times += 1
  63.                 word = ''
  64.                 try:
  65.                     i = i + len(lst[0][m])
  66.                     for a in range(len(lst[0][m])):
  67.                         word += letters[a + i]
  68.                 except:  # remember to check if there is a error 'list index out of range'
  69.                     break  # exits the loop becuase it is at the end of the dna line
  70.  
  71.             # makes the number of repetion into a list
  72.             total_appear_time_list.append(total_appear_times)
  73.  
  74.         # makes another list to store the biggest number of times the dna string has appeared
  75.         dna_repeated_list.append(max(total_appear_time_list))
  76.  
  77.     return dna_repeated_list
  78.  
  79.  
  80. def get_match(lst=[], dna_repeated_list=[]):
  81.     ''' checks lists of DNA counts against people list '''
  82.     # loop through the dna file with the people on it checking all the verticle rows except the first
  83.     for m in range(len(lst) - 1):
  84.         adding = 0
  85.         m = m + 1
  86.         # loop through the dna file with the people on it to checking the numbers but doesn't check the names
  87.         for q in range(len(lst[1]) - 1):
  88.             q = q + 1  # makes the variable q start at 1
  89.             lst[m][q] = int(lst[m][q])  # makes it an int to compare it
  90.             # compares the double list to the singular list with all the maximumun number of times the pattern appears in a row
  91.             if lst[m][q] != dna_repeated_list[q - 1]:
  92.                 adding += 1  # if it does not match then add it by one
  93.         if adding == 0:  # if it all matches then print the name of the person
  94.             print(f"{lst[m][0]}")
  95.             exit(1)
  96.  
  97.     print(f"No match")  # if it has not been found
  98.  
  99.  
  100. if __name__ == '__main__':
  101.     main()
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement