SHARE
TWEET

Untitled

a guest Mar 25th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. def periodiska_systemet():
  4.     list = ["H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br","Kr","Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Sb","Te","I","Xe","Cs","Ba","La","Ce","Pr","Nd","Pm","Sm","Eu","Gd","Tb","Dy","Ho","Er","Tm","Yb","Lu","Hf","Ta","W","Re","Os","Ir","Pt","Au","Hg","Tl","Pb","Bi","Po","At","Rn","Fr","Ra","Ac","Th","Pa","U","Np","Pu","Am","Cm","Bk","Cf","Es","Fm","Md","No","Lr","Rf","Db","Sg","Bh","Hs","Mt","Ds","Rg","Cn","Fl","Lv"]
  5.     return list
  6.  
  7. # Syntaxkontroll
  8. import string
  9.  
  10.  
  11. class Node:
  12.     def __init__(self, value=None, next=None):
  13.         self.value = value  # HĂ„ller ett vĂ€rde för noden
  14.         self.next = next    # SĂ€tter en pekare pĂ„ vilken nod som Ă€r nĂ€sta
  15.  
  16.     def __str__(self):
  17.         return self.value   # Returnerar vĂ€rdet pĂ„ noden
  18.  
  19.  
  20. class LinkedQ:
  21.     def __init__(self):
  22.         self.__first = None     # SĂ€tter en pekare pĂ„ den första noden i kön
  23.         self.__last = None      # SĂ€tter en pekare pĂ„ den sista noden i kön
  24.  
  25.     def enqueue(self, value):   # Metod för att lĂ€gga till noder i kön
  26.  
  27.         new_node = Node(value)
  28.  
  29.         if self.isEmpty():
  30.             self.__first = new_node
  31.             self.__last = new_node
  32.         else:
  33.             self.__last.next = new_node     # Den senaste noden pekar nu pĂ„ den nya tillagda noden
  34.             self.__last = new_node          # Den nya tillagda noden tilldelas den sista platsen
  35.  
  36.     def dequeue(self):                      # Metod för att ta ut den första noden i kön
  37.         value = self.__first.value
  38.         self.__first = self.__first.next    # SĂ€tter den som Ă€r efter till först i kön
  39.         return value
  40.  
  41.     def isEmpty(self):                      # Kollar om kön Ă€r tom
  42.         if self.__first is None:
  43.             return True
  44.         else:
  45.             return False
  46.  
  47.     def peek(self):
  48.         if not self.isEmpty():
  49.             return self.__first.value
  50.         else:
  51.             return None
  52.  
  53.  
  54. class Syntaxfel(Exception):
  55.     pass
  56.  
  57. def errorPrinter(q):
  58.     error_str = ""
  59.     while not q.isEmpty():
  60.         x = q.dequeue()
  61.         x=str(x)
  62.         error_str += x
  63.     return error_str
  64.  
  65.  
  66. def LETTER(q):
  67.  
  68.     ALPHABET = alphabetmaker(2)
  69.  
  70.     tecken = q.dequeue()
  71.  
  72.     if tecken in ALPHABET:
  73.         print(tecken)
  74.         return tecken
  75.     raise Syntaxfel("Saknad stor bokstav")
  76.  
  77.  
  78. def letter(q):
  79.  
  80.     alphabet = alphabetmaker(0)
  81.  
  82.     tecken = q.dequeue()
  83.  
  84.     if tecken in alphabet:
  85.         print(tecken)
  86.         return tecken
  87.     raise Syntaxfel("Ska vara en stor bokstav")
  88.  
  89.  
  90. def num(q):
  91.  
  92.     number = q.dequeue()
  93.  
  94.     next = q.peek()
  95.  
  96.  
  97.  
  98.     if type(number) is int and number > 0:
  99.  
  100.         if type(next) is int:
  101.             print(number)
  102.  
  103.             while type(next) is int:
  104.  
  105.                 nextnumber = q.dequeue()
  106.                 next = q.peek()
  107.  
  108.                 print(nextnumber)
  109.             return
  110.  
  111.     if type(number) is int and number >= 2:
  112.         print(number)
  113.         return
  114.     elif type(number) is int:
  115.         raise Syntaxfel("För litet tal vid radslutet " + errorPrinter(q))
  116.     else:
  117.         raise Syntaxfel("String istĂ€llet för int " + errorPrinter(q))
  118.  
  119.  
  120. # def num(q):
  121. #
  122. #     number = q.dequeue()
  123. #
  124. #     next = q.peek()
  125. #
  126. #     if type(number) is int and number >= 2:
  127. #         print(number)
  128. #         while type(next) is int:
  129. #
  130. #             nextnumber = q.dequeue()
  131. #             next = q.peek()
  132. #
  133. #             print(nextnumber)
  134. #         return
  135. #     if type(number) is int:
  136. #         raise Syntaxfel("För litet tal vid radfel")
  137. #     else:
  138. #         raise Syntaxfel("String istĂ€llet för int")
  139. #
  140.  
  141. def readAtom(q):
  142.  
  143.     alphabet = alphabetmaker(0)
  144.  
  145.     LETTER_CAPITAL = LETTER(q)
  146.  
  147.     next = q.peek()
  148.  
  149.     if next in alphabet:
  150.         letter_lower = letter(q)
  151.         if LETTER_CAPITAL + letter_lower in periodiska_systemet():
  152.             return
  153.         raise Syntaxfel("OkĂ€nd atom vid radslutet " + errorPrinter(q))
  154.     else:
  155.         if LETTER_CAPITAL in periodiska_systemet():
  156.             return
  157.         raise Syntaxfel("readAtom2")
  158.  
  159.  
  160. def readGroup(q):
  161.     alphabet = alphabetmaker(0)
  162.     ALPHABET = alphabetmaker(2)
  163.     next = q.peek()
  164.  
  165.     if next == "(":
  166.         print(next)
  167.         q.dequeue()  # Tar ut "("
  168.         readMol(q)
  169.  
  170.         # next = q.peek()
  171.  
  172.         # if next is ")":
  173.         #     print(next)
  174.         #     q.dequeue()  # Tar ut ")"
  175.         #     next = q.peek()
  176.         #     if type(next) is int:
  177.         #         num(q)
  178.         #         return
  179.         #     raise Syntaxfel("Readgroup 2")
  180.         # else:   #        if next is not ")":
  181.         #     raise Syntaxfel("Saknad högerparentes vid radslutet")
  182.  
  183.     if next is ")":
  184.         print(next)
  185.         next = q.peek()
  186.         q.dequeue()
  187.         if type(next) is int:
  188.             num(q)
  189.             return
  190.         raise Syntaxfel("Saknad siffra vid radslutet " + errorPrinter(q))
  191.  
  192.     if next in ALPHABET:
  193.         readAtom(q)
  194.         next = q.peek()
  195.         if type(next) is int:
  196.             num(q)
  197.             return
  198.         if next is ")":
  199.             raise Syntaxfel("Felaktig gruppstart vid radslutet " + errorPrinter(q))
  200.  
  201.         # else:
  202.         #     return
  203.     if next is None:
  204.         raise Syntaxfel("Saknad högerparentes vid radslutet")       # DĂ„ next Ă€r Nonetype, finns det fler scenarion?
  205.  
  206.     if type(next) in alphabet: ############ fortsĂ€tt hĂ€r
  207.  
  208.  
  209. def readMol(q):
  210.  
  211.     readGroup(q)
  212.  
  213.     next = q.peek()
  214.  
  215.     ALPHABET = alphabetmaker(2)
  216.  
  217.     alphabet = alphabetmaker(0)
  218.  
  219.     if next == "(":
  220.  
  221.         #print("next 1")
  222.         readMol(q)
  223.         return
  224.  
  225.     if next in ALPHABET:
  226.         #print("next 2")
  227.         readMol(q)
  228.         return
  229.  
  230.     if next == ")":
  231.         #print("next 3")
  232.         readMol(q)
  233.  
  234.     if next in alphabet:
  235.         raise Syntaxfel("Felaktig gruppstart")
  236.  
  237. def readFormel(q):
  238.     readMol(q)
  239.     print("123klar\n")
  240.  
  241.  
  242. def makeLinkedQ(string):
  243.     q = LinkedQ()
  244.     string_int=["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
  245.     for tecken in string:
  246.         if tecken in string_int:
  247.             q.enqueue(int(tecken))
  248.         else:
  249.             q.enqueue(tecken)
  250.     return q
  251.  
  252.  
  253. def alphabetmaker(x):
  254.  
  255.     if x > 0:
  256.         alph = string.ascii_uppercase
  257.     else:
  258.         alph = string.ascii_lowercase
  259.  
  260.     list = []
  261.     for i in alph:
  262.         list.append(i)
  263.     return list
  264.  
  265.  
  266. def tryProgram(q):
  267.     # q=makeQueueforFormel("sampleinput1")
  268.     # q = makeLinkedQ("Aa4B7Cc33M33")
  269.     #q = makeLinkedQ("Aa4B7Cc33M33")
  270.  
  271.     try:
  272.         readFormel(q)
  273.         print("Följer syntax")
  274.     except Syntaxfel as fel:
  275.         print(str(fel))
  276.  
  277. def runProgram(file):
  278.     with open(file, "r", encoding="utf-8") as r_file:
  279.         print("Fil: ", file)
  280.         for rad in r_file:
  281.             rad = rad.strip()
  282.             rad = rad.strip("\n")
  283.             # print(rad)
  284.             print("molekyl som testas: ", rad)
  285.  
  286.             if rad != "#":
  287.                 q = makeLinkedQ(rad)
  288.                 tryProgram(q)
  289.                 print(rad)
  290.                 print("_____________________________")
  291.  
  292. runProgram("sampleinput2")
  293.  
  294. # runProgram()
  295. ##########################
  296. # Nacl <- kolla för liten bokstav i readGroup.
  297. # a
  298. # (Cl)2)3
  299. # )
  300. 2
  301. #
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top