Give an example of iteration.

for i in range(1, 11):
    print(i)
1
2
3
4
5
6
7
8
9
10

What is the difference between a for loop and while loop? That is, when would you use a for loop and when would you use a while loop?

The main difference between a for loop and a while loop is that a for loop is typically used when you know the number of times you want to execute a block of code, while a while loop is used when you want to execute a block of code repeatedly until a certain condition is met.

In the APCSP AP exam, what number do indexes start with? important to know

indexes start at 0. This means that the first element in an array, list, or other data structure has an index of 0, the second element has an index of 1, and so on.

Are dictionaries and lists mutable?

Yes, both dictionaries and lists are mutable in Python.


Explain an example of something you could simulate.

One thing that could be simulated is the performance of a wind farm. Wind farms consist of multiple wind turbines that generate electricity by harnessing wind energy. To simulate the performance of a wind farm, we could use data such as the wind speed and direction, the size and type of turbines, and the capacity of the electrical grid to model how the wind turbines would generate electricity under different conditions.

Why are simulations useful and important?

Simulations can be used to model a wide range of phenomena, such as weather patterns, traffic flow, and the spread of infectious diseases. They can also be used to simulate the behavior of financial markets, electronic devices, and ecosystems. Additionally, simulations can be used to optimize the performance of systems and processes, as well as to train people in a safe and controlled environment. Finally, simulations can be used to forecast potential outcomes and inform decision-making about the future.

Create a visual or written description. Objective is to compare lists and dictionaries.

Lists and dictionaries are two of the most commonly used data structures in Python. While they may appear similar at first glance, there are important differences between them that make them suited for different tasks.

A list is an ordered collection of elements, where each element is identified by its position or index within the list. Lists are created using square brackets [], and elements are separated by commas. Lists are often used to store a collection of similar objects, such as a list of numbers or a list of names.

A dictionary, on the other hand, is an unordered collection of key-value pairs, where each key is associated with a value. Dictionaries are created using curly braces {}, with each key-value pair separated by a colon. Dictionaries are often used to store information that can be looked up by a unique key, such as a dictionary of words and their definitions.

One important difference between lists and dictionaries is that lists are indexed by their position, while dictionaries are indexed by keys. This means that accessing elements in a list is based on their position, while accessing elements in a dictionary is based on their keys. Additionally, while lists can contain duplicate elements, each key in a dictionary must be unique.

In terms of performance, accessing an element in a list by its position is faster than accessing an element in a dictionary by its key. However, dictionaries are more efficient than lists when it comes to looking up elements by their key.

countries = {
    "China": 1393,
    "India": 1366,
    "United States": 329,
    "Indonesia": 270,
    "Pakistan": 225,
    "Brazil": 213,
    "Nigeria": 206,
    "Bangladesh": 165,
    "Russia": 146,
    "Mexico": 130
}

# sort the countries by population in ascending order using bubble sort
def bubble_sort(dict):
    sorted_dict = dict.copy()
    for i in range(len(sorted_dict)-1):
        for j in range(len(sorted_dict)-i-1):
            if sorted_dict[list(sorted_dict.keys())[j]] > sorted_dict[list(sorted_dict.keys())[j+1]]:
                sorted_dict[list(sorted_dict.keys())[j]], sorted_dict[list(sorted_dict.keys())[j+1]] = sorted_dict[list(sorted_dict.keys())[j+1]], sorted_dict[list(sorted_dict.keys())[j]]
    return sorted_dict

print("Countries sorted by population in ascending order using bubble sort:")
print(bubble_sort(countries))

# sort the countries by population in descending order using merge sort
def merge_sort(dict):
    if len(dict) <= 1:
        return dict
    else:
        mid = len(dict)//2
        left_dict = dict.copy()
        right_dict = dict.copy()
        for i in range(mid):
            del right_dict[list(right_dict.keys())[0]]
        for i in range(mid, len(left_dict)):
            del left_dict[list(left_dict.keys())[mid]]
        left_dict = merge_sort(left_dict)
        right_dict = merge_sort(right_dict)
        return merge(left_dict, right_dict)

def merge(left, right):
    result = {}
    while len(left) > 0 and len(right) > 0:
        if left[list(left.keys())[0]] >= right[list(right.keys())[0]]:
            result[list(left.keys())[0]] = left[list(left.keys())[0]]
            del left[list(left.keys())[0]]
        else:
            result[list(right.keys())[0]] = right[list(right.keys())[0]]
            del right[list(right.keys())[0]]
    while len(left) > 0:
        result[list(left.keys())[0]] = left[list(left.keys())[0]]
        del left[list(left.keys())[0]]
    while len(right) > 0:
        result[list(right.keys())[0]] = right[list(right.keys())[0]]
        del right[list(right.keys())[0]]
    return result

print("Countries sorted by population in descending order using merge sort:")
print(merge_sort(countries))
Countries sorted by population in ascending order using bubble sort:
{'China': 130, 'India': 146, 'United States': 165, 'Indonesia': 206, 'Pakistan': 213, 'Brazil': 225, 'Nigeria': 270, 'Bangladesh': 329, 'Russia': 1366, 'Mexico': 1393}
Countries sorted by population in descending order using merge sort:
{'China': 1393, 'India': 1366, 'United States': 329, 'Indonesia': 270, 'Pakistan': 225, 'Brazil': 213, 'Nigeria': 206, 'Bangladesh': 165, 'Russia': 146, 'Mexico': 130}

simulation

import random

def roll_dice(num_dice):
    dice = [1, 2, 3, 4, 5, 6]
    rolls = []
    for i in range(num_dice):
        roll = random.choice(dice)
        rolls.append(roll)
    rolls.sort(reverse=True)
    return rolls

def attack(attacker, defender):
    attacker_dice = min(attacker - 1, 3)
    defender_dice = min(defender, 2)
    attacker_rolls = roll_dice(attacker_dice)
    defender_rolls = roll_dice(defender_dice)
    for i in range(min(attacker_dice, defender_dice)):
        if attacker_rolls[i] > defender_rolls[i]:
            defender -= 1
        else:
            attacker -= 1
    return attacker, defender

def play_game(num_armies):
    player1_armies = num_armies
    player2_armies = num_armies
    while player1_armies > 1 and player2_armies > 1:
        print(f"Player 1 has {player1_armies} armies and Player 2 has {player2_armies} armies.")
        player1_attacker = int(input("Player 1, how many armies do you want to attack with? "))
        while player1_attacker < 2 or player1_attacker >= player1_armies:
            player1_attacker = int(input("Invalid number of armies. Please choose a number between 2 and {player1_armies-1}: "))
        player1_defender = int(input("Player 1, how many armies do you want to defend with? "))
        while player1_defender < 1 or player1_defender > min(player2_armies, 2):
            player1_defender = int(input(f"Invalid number of armies. Please choose a number between 1 and {min(player2_armies, 2)}: "))
        player2_attacker = random.randint(2, min(player2_armies, 3))
        player2_defender = random.randint(1, min(player1_armies - 1, 2))
        print(f"Player 1 attacks with {player1_attacker} armies and defends with {player1_defender} armies.")
        print(f"Player 2 attacks with {player2_attacker} armies and defends with {player2_defender} armies.")
        player1_armies, player2_armies = attack(player1_attacker, player2_defender)
        if player2_armies > 1:
            player2_armies, player1_armies = attack(player2_attacker, player1_defender)
    if player1_armies > 1:
        print("Player 1 wins!")
    else:
        print("Player 2 wins!")
        
play_game(10)
Player 1 has 10 armies and Player 2 has 10 armies.
Player 1 attacks with 3 armies and defends with 2 armies.
Player 2 attacks with 3 armies and defends with 1 armies.
Player 1 wins!

Kahoot

if the image dont work: i got 2438

i went to the bathroom during the majority of the game, so i could not answer all of the questions