# List Operations and Filtering Homework

# Popcorn Hack 1: Data Collection Methods
# -------------------------------------------------
# Question: What's wrong with this method of data collection?
# 
# score1 = 87
# score2 = 90
# score3 = 85
# score4 = 70
# score5 = 80
# 
# Answer:
# This method uses separate variables for each score, which becomes unwieldy.
# A better approach is using a list:
# 
scores = [87, 90, 85, 70, 80]
# 
# Benefits of using lists:
# - Easier to iterate through with loops
# - Built-in methods for manipulation
# - Better organization of related data
# - More scalable for larger datasets


# Popcorn Hack 2: List Operations
# -------------------------------------------------
# Question: What does this code output?
# 
# items = ["pen", "pencil", "marker", "eraser"]
# items.remove("pencil")
# items.append("sharpener")
# print(items[2])
# 
# Answer: The code outputs: eraser
# 
# Here's what happens step by step:
# 1. Initial list: ["pen", "pencil", "marker", "eraser"]
# 2. After removing "pencil": ["pen", "marker", "eraser"]
# 3. After adding "sharpener": ["pen", "marker", "eraser", "sharpener"]
# 4. items[2] accesses the element at index 2, which is "eraser"


# Popcorn Hack 3: Real-World Filtering Algorithms
# -------------------------------------------------
# Question: What are some real-world examples of filtering algorithms?
# 
# Answer: Filtering algorithms appear in many applications:
# 
# - Social Media → filter posts by friends → Only friends' content (new list)
# - Online Shopping → filter by rating >= 4 stars → Highly-rated products (new list)
# - Video Streaming → filter by category == "Documentary" → Only documentaries (new list)
# 
# A filtering algorithm typically:
# 1. Examines each item in a collection
# 2. Tests each item against specific criteria
# 3. Creates a new collection with only the items that passed the test

# 1) Create a list with at least five items
favorite_games = ["Skyrim", "The Witcher 3", "Elden Ring", "Cyberpunk 2077", "Baldur's Gate 3"]

# List procedure 1: Append - adds a new item to the end of the list
favorite_games.append("Starfield")  # Adding a new game to the list
print("After append:", favorite_games)

# List procedure 2: Sort - arranges elements in alphabetical order
favorite_games.sort()  # Sorting games alphabetically
print("After sort:", favorite_games)

# List procedure 3: Remove - removes a specific element
favorite_games.remove("Starfield")  # Removing a specific game
print("After remove:", favorite_games)

# 2) List Traversal
print("\nTraversing the list with a for loop:")
for game in favorite_games:  # Using a for loop to go through each item one by one
    print(f"I enjoy playing {game}")

# 3) Filtering Algorithm using pandas
import pandas as pd

# Convert list to pandas DataFrame
games_df = pd.DataFrame(favorite_games, columns=["Game"])

# Add a column for game length (estimated hours)
games_df["Length"] = [300, 150, 100, 80, 120]

print("\nOriginal DataFrame:")
print(games_df)

# Filtering condition: Games that take over 100 hours to complete
long_games = games_df[games_df["Length"] > 100]

print("\nFiltered DataFrame (games longer than 100 hours):")
print(long_games)

# Manual filtering using list comprehension
long_games_list = [game for i, game in enumerate(favorite_games) if games_df.iloc[i]["Length"] > 100]
print("\nLong games using list comprehension:", long_games_list)

# Final Reflection
"""
Filtering algorithms and lists are used in everyday applications like streaming services, which filter vast catalogs
of content based on user preferences to create personalized recommendations. e-commerce websites use filtering
to help customers narrow down product choices based on specified criteria like price range, ratings, or features.
"""
After append: ['Skyrim', 'The Witcher 3', 'Elden Ring', 'Cyberpunk 2077', "Baldur's Gate 3", 'Starfield']
After sort: ["Baldur's Gate 3", 'Cyberpunk 2077', 'Elden Ring', 'Skyrim', 'Starfield', 'The Witcher 3']
After remove: ["Baldur's Gate 3", 'Cyberpunk 2077', 'Elden Ring', 'Skyrim', 'The Witcher 3']

Traversing the list with a for loop:
I enjoy playing Baldur's Gate 3
I enjoy playing Cyberpunk 2077
I enjoy playing Elden Ring
I enjoy playing Skyrim
I enjoy playing The Witcher 3



Original DataFrame:
              Game  Length
0  Baldur's Gate 3     300
1   Cyberpunk 2077     150
2       Elden Ring     100
3           Skyrim      80
4    The Witcher 3     120

Filtered DataFrame (games longer than 100 hours):
              Game  Length
0  Baldur's Gate 3     300
1   Cyberpunk 2077     150
4    The Witcher 3     120

Long games using list comprehension: ["Baldur's Gate 3", 'Cyberpunk 2077', 'The Witcher 3']





'\nFiltering algorithms and lists are used in everyday applications like streaming services, which filter vast catalogs\nof content based on user preferences to create personalized recommendations. e-commerce websites use filtering\nto help customers narrow down product choices based on specified criteria like price range, ratings, or features.\n'