SZZ materiály

Algoritmizace a programování I a II

Užitečné odkazy

Programování: funkce a cykly

Řešení ukázkové úlohy

def test_of_parentheses(text: str, parentheses: tuple[str, str]) -> bool:
    if not text.strip():
        raise ValueError("Text is empty")

    left, right = parentheses
    count = 0

    for character in text:
        if character == left:
            count += 1
        elif character == right:
            count -= 1
        
        if count < 0:
            return False

    return count == 0

Programování: kolekce

Řešení ukázkové úlohy

def increment_even(data: list) -> list:
    new_data = []

    for index, item in enumerate(data):
        if not isinstance(item, int):
            raise TypeError("Item is not a number")
            
        new_data.append(item + index % 2)

    return new_data

Programování: základy OOP

Řešení ukázkové úlohy

class Semaphore:
    colors = ["red", "yellow", "green"]

    def __init__(self, color: str):
        if color not in Semaphore.colors:
            raise ValueError("Unsupported color")
        self.color = color

    @property
    def stop(self):
        return self.color in ("red", "yellow")

    def __str__(self):
        return self.color

    def __eq__(self, other: "Semaphore"):
        return self.color == other.color

    def __iter__(self):
        return self

    def __next__(self):
        index = self.colors.index(self.color)
        self.color = self.colors[(index + 1) % len(self.colors)]
        return self