Zaawansowane funkcje funkcji


    
    1. Więcej o funkcji int()
    def convertList(values, bases):
      # Użyj listy składanej do wykonania konwersji
      return [int(str(x), y) for x, y in zip(values, bases)]


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

    2. Ćwiczenie - złe wartości
    def convertList(values, bases):
      # Inicjuj pustą listę na nieprawidłowe wartości
      invalid_values = []
      
      # Iteruj przez listę wartości i odpowiadające im bazy
      for value, base in zip(values, bases):
          try:
              # Spróbuj dokonać konwersji
              int(value, base=base)
          except ValueError:
              # Jeśli wystąpi błąd ValueError, dodaj wartość do listy nieprawidłowych
              invalid_values.append(value)
      
      return invalid_values


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

    3. Argumenty opcjonalne
    def increment(x, by=None):
      if by is not None:
          return x + by
      else:
          return x + 1

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

    4. Ćwiczenie - cena brutto
    def grossPrice(net_price, tax_rate=23):
      # Oblicz cenę brutto na podstawie ceny netto i stawki podatku
      gross_price = net_price * (1 + tax_rate / 100)
      
      # Zaokrąglij cenę brutto do pełnych centów
      gross_price = round(gross_price, 2)
      
      return gross_price


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

    5. Ćwiczenie - drugie imię
    def retName(first_name, last_name, middle_name=None):
      if middle_name:
          full_name = f'{first_name} {middle_name} {last_name}'
      else:
          full_name = f'{first_name} {last_name}'
      
      return full_name


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

    6. Podawanie nazw argumentów
    def make_dict(x):
      # Utwórz słownik z kluczem 'number' i wartością x
      return {'number': x}

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

    7. Ćwiczenie - wolna powierzchnia
    def cargoSpace(area=240, business=0, premium=0, economy=0):
      print("Available space:", round(area - business * 2 - premium * 1.6 - economy * 1.2, 2))
  
    cargoSpace(premium=12, economy=145)


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

    8. Ćwiczenie - ostateczna cena
    def finalPrice(price, tax=23, discount=0):
      # Oblicz cenę po uwzględnieniu podatku
      price_with_tax = price * (1 + tax / 100)
      
      # Oblicz cenę po uwzględnieniu rabatu
      final_price = price_with_tax * (1 - discount / 100)
      
      # Zaokrąglij cenę do pełnych centów
      final_price = round(final_price, 2)
      
      return final_price


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

    9. Ćwiczenie - ostateczna cena 2
    def finalPrice(price, /, tax=23, discount=0):
      final_price = price + (price * (tax / 100))
      final_price -= (final_price * (discount / 100))
      return round(final_price, 2)

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

    10. Ćwiczenie - ostateczna cena 3
    def finalPrice(price, *, tax=23, discount=0):
      final_price = price + (price * (tax / 100))
      final_price -= (final_price * (discount / 100))
      return round(final_price, 2)



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

    11. Ćwiczenie - parseInt
    import re
    def parseInt(x, / ,base=10, * ,changeEndianness=False):
        x = int(str(x), base)
        
        if changeEndianness:
            #converted num TO hex
            x = hex(x)[2:]
            if len(x) % 2 == 1:
                x = '0' + x
            
            #swapping    
            x = re.findall('..', x)
            x.reverse()
            x = ''.join(map(str, x))
            
            return int(x, 16)
        return x
    
 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    12. Funkcje o dowolnej liczbie argumentów
    def operation(op, *args):
      if op == "+":
          result = sum(args)
      elif op == "*":
          result = 1
          for arg in args:
              result *= arg
      else:
          raise ValueError("Nieobsługiwane działanie: " + op)
      
      return result
    
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    13. Ćwiczenie - wielkie litery
    def toUpper(text, *letters):
      result = text
      
      for letter in letters:
          result = result.replace(letter.lower(), letter.upper())
      
      return result



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

    14. Ćwiczenie - usuń hasła
    def delKeys(dictionary, *keys_to_delete):
      result = dictionary.copy()
      
      for key in keys_to_delete:
          if key in result:
              del result[key]
      
      return result
      

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

    15. Funkcje o dowolnej liczbie nazwanych argumentów
    def filter_kwargs(modulo, **kwargs):
      filtered_args = []
      
      for key, value in kwargs.items():
          if isinstance(value, int) and value % modulo == 0:
              filtered_args.append(key)
      
      return filtered_args

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

    16. Ćwiczenie - zaprzęg św. Mikołaja
    def setHarness(a,**kwargs):
      b=0
      c=[]
      for i in sorted(kwargs,key=lambda x: kwargs[x],reverse=True):
          if i in ('Dasher', 'Dancer', 'Prancer', 'Vixen', 'Comet', 'Cupid', 'Donner', 'Blitzen','Rudolph'):
              if b