Untitled

From Saha, 3 Months ago, written in Plain Text, viewed 1 times. This paste will buy the farm in 1 Second.
URL https://paste.paivola.fi/view/33437dd0 Embed
Download Paste or View Raw
  1. """
  2. COMP.CS.100 Ohjelmointi 1 / Programming 1
  3. Name:       Samuel Ahopelto
  4. Email:      samuel.ahopelto@tuni.fi
  5. Student Id: 050134989
  6. """
  7.  
  8.  
  9. DOORCODES = {'TC114': ['TIE'], 'TC203': ['TIE'], 'TC210': ['TIE', 'TST'],
  10.              'TD201': ['TST'], 'TE111': [], 'TE113': [], 'TE115': [],
  11.              'TE117': [], 'TE102': ['TIE'], 'TD203': ['TST'], 'TA666': ['X'],
  12.              'TC103': ['TIE', 'OPET', 'SGN'], 'TC205': ['TIE', 'OPET', 'ELT'],
  13.              'TB109': ['OPET', 'TST'], 'TB111': ['OPET', 'TST'],
  14.              'TB103': ['OPET'], 'TB104': ['OPET'], 'TB205': ['G'],
  15.              'SM111': [], 'SM112': [], 'SM113': [], 'SM114': [],
  16.              'S1': ['OPET'], 'S2': ['OPET'], 'S3': ['OPET'], 'S4': ['OPET'],
  17.              'K1705': ['OPET'], 'SB100': ['G'], 'SB202': ['G'],
  18.              'SM220': ['ELT'], 'SM221': ['ELT'], 'SM222': ['ELT'],
  19.              'secret_corridor_from_building_T_to_building_F': ['X', 'Y', 'Z'],
  20.              'TA': ['G'], 'TB': ['G'], 'SA': ['G'], 'KA': ['G']}
  21.  
  22. class Accesscard:
  23.     """
  24.     This class models an access card which can be used to check
  25.     whether a card should open a particular door or not.
  26.     """
  27.  
  28.     def __init__(self, id, name):
  29.         """
  30.         Constructor, creates a new object that has no access rights.
  31.  
  32.         :param id: str, card holders personal id
  33.         :param name: str, card holders name
  34.  
  35.         THIS METHOD IS AUTOMATICALLY TESTED, DON'T CHANGE THE NAME OR THE
  36.         PARAMETERS!
  37.         """
  38.  
  39.         self.id = id
  40.         self.__name = name
  41.         self.accesses = {}
  42.  
  43.     def get_id(self):
  44.         return self.id
  45.  
  46.  
  47.     def info(self):
  48.         """
  49.         The method has no return value. It prints the information related to
  50.         the access card in the format:
  51.         id, name, access: a1,a2,...,aN
  52.         for example:
  53.         777, Thelma Teacher, access: OPET, TE113, TIE
  54.         Note that the space characters after the commas and semicolon need to
  55.         be as specified in the task description or the test fails.
  56.  
  57.         THIS METHOD IS AUTOMATICALLY TESTED, DON'T CHANGE THE NAME, THE
  58.         PARAMETERS, OR THE PRINTOUT FORMAT!
  59.         """
  60.  
  61.         print(f"{self.id}, {self.__name}, access: ", end="")
  62.  
  63.         accesses = []
  64.         for x in self.accesses:
  65.             if x == "other":
  66.                 for y in self.accesses[x]:
  67.                     accesses.append(y)
  68.             else:
  69.                 accesses.append(x)
  70.         accesses.sort()
  71.         print(*accesses, sep=", ")
  72.  
  73.  
  74.  
  75.     def get_name(self):
  76.         """
  77.         :return: Returns the name of the accesscard holder.
  78.         """
  79.  
  80.         return self.__name
  81.  
  82.  
  83.     def add_access(self, new_access_code):
  84.         """
  85.         The method adds a new accesscode into the accesscard according to the
  86.         rules defined in the task description.
  87.  
  88.         :param new_access_code: str, the accesscode to be added in the card.
  89.  
  90.         THIS METHOD IS AUTOMATICALLY TESTED, DON'T CHANGE THE NAME, THE
  91.         PARAMETERS, OR THE RETURN VALUE! DON'T PRINT ANYTHING IN THE METHOD!
  92.         """
  93.  
  94.         if new_access_code in self.accesses:
  95.             return
  96.         for x in self.accesses:
  97.             if new_access_code in x:
  98.                 return
  99.  
  100.         doorcodes = []
  101.         if new_access_code in DOORCODES:
  102.             if "other" in self.accesses:
  103.                 doorcodes = self.accesses["other"]
  104.             doorcodes.append(new_access_code)
  105.             self.accesses["other"] = doorcodes
  106.             return
  107.  
  108.         for x in DOORCODES:
  109.             if new_access_code in DOORCODES[x]:
  110.                 doorcodes.append(x)
  111.  
  112.         if doorcodes == []:
  113.             return
  114.  
  115.         mydoorcodes = []
  116.         if "other" in self.accesses:
  117.             for x in self.accesses["other"]:
  118.                 mydoorcodes.append(x)
  119.  
  120.         for x in doorcodes:
  121.             if x in mydoorcodes:
  122.                 mydoorcodes.remove(x)
  123.  
  124.         self.accesses["other"] = mydoorcodes
  125.         self.accesses[new_access_code] = doorcodes
  126.  
  127.  
  128.  
  129.     def check_access(self, door):
  130.         """
  131.         Checks if the accesscard allows access to a certain door.
  132.  
  133.         :param door: str, the doorcode of the door that is being accessed.
  134.         :return: True: The door opens for this accesscard.
  135.                  False: The door does not open for this accesscard.
  136.  
  137.         THIS METHOD IS AUTOMATICALLY TESTED, DON'T CHANGE THE NAME, THE
  138.         PARAMETERS, OR THE RETURN VALUE! DON'T PRINT ANYTHING IN THE METHOD!
  139.         """
  140.  
  141.         if door not in DOORCODES:
  142.             return False
  143.  
  144.         for x in self.accesses:
  145.             if door in self.accesses[x]:
  146.                 return True
  147.  
  148.         return False
  149.  
  150.  
  151.     def merge(self, card):
  152.         """
  153.         Merges the accesscodes from another accesscard to this accesscard.
  154.  
  155.         :param card: Accesscard, the accesscard whose access rights are added to this card.
  156.  
  157.         THIS METHOD IS AUTOMATICALLY TESTED, DON'T CHANGE THE NAME, THE
  158.         PARAMETERS, OR THE RETURN VALUE! DON'T PRINT ANYTHING IN THE METHOD!
  159.         """
  160.  
  161.         for x in card.accesses:
  162.             if x not in self.accesses:
  163.                 self.accesses[x] = card.accesses[x]
  164.         doors = card.accesses["other"]
  165.         myDoors = self.accesses["other"]
  166.         for code in doors:
  167.             if code not in myDoors:
  168.                 self.add_access(code)
  169.  
  170.  
  171. def main():
  172.  
  173.     access_cards = []
  174.     file = open("accessinfo.txt", "r")
  175.     for row in file:
  176.         print("hei")
  177.         row = row.rstrip()
  178.         blocks = row.split(";")
  179.         print(blocks)
  180.         card = Accesscard(blocks[0], blocks[1])
  181.  
  182.         if blocks[2] != "":
  183.             accesses = blocks[2].split(",")
  184.             for access in accesses:
  185.                 card.add_access(access)
  186.         access_cards.append(card)
  187.  
  188.     access_cards.sort(key=lambda card: card.id)
  189.  
  190.     while True:
  191.         line = input("command> ")
  192.  
  193.         if line == "":
  194.             break
  195.  
  196.         strings = line.split()
  197.         command = strings[0]
  198.  
  199.         if command == "list" and len(strings) == 1:
  200.             for card in access_cards:
  201.                 card.info()
  202.  
  203.         elif command == "test" and len(strings) == 2:
  204.             list_id = int(strings[1])
  205.             access_cards[list_id].info()
  206.             print(access_cards[list_id].accesses)
  207.  
  208.         elif command == "info" and len(strings) == 2:
  209.             card_id = strings[1]
  210.             pass  # TODO: Excecute the command info here
  211.  
  212.         elif command == "access" and len(strings) == 3:
  213.             card_id = strings[1]
  214.             door_id = strings[2]
  215.             pass  # TODO: Excecute the command access here
  216.  
  217.         elif command == "add" and len(strings) == 3:
  218.             card_id = strings[1]
  219.             access_code = strings[2]
  220.             pass  # TODO: Excecute the command add here
  221.  
  222.         elif command == "merge" and len(strings) == 3:
  223.             card_id_to = strings[1]
  224.             card_id_from = strings[2]
  225.             pass  # TODO: Excecute the command merge here
  226.  
  227.         elif command == "quit":
  228.             print("Bye!")
  229.             return
  230.         else:
  231.             print("Error: unknown command.")
  232.  
  233.  
  234. if __name__ == "__main__":
  235.     main()
  236.  

Reply to "Untitled"

Here you can reply to the paste above