Advertisement
plarmi

ООП_0_Обработка строк

Mar 26th, 2024
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. from random import choice, choices
  2.  
  3. nucleobase = "AGCT"
  4.  
  5. start_codon = "ATG"
  6. stop_codon = ("TAG", "TAA", "TGA")
  7.  
  8. def isPotentialGene(DNA: str):
  9.     # длина кратна 3
  10.     if (len(DNA) % 3) != 0:
  11.         return False
  12.     # начинается с кодона начала
  13.     if not DNA.startswith(start_codon):
  14.         return False
  15.     # не имеет кодонов конца внутри
  16.     for i in range(len(DNA) - 3):
  17.         if i % 3 == 0:
  18.             if DNA[i:i+3] in stop_codon:
  19.                 return False
  20.     # завершается кодоном конца
  21.     if DNA.endswith(stop_codon[0]):
  22.         return True
  23.     if DNA.endswith(stop_codon[1]):
  24.         return True
  25.     if DNA.endswith(stop_codon[2]):
  26.         return True
  27.     return False
  28.  
  29.  
  30. # 0
  31. def generateCodon(triplets: int):
  32.     if triplets <= 0:
  33.         raise ValueError("the minimum number of triplets is 1")
  34.     codons = ""
  35.     while triplets > 0:
  36.         if (codon := "".join(choices(nucleobase, k=3))) not in stop_codon:
  37.             codons += codon
  38.             triplets -= 1
  39.     return codons
  40.  
  41. def generateDNA(length: int):
  42.     if length <= 6 or length % 3 != 0:
  43.         raise ValueError("the length of the DNA must be greater than six and a multiple of six")
  44.     return start_codon + generateCodon(int((length - 6) / 3)) + choice(stop_codon)
  45.  
  46. # 1
  47. def isValidDNA(st: str):
  48.     return set(st).issubset(nucleobase)
  49.  
  50. # 2
  51. def complementWC(DNA: str):
  52.     return ''.join([{'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C'}[base] for base in DNA])
  53.  
  54. # 3
  55. def palindromeWC(DNA: str):
  56.     return DNA == complementWC(DNA)[::-1]
  57.  
  58. # 4
  59. def isShift(s: str, t: str):
  60.     return len(s) == len(t) and t in s + s
  61.  
  62. # 5
  63. def find_PotentialGene(DNA: str, length: int):
  64.     potential_genes = []
  65.     for i in range(len(DNA)):
  66.         if DNA[i:i+3] == start_codon:
  67.             for j in range(i+3, len(DNA), 3):
  68.                 if DNA[j:j+3] in stop_codon:
  69.                     gene = DNA[i:j+3]
  70.                     if len(gene) >= length and len(gene) % 3 == 0:
  71.                         potential_genes.append(gene)
  72.                     break
  73.     if not potential_genes:
  74.         return None
  75.     return potential_genes
  76.  
  77. # 6
  78. def caesar_cipher(text: str, ROT: int, decipher=False):
  79.     if ROT < 0:
  80.         raise ValueError("the number of shifts must be greater than or equal to zero")
  81.     encrypted_text = ""
  82.     if decipher:
  83.         ROT = -ROT
  84.     for char in text:
  85.         if char.isalpha():
  86.             shifted = ord(char) + ROT
  87.             if char.islower():
  88.                 if shifted > ord("z"):
  89.                     shifted -= 26
  90.                 elif shifted < ord("a"):
  91.                     shifted += 26
  92.             elif char.isupper():
  93.                 if shifted > ord("Z"):
  94.                     shifted -= 26
  95.                 elif shifted < ord("A"):
  96.                     shifted += 26
  97.             encrypted_text += chr(shifted)
  98.         else:
  99.             encrypted_text += char
  100.     if not encrypted_text:
  101.         return None
  102.     return encrypted_text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement