Advertisement
Andry41

HW8req

Dec 15th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.01 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """ Nikita, a clever spy, needs to follow a series of clues (namely a
  4. sequence of words) that will lead her to discover one or more secret
  5. information (sequences of words).  To find the secret(s) Nikita has to
  6. visit different cities: in each city Nikita will find which is the
  7. next city she has to move to and will also get a new word of the
  8. secret.  The next city you have to move to depends on the next clue.
  9.  
  10. It is a bit like a treasure hunt, in which Nikita explores a network
  11. of cities, collecting information.
  12.  
  13. NOTE: From the same city, the same clue can lead to multiple
  14.   alternative cities.  So the paths to explore could be multiple and
  15.   the secrets more than one.
  16.  
  17. NOTE: if in a certain city the instruction corresponding to the next
  18.   clue is missing, this means that the enemy spy network has
  19.   captured and destroyed the information. This also means the secret
  20.   that Nikita was following with that sequence cannot be completed
  21.   anymore.  Thus, our spy quits that sequence and she tries to
  22.   complete all the other secrets she has already collected.
  23.  
  24. We want to reconstruct all the secrets that Nikita could reconstruct,
  25. given the available clues and the pieces of instructions scattered
  26. throughout the different cities.
  27.  
  28. The instructions about how to move between the cities are contained in
  29. a text file, according to the following format:
  30. - every line starting with the '#' character should be ignored
  31. - cities are always written in UPPERCASE
  32. - the clues and words of the secret(s) to be discovered are always
  33. written in lowercase letters
  34.  
  35. The file contains, separated by at least one space/tab/return, zero or
  36. more instructions to follow.  Each instruction is written as the
  37. concatenation of four words:
  38.    - city    (UPPERCASE word)
  39.    - clue    (lowercase word)
  40.   - destination (UPPERCASE word)
  41.    - secret      (lowercase word)
  42.  
  43. Example:
  44.   the instruction ROMAcarciofoPARIGIchampagne indicates that
  45.       - when the spy is in                         'ROME'
  46.       - if the following clue is                   'carciofo'
  47.       - the spy must go to                         'PARIGI'.
  48.       - and add to the secret the word             'champagne'
  49.  
  50. NOTE: You can assume that the each line of instruction is unique.
  51. NOTE: There may be different instructions that even starting from the
  52.     same city AND having the same clue, lead to different cities and/or
  53.     produce different secrets.
  54.   Example:
  55.   ROMAcarciofoPARIGIchampagne
  56.   ROMAcarciofoCANCANCANCUNchampagne
  57.   ROMAcarciofoPARIGImitraglietta
  58.   ROMAcarciofoCATANZAROcommissario
  59.  
  60. Design and implement the function ex1(instructions_file, initial_city, clues)
  61. recursive or using recursive functions or methods, which receives as
  62. arguments:
  63. - instructions_file: the name of a text file that contains
  64.                     instructions to follow in each city
  65. - initial_city:      the name of the initial city from which Nikita
  66.                     starts her journey (UPPERCASE word)
  67.  
  68. - clues:             a list of clues (string of lowercase words separated
  69.                     by spaces)
  70.  
  71. that reconstructs all the possible secrets Nikita can discover and that
  72. returns the set of ALL possible pairs (secret, CITY), where:
  73.  
  74. - secret is one of the possible secrets Nikita can discover, namely a
  75.        string obtained by the concatenation of the words collected,
  76.        separated by space
  77. - CITY   is the city reached by Nikita when she completed that secret.
  78.  
  79. Example:
  80.  
  81. If the file is 'example.txt', the starting city is 'ROMA' and the
  82. clues are "la bocca sollevò dal fiero pasto", then all the possible
  83. secret/city pairs will be:
  84.    ('vendita diamanti rubati stanotte ad anversa', 'CANCUN')
  85.    ('vendita cannoni mercato nero del cairo',      'CANCUN')
  86.    ('furto di diamanti a buckingham palace',       'MILANO')
  87.    ('mata hari ha sedotto ambasciatore zambia',    'MILANO')
  88.  
  89.  
  90. NOTE: it is forbidden to import/use other libraries or open files
  91.     except the one indicated
  92.  
  93. NOTE: The test system recognizes recursion ONLY if the recursive
  94.     function/method is defined in the outermost level.  DO NOT
  95.     define the recursive function within another function/method
  96.     otherwise you will fail all the tests.
  97.  
  98. """
  99.  
  100.  
  101. def treasure_hunt_ultimate(initial_city, clues_list, dictionary, index=0, sm='', ss=set()):
  102.     #base case
  103.     if len(clues_list) == index:
  104.         ss.add((sm, initial_city))
  105.         index = 0
  106.         return #nothing, just stop the recursion
  107.    
  108.     dict_key = initial_city + clues_list[index]
  109.    
  110.     if dict_key in dictionary:
  111.        
  112.         for elem in dictionary[dict_key]:
  113.             #in case there's more than one path and there's already a secret message
  114.             #(provided we're not considering the first element, when the message has just one word)
  115.             if len(dictionary[dict_key]) > 1 and len(sm) > 0 and dictionary[dict_key].index(elem)>0:
  116.                     l = sm.split()
  117.                     sm = sm.rstrip(l[-1])
  118.                     #basically, we eliminate the last word which doesn't belong
  119.    
  120.             #we concatenate the secret word to the secret message
  121.             sm += elem[1] if sm.endswith(' ') else ' ' + elem[1]
  122.             #we call treasure hunt with the new initial city
  123.             treasure_hunt_ultimate(elem[0], clues_list, dictionary, index+1, sm.lstrip(), ss)
  124.        
  125.     return ss
  126.  
  127.  
  128.  
  129. def  ex1(instructions_file, initial_city, clues):
  130.     f = open(instructions_file, encoding='utf-8')
  131.    
  132.     mother_dictionary = {}
  133.     CITYclue = ''
  134.     arrival_city = ''
  135.    
  136.     for line in f:
  137.         if line[0] != '#' and line != '\n':
  138.             instructions_list = line.split()
  139.             for instruction in instructions_list:
  140.                 #it'd be ideal if we did what you did before, but starting from the last char. You see where I'm going right? Of course I do
  141.                
  142.                 #we separate CITY2 and secret from CITY1clueCITY2secret
  143.                 index = -1
  144.                 while instruction[index].islower():
  145.                     index -= 1
  146.                 secret = instruction[index+1:]
  147.                
  148.                 while instruction[index].isupper():
  149.                     index -= 1
  150.                 CITY = instruction[index+1:].rstrip(secret)
  151.                
  152.                 #and we add it to a list of lists, the value of each dictionary
  153.                 #while the key of the dictionary will be CITY1clue
  154.                
  155.                 if instruction[:index+1] not in mother_dictionary:
  156.                     mother_dictionary[instruction[:index+1]] = []
  157.                 mother_dictionary[instruction[:index+1]].append([CITY, secret])
  158.            
  159.    
  160.             pass
  161.     clues_list = clues.split()
  162.     secret_message = treasure_hunt_ultimate(initial_city, clues_list, mother_dictionary)
  163.    
  164.     return secret_message
  165.  
  166.    
  167.    
  168.  
  169.    
  170.  
  171. #ex1('missing.txt', 'NEWYORK', 'one two three')
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement