Rzutowanie, zbiory i słowniki


    1. Rzutowanie typów
    def joinData(data):
      if len(data) == 2 and isinstance(data[1], int) and data[1] > 0:
          height_in_meters = data[1] / 100.0
          height_str = "{:.2f}".format(height_in_meters)
          
          result = "{}: {}".format(data[0], height_str)
          return result
      else:
          return "Błędne dane"

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    2. Metoda łańcuchów: split()
    def selectAndSum(s):
      total_sum = 0
      
      elements = s.replace(' ', '').split(',')
      
      for element in elements:
          if element.isdigit():
              total_sum += int(element)
      
      return total_sum

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    3. Funkcje input() i round()
    def brutto(x):
      y = x.split()
      return str( round( float( y[0] ) * 1.08, 2 ) ) + " " + y[1]
    
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    4. Ćwiczenie: dni pracy
    from datetime import datetime

    def englishBreakfast(agent_data):
        result = []
        current_date = datetime.now()
        
        for agent in agent_data:
            agent_code, hire_date_str = agent
            hire_date = datetime.strptime(hire_date_str, "%Y%m%d")
            days_employed = (current_date - hire_date).days
            result.append([agent_code, days_employed])
        
        return result
    
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    5. Zbiory, set()
    def countDistinct(x):
      unique_aircrafts = set(x)
      return len(unique_aircrafts)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    6. Metoda add()
    def setSet(x):
      logins_set = set()
      
      for item in x:
          if len(item) >= 1:
              login = item[0]
              logins_set.add(login)
      
      return logins_set
    
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    7. Metody remove() i discard()
    def clearData(x, y):
      result_set = set()
      
      for item in x:
          kwota, data = item
          if data <= y:
              result_set.add(item)
      
      return result_set

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    8. Metody clear(), copy(), porównanie zbiorów
    def isEqual(x, y):
      if x == y:
          return True
      else:
          return False
    
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    9. Suma zbiorów
    def getEmails(s1, s2):
      email_set = set()
  
      for entry in s1:
          email, consent = entry
          if consent is True or consent == "True":
              email_set.add(email)

      for entry in s2:
          email, consent = entry
          if consent is True or consent == "True":
              email_set.add(email)
  
      return email_set
          
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    10. Część wspólna zbiorów
    def getFeatures(x):
      if not x:
          return set()
  
      common_features = set(x[0])

      for survey in x[1:]:
          common_features = common_features.intersection(set(survey))
  
      return common_features

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    11. Interpol 1
    def findSuspects(x, y, z):
      x_set = set(x)
      y_set = set(y)
      z_set = set(z)
  
      common_passports = x_set.intersection(y_set).union(x_set.intersection(z_set)).union(y_set.intersection(z_set))
  
      return common_passports
 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    12. Różnica zbiorów
    def getCustomers(myBase, theirBase):
      my_customers = set(myBase)
      their_customers = set(theirBase)
      new_customers = their_customers.difference(my_customers)
  
      return new_customers
    
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    13. Różnica symetryczna
    def mutation1(m1, m2):
      set_m1 = set(m1)
      set_m2 = set(m2)
      unique_mutations = set_m1.symmetric_difference(set_m2)
  
      return unique_mutations

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    14. Słowniki
    def findName(d, n):
      for company in d:
          if company.get("NIP") == n:
              return company.get("name")
      return "Data not found"

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    15. Zmiana wartości
    def complete(x):
      for key, value in x.items():
          if value == "":
              x[key] = False
      return x

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    16. Dodanie nowego elementu słownika
    def convert(x):
      converted_dict = {str(key): value for key, value in x.items()}
      return converted_dict

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    17. Usuwanie elementów
    import time

    def update(x):
        current_date = time.strftime("%Y%m%d")
        five_years_ago = str(int(current_date[:4]) - 5) + current_date[4:]
    
        filtered_dict = {key: value for key, value in x.items() if value[1] >= five_years_ago}
        return filtered_dict

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    18. Metoda fromkeys()
    def is_prime(n):
      if n <= 1:
          return False
      if n <= 3:
          return True
      if n % 2 == 0 or n % 3 == 0:
          return False
      i = 5
      while i * i <= n:
          if n % i == 0 or n % (i + 2) == 0:
              return False
          i += 6
      return True

    def setDict(x):
        result_dict = {number: None for number in x}
        for key in result_dict:
            if is_prime(key):
                result_dict[key] = "prime"
        return result_dict
    
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    19. Metody keys(), values(), items()
    def compareKeys(x, y):
      return set(x.keys()) == set(y.keys())

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    20. Metody zbiorów
    def checkPrisoners(t, i):
      x = set(t.items())
      y = set(i.items())
  
      return x.issubset(y)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    21. Ćwiczenie: policz znaki
    def countChars(s):
      char_count = {}
  
      for char in s:
          char = char.upper()
          
          if char in char_count:
              char_count[char] += 1
          else:
              char_count[char] = 1
  
      return char_count
      
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    22. Cwiczenie: delivery robots 
    import delivery

    def getTemp(x):
        return delivery.get(x)['data']['engineTemp']

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    23. Ćwiczenie: suma tras
    import delivery

    def totalRoute():
        robot_numbers = delivery.robots()
    
        total_distance = 0
    
        for robot_number in robot_numbers:
            robot_data = delivery.get(robot_number)
    
            if 'data' in robot_data and 'distanceToday' in robot_data['data']:
                today_distance = robot_data['data']['distanceToday']
                total_distance += today_distance
    
        return total_distance
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    24. Ćwiczenie: ładowanie
    import delivery

    def chargeFloat():
        robot_numbers = delivery.robots()
    
        for robot_number in robot_numbers:
            robot_data = delivery.get(robot_number)
    
            if 'data' in robot_data and 'batteryCharge' in robot_data['data']:
                battery_charge = robot_data['data']['batteryCharge']
    
                if battery_charge < 25:
                    delivery.charge(robot_number)
    
    chargeFloat()
    
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    25. Ćwiczenie: tłok na trasie

    import delivery

    def keyPoints():
        robot_numbers = delivery.robots()
        
        all_points = set()
    
        for robot_number in robot_numbers:
            robot_data = delivery.get(robot_number)
    
            if 'route' in robot_data:
                all_points.update(robot_data['route'])
    
        if len(robot_numbers) > 0:
            common_points = all_points.copy()
    
            for robot_number in robot_numbers:
                robot_data = delivery.get(robot_number)
                if 'route' in robot_data:
                    common_points.intersection_update(robot_data['route'])
    
            if common_points:
                return common_points
    
        return 0