Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. #skrár: flights.txt  -> lesa úr henni
  2. # inniheldur: nöfn lönd
  3. # notum split()
  4. # munum þurfa nota dict þar sem nöfnin eru key
  5. # 1. prenta út öll nöfn í stafrófsröð
  6. # 2. prenta út þau lönd sem þau hafa heimsótt
  7. # 3. prenta út nafn sá aðila sem hefur ferðast til flestra landa og hversu marga landa hann hvefur farið til
  8.  
  9. #read_file  -> skilar lista af listum
  10. #my_dict  -> skilar dict nafn = key   lönd = value
  11. #max_flyers
  12. """
  13. def check_file(filename):
  14.    try:
  15.        with open(filename, 'r') as f:
  16.            return f
  17.    except FileNotFoundError:
  18.        return None
  19. """
  20.  
  21. def read_file(filename):
  22.     flyer_list = []
  23.     with open(filename, 'r') as f:
  24.         for line in f:
  25.             flyer_list.append(line.split())
  26.     return flyer_list
  27.  
  28.  
  29.  
  30. def make_dict(flyer_list):
  31.     flight_dict = {}
  32.     # loop through the inner list of our f_list
  33.     #check if names in innerlist are in dict, if so add country to the key
  34.     for inner_list in flyer_list:
  35.         name = inner_list[0]
  36.         country = inner_list[1]
  37.         if name in flight_dict:
  38.             flight_dict[name].append(country)
  39.         else:
  40.             flight_dict[name] = [country]
  41.     return flight_dict
  42.  
  43. def clean_dict(flight_dict):
  44.     #flight_dict = sorted(flight_dict)
  45.  
  46.     for flyer in flight_dict:           #for flyer, country þá sækir bæði key og value
  47.         flight_dict[flyer] = sorted(set(flight_dict[flyer]))
  48.     return sorted(flight_dict.items())
  49.  
  50.  
  51.  
  52. def print_flight(final_dict):
  53.     for t in final_dict:
  54.         print(t[0] + ':')
  55.         country_list = t[1]
  56.         for country in country_list:
  57.             print('\t'+country)
  58.  
  59.  
  60. def frequent_flyer(flight_list):
  61.     max_val = 0
  62.     max_name = ''
  63.     for t in flight_list:
  64.         name = t[0]
  65.         country_list = t[1]
  66.         curr_val = len(country_list)
  67.         if curr_val > max_val:
  68.             max_val = curr_val
  69.             max_name = name
  70.     print('{} has been to {} countries'.format(max_name, max_val))
  71.  
  72.  
  73.  
  74. def main():
  75.     #read from file
  76.     filename = ('/Users/kingamaris/Desktop/2019-T-111-PROG-master 3/projects/p9/flights.txt')
  77.     if filename is None:
  78.         print()
  79.     else:
  80.         flyer_list = read_file(filename)
  81.         flight_dict = make_dict(flyer_list)
  82.         flight_list = clean_dict(flight_dict)
  83.         print_flight(flight_list)
  84.         frequent_flyer(flight_list)
  85.     #make dict from list of lists from file
  86.     #print names
  87.     #print names and countries
  88.     #print frequent flyer
  89.    
  90.  
  91.  
  92. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement