# Up until now, we've written functions that either return lists or 
# that do only simple aggregations of data in lists that were already 
# built in (like sum). Sometimes, we need to aggregate data in ways 
# that aren't built in. These notes introduce "for loops", a general
# purpose construct for doing aggregations (nearly every language 
# provides them in some form)

# Example: take a list of words and concatenate them all together 
# into a single string

def concat_all(words: list[str]) -> str:
    """concatenate all strings in list into one string, without spaces"""
    result = "" # what to return if list is empty
    # build up the result string, one word at a time
    for w in words:  # w is like the lam variable in filter/map
        # augment the result so far with w (the next elt of the list)
        result = result + w
    # when we run out of elements, return the answer we've built up
    return result

# examples of concat_all
def test_concat_all():
    assert concat_all(["hello", "brown", "bear"]) == "hellobrownbear"
    assert concat_all(["b", "A", "g"]) == "bAg"
    assert concat_all([]) == ""

# built in functions like sum and length follow the same pattern.
# Note where these functions differ from concat_all

def sum(nums: list[int]) -> int:
    """compute sum of items in a list"""
    result = 0 # what to return if list is empty
    for w in nums:
        # augment the result so far with w(one elt of the list)
        result = result + w
    # when we run out of elements, return the answer we've built up
    return result

def length(items: list) -> int:
    """count number of items in a list"""
    result = 0 # what to return if list is empty
    for w in items:
        # augment the result so far with w(one elt of the list)
        result = result + 1
    # when we run out of elements, return the answer we've built up
    return result

# ===================================

# Now, let's see how this pattern works on a more complicated aggregation.
# Here, we will aggregate a list of votes into a data structure that
# summarizes how many votes were cast per candidate

from dataclasses import dataclass

# datatype for storing counts in an election among candidates blue, green, purple
@dataclass
class Tally:
    blue: int
    green: int
    purple: int

# Here's how to define a list in Python -- just square brackets, no list: needed
VOTES = ["green", "purple", "purple", "blue", "green"]

def tally_votes(votes: list[str]) -> Tally:
    "Generate tally summarizing the number of votes per candidate"
    result = Tally(0, 0, 0)  #the default if list is empty
    for v in votes:
        if v == "blue": 
            result.blue = result.blue + 1
        elif v == "green": 
            result.green = result.green + 1
        elif v == "purple": 
            result.purple = result.purple + 1
    return result

def test_tally_votes():
    assert tally_votes([]) == Tally(0,0,0)
    assert tally_votes(["purple", "purple"]) == Tally(0,0,2)
    assert tally_votes(VOTES) == Tally(1, 2, 2)