# Generated from the published Markdown; run scripts/build_language_examples.py to refresh.
# Independent examples may reuse top-level names. Python remains the explanation language.


# topics/analysis/counting-work.md
def contains_with_count(values, target):
    comparisons = 0
    for value in values:
        comparisons += 1
        if value == target:
            return True, comparisons
    return False, comparisons

stations = ["KR", "MUS", "UTN", "CLM"]
print(contains_with_count(stations, "UTN"))  # (True, 3)
print(contains_with_count(stations, "BG"))   # (False, 4)

# topics/analysis/counting-work.md
def visit_count(values):
    visits = 0
    for value in values:
        visits += 1
    return visits

print(visit_count(["KR", "MUS", "UTN"]))  # 3
print(visit_count([]))                      # 0

# topics/analysis/counting-work.md
class Node:
    def __init__(self, value, next_node=None):
        self.value = value
        self.next = next_node

known = Node("KR", Node("UTN"))
new = Node("MUS")
new.next = known.next
known.next = new

# topics/analysis/code-and-space.md
def count_two_passes(values):
    work = 0
    for value in values:
        work += 1
    for value in values:
        work += 1
    return work

print(count_two_passes(["KR", "MUS", "UTN"]))  # 6

# topics/analysis/code-and-space.md
def count_pairs(n):
    full = 0
    for i in range(n):
        for j in range(n):
            full += 1

    triangular = 0
    for i in range(n):
        for j in range(i):
            triangular += 1
    return full, triangular

print(count_pairs(5))  # (25, 10)

# topics/analysis/code-and-space.md
def halving_steps(n):
    if n < 1:
        raise ValueError("n must be positive")
    steps = 0
    remaining = n
    while remaining > 1:
        remaining //= 2
        steps += 1
    return steps

print(halving_steps(16))  # 4
print(halving_steps(13))  # 3
print(halving_steps(1))   # 0

# topics/analysis/code-and-space.md
def inspect_count(values, detailed):
    if not values:
        return 0
    if not detailed:
        return 1
    return count_two_passes(values)

# topics/analysis/code-and-space.md
def count_values(values):
    count = 0
    for value in values:
        count += 1
    return count

def copy_values(values):
    result = []
    for value in values:
        result.append(value)
    return result
