# ============================================================
# HOMEWORK: Debugging & Code Practice
# ============================================================
# Two kinds of work in here:
#   PART 1 - Complete the Code  (write the missing logic)
#   PART 2 - Test & Debug       (write tests, watch them break,
#                                then track down exactly why)
#
# The whole point of PART 2 is the loop a real developer lives in:
#   write a test  ->  run it  ->  something's wrong  ->  find it  ->  fix it
# Don't skip straight to "reading" the bug. Run things first.
#
# At the very bottom there is a BONUS in each part that is a step
# harder. Each bonus comes with a tip - read it only after you've
# been stuck for a few minutes.
# ============================================================


# ============================================================
# PART 1: COMPLETE THE CODE
# ============================================================
# Each function has a description of what it SHOULD do and test
# lines showing expected results. Fill in the body.


# --- TASK 1A ---
# Return how many vowels (a, e, i, o, u) are in the given text.
# Capital letters count too: "Apple" has 2 vowels.

def count_vowels(text):
    # YOUR CODE HERE
    pass


# print(count_vowels("hello"))      # 2
# print(count_vowels("Volcanic"))   # 4
# print(count_vowels("xyz"))        # 0


# --- TASK 1B ---
# Return True if word reads the same forwards and backwards,
# otherwise return False. Assume it's already lowercase, no spaces.

def is_palindrome(word):
    # YOUR CODE HERE
    pass


# print(is_palindrome("racecar"))   # True
# print(is_palindrome("python"))    # False
# print(is_palindrome("level"))     # True


# --- TASK 1C ---
# Turn a numeric score (0-100) into a letter grade:
#   90 and above  -> "A"
#   80 to 89      -> "B"
#   70 to 79      -> "C"
#   below 70      -> "F"

def grade(score):
    # YOUR CODE HERE
    pass


# print(grade(95))   # A
# print(grade(82))   # B
# print(grade(70))   # C
# print(grade(50))   # F


# --- TASK 1D ---
# Complete the two methods so the Inventory works.
#   add(item, qty)    -> store the item with its quantity.
#                        If the item already exists, add to its quantity.
#   total_items()     -> return the total quantity of everything combined.

class Inventory:
    def __init__(self):
        self.stock = {}

    def add(self, item, qty):
        # YOUR CODE HERE
        pass

    def total_items(self):
        # YOUR CODE HERE
        pass


# bag = Inventory()
# bag.add("potion", 3)
# bag.add("sword", 1)
# bag.add("potion", 2)
# print(bag.stock)         # {'potion': 5, 'sword': 1}
# print(bag.total_items()) # 6


# --- BONUS 1E (harder) ---
# "Run-length encoding". Squash repeated characters into the
# character followed by how many times it repeated in a row.
#   "aaabbc"  -> "a3b2c1"
#   "xyz"     -> "x1y1z1"
#   ""        -> ""
# Single characters still get a count of 1.

def compress(text):
    # YOUR CODE HERE
    pass


# print(compress("aaabbc"))   # a3b2c1
# print(compress("xyz"))      # x1y1z1
# print(compress(""))         # (empty string)
#
# TIP (read only if stuck): Trace "aaab" on paper first. As you walk
# the string you need to remember TWO things at once: which character
# you're currently counting, and how many in a row you've seen. The
# tricky part is deciding the exact moment you "lock in" a finished
# run and start a new one - including the very last run.


# ============================================================
# PART 2: TEST & DEBUG
# ============================================================
# Each function below is SUPPOSED to do what its description says,
# but at least one is lying to you. For EACH task:
#
#   1. Write 2-3 test calls in the space provided (use print()).
#   2. Run the file and look at the real output.
#   3. When a result is wrong (or it crashes), find the exact line
#      and fix it.
#   4. In the ANSWER space, write one sentence: what was the bug?
#
# Don't read the code first looking for the mistake. Tests first.


# --- TASK 2A ---
# Should return the largest number in the list.

def find_max(numbers):
    biggest = 0
    for n in numbers:
        if n > biggest:
            biggest = n
    return biggest


# Write your tests here (try a list of all-negative numbers too):
#
#
# ANSWER (what was the bug?):
#


# --- TASK 2B ---
# Should count how many times target appears in the list.

def count_occurrences(items, target):
    count = 0
    for item in items:
        if item == target:
            count = 1
    return count


# Write your tests here:
#
#
# ANSWER (what was the bug?):
#


# --- TASK 2C ---
# Should return the average (mean) of the numbers in the list.

def average(numbers):
    total = 0
    for n in numbers:
        total = total + n
        average = total / len(numbers)
    return average


# Write your tests here:
#
#
# ANSWER (what was the bug?):
#


# --- TASK 2D ---
# Should return a list of the even numbers from 1 up to and
# including n. There is more than one thing wrong here.

def evens_up_to(n):
    result = []
    for i in range(n):
        if i % 2 == 1:
            result.append(i)
    return result


# Write your tests here (check evens_up_to(10) carefully):
#
#
# ANSWER (what were the bugs?):
#


# --- BONUS 2E (harder) ---
# Should hand each new player a fresh, empty backpack and put their
# first item in it. Every player starts with NOTHING but that item.

def new_backpack(first_item, backpack=[]):
    backpack.append(first_item)
    return backpack


# Write your tests here. Important: call new_backpack THREE separate
# times for three different players, and print each result:
#
#
# ANSWER (what was the bug?):
#
# TIP (read only if stuck): The function looks fine and even passes if
# you only test it once. The trouble only shows up across repeated
# calls. The bug is NOT inside the loop or the append - look at the
# very first line where the function is defined, and ask yourself when
# that "empty" backpack actually gets created.


# ============================================================
# DONE! Save the file and bring it to our next lesson.
# We'll walk through the bugs and the bonuses together.
# ============================================================
