Skip to content

Latest commit

 

History

History
366 lines (292 loc) · 13.8 KB

File metadata and controls

366 lines (292 loc) · 13.8 KB

Magic Square Program Explanation


Introduction:

In this set of videos, the presenter explains the algorithm for generating magic squares and demonstrates a Python program that implements the algorithm. A magic square is a square matrix where the sum of each row, column, and diagonal is the same. The program is designed to work with odd-sized magic squares (e.g., 3x3, 5x5, 7x7).

Algorithm Explanation and Initialization:

  • Magic squares are square matrices with equal sums for rows, columns, and diagonals.
  • The algorithm is demonstrated for a 3x3 magic square.
  • The location of elements is defined using the 'x, y' coordinates.

Example:

n = 3
magic_square = [[0 for _ in range(n)] for _ in range(n)]
# The matrix is initialized with zeros.

Creating the Magic Square Function

  • Introduces the idea of encapsulating code in a function to make it reusable.
  • Demonstrates how to create a matrix with zeros.
  • Prints the matrix in a readable format.

Code:

def magic_square(n):
    magic_square = [[0 for _ in range(n)] for _ in range(n)]
    # Filling the matrix with zeros.
    for i in range(n):
        for j in range(n):
            print(magic_square[i][j], end="\t")
        print()
n = 3  
magic_square(n)

Example Output:

0	0	0
0	0	0
0	0	0

Filling the Magic Square

  • Explains the algorithm to fill the magic square.
  • Conditions for positioning the number '1'.
  • Conditions for filling subsequent numbers.

Code:

def magic_square(n):
    magic_square = [[0 for _ in range(n)] for _ in range(n)]
    i = n // 2
    j = n - 1
    num = n * n
    count = 1

    while count <= num:
        if i == -1 and j == n:
            j = n - 2
            i = 0
        if j == n:
            j = 0
        if i < 0:
            i = n - 1
        if magic_square[i][j] != 0:
            j -= 2
            i += 1
            continue
        else:
            magic_square[i][j] = count
            count += 1
        j += 1
        i -= 1

Example Output: A sample output for a 3x3 magic square:

2	7	6
9	5	1
4	3	8

Conclusion: The presenter has explained the algorithm for generating magic squares and provided a working Python program. The program fills the magic square following the defined rules and produces a magic square with the specified size.

Note: The provided code is simplified and does not include the functionality to calculate the sums of rows, columns, and diagonals to verify the magic square properties.

*NOTE :Magic Constant = (n * (n^2 + 1)) / 2

Dobble Game


Explaination:

The Dobble game is a fast-paced card game that involves identifying matching symbols between pairs of cards. It's also known as Spot It! in some regions. Here's a brief explanation of the game:

  1. Deck of Cards: Dobble uses a deck of cards, with each card featuring a set of symbols. There are multiple symbols on each card.
  2. Common Symbol: Any two cards in the deck share exactly one matching symbol. This common symbol is the key to the game.
  3. Game Objective: The objective is to identify and call out the common symbol between two cards faster than your opponents.
  4. Gameplay:
    • Two players are typically involved in each round.
    • One player flashes two cards, and the other player must quickly spot the common symbol.
    • The player who correctly identifies the common symbol scores a point.
  5. Variations: There can be different variations of Dobble, each with its own set of symbols and cards. The basic principle of spotting the common symbol remains the same.
  6. Challenging: The game may seem simple, but it can be challenging. The symbols are strategically arranged to make it harder to spot the common one quickly.
  7. Quick Rounds: Rounds are usually fast, making it suitable for a quick and fun game session.
  8. Winning: The player with the most points at the end of the game wins.
  9. Educational: Dobble can also be used as an educational tool, helping improve observation skills, visual perception, and quick thinking.
  10. A Fun Party Game: It's a popular game for parties, family gatherings, and social events due to its simplicity and entertainment value.
  11. Skill and Luck: Success in the game depends on a combination of visual observation skills and a bit of luck in drawing cards.
  12. Expansions: There are different versions and expansions of Dobble, with unique symbols and gameplay twists.

Code:

import string
import random

# Step 1: Set up Symbols
symbols = list(string.ascii_letters)

# Step 2: Create Cards
pos1 = random.randint(0, 4)
pos2 = random.randint(0, 4)

# Step 3: Choose a Common Symbol
same_symbol = random.choice(symbols)
symbols.remove(same_symbol)

# Step 4: Assign Symbols to the Cards
card1 = [None] * 5
card2 = [None] * 5

if pos1 == pos2:
    card1[pos1] = same_symbol
    card2[pos2] = same_symbol
else:
    card1[pos1] = same_symbol
    card2[pos2] = same_symbol

    alphabet1 = random.choice(symbols)
    symbols.remove(alphabet1)
    alphabet2 = random.choice(symbols)
    symbols.remove(alphabet2)

    card1[pos2] = alphabet1
    card2[pos1] = alphabet2

# Step 5: Check for Remaining Positions
i = 0
while i < 5:
    if i != pos1 and i != pos2:
        alphabet1 = random.choice(symbols)
        symbols.remove(alphabet1)
        alphabet2 = random.choice(symbols)
        symbols.remove(alphabet2)

        card1[i] = alphabet1
        card2[i] = alphabet2
    i += 1

# Step 6: Player Input
ch = input("Spot the common symbol: ")

# Step 7: Check for Correct Answer
if ch == same_symbol:
    print("Right!")
else:
    print("Wrong!")

# Example Output:
# (Assuming the common symbol is 'A' and player inputs 'A')
# Output: Spot the common symbol: A
#         Right!

Explanation:

  • This code simulates the Dobble game by generating two cards, each containing a common symbol.
  • The program first sets up a list of symbols (letters) and chooses two positions for the common symbol on the two cards.
  • It selects a random common symbol, ensuring it's not repeated in the cards or the symbol list.
  • Then, it assigns the common symbol and unique symbols to the two cards.
  • For the remaining positions on the cards, unique symbols are assigned.
  • The player is prompted to input a character to spot the common symbol.
  • Finally, the player's input is checked, and if it matches the common symbol, "Right!" is printed; otherwise, "Wrong!" is printed.

The example output demonstrates the game's interaction, where the player successfully spots the common symbol ('A' in this case), resulting in "Right!" being printed.

Example Output:

Here's an example of the output when running the code:

Spot the common symbol: S Right!

This output indicates that the player successfully spotted the common symbol.

Birthday Paradox


Problem Statement:

  • The Birthday Paradox explores the surprising probability of shared birthdays within a group of people.
  • It aims to answer the question: How large must a group be for there to be a significant probability that at least two people share the same birthday?
  • The paradox states that even in relatively small groups, the likelihood of shared birthdays is higher than most people expect.
  • The core challenge is to determine the minimum group size required for a better-than-even chance of a shared birthday.
  • This problem highlights the counterintuitive nature of probability, demonstrating that probabilities can differ from our intuitive expectations.

Concepts :

  1. Birthday Paradox:

    • The central problem explored in the transcript.
    • The counterintuitive phenomenon where the likelihood of shared birthdays is higher than expected in relatively small groups.
    • The focus of the discussion, exploring the probability of shared birthdates within a group.
  2. Random Number Generation:

    • The process of generating random integers for year, month, and day to simulate birthdates.
    • Achieved using the random library in Python.
    • Birthdates are generated within specific ranges to reflect real-world scenarios.
  3. Leap Years:

    • Mentioned in the context of February.
    • Explains that February can have either 28 or 29 days depending on whether it's a leap year.
    • Leap years are identified by divisibility by 4, with exceptions for century years.
  4. Datetime Library:

    • Used for date and time manipulation.
    • Demonstrated how to extract and format current date, month, day, week number, and more.
    • Utilized for extracting the day of the year, day of the month, and day of the week.
  5. Data Sorting:

    • Employed to organize the generated birthdates for analysis.
    • Sorting is essential for identifying shared birthdays easily.
    • The sorted list of birthdates aids in finding collisions (shared birthdays).
  6. Probability Calculation:

    • The main objective is to determine the probability of shared birthdays.
    • The more people you survey, the higher the probability of at least one shared birthday.
    • Simulated experiments show how this probability increases as the group size grows.

Code :

import random

def is_shared_birthday(num_people):
    birthdays = [random.randint(1, 365) for _ in range(num_people)]
    return len(birthdays) != len(set(birthdays))

def birthday_paradox_simulation(num_simulations, num_people):
    shared_count = 0
    for _ in range(num_simulations):
        if is_shared_birthday(num_people):
            shared_count += 1
    return shared_count / num_simulations

num_simulations = 10000
num_people = 23  # You can change the group size

probability = birthday_paradox_simulation(num_simulations, num_people)
print(f"Probability of shared birthdays in a group of {num_people} people: {probability:.2f}")

# Sample Output
# Probability of shared birthdays in a group of 23 people: 0.51

Sample Output:

The output will vary from run to run due to the random nature of the simulation, but it should look something like this:

Probability of shared birthdays in a group of 23 people: 0.51

In this example, we simulated the Birthday Paradox by running 10,000 experiments with a group of 23 people. The code calculates the probability of at least one shared birthday within that group, and the sample output indicates that there's a 51% chance of shared birthdays in this scenario.

Guess the Movie


Movie Guessing Game

  • Introduction: The text discusses a simple movie guessing game implemented in Python. The game involves two players taking turns to guess the name of a randomly selected movie from a list.

  • Initial Setup:

    • Players enter their names.
    • Players start with zero points.
    • Use of variables like "turn" and "willing" to manage turns and game continuation.
  • Gameplay Logic:

    • Players take turns guessing a letter present in the movie name.
    • The code randomly selects a movie from a list.
    • A "question" is created, where letters are displayed as spaces or stars.
    • Players guess letters. If the letter is in the movie name, the letter is revealed.
    • Players can unlock other characters if the letter they guess is not in the movie name.
  • Scoring:

    • Correct guesses increase the player's points by one.
    • The game can continue or end based on the player's choice.
    • If the player wants to quit, the game ends, and the scores are displayed.
  • Handling Repetition: It's mentioned that there might be repetitions in the movie selection due to the random nature of movie picking. The text suggests increasing the number of movies in the list to reduce repetition.

  • Code Improvements:

    • Suggestion to improve code by making common functionalities reusable in functions.
    • Recommendations for implementing different game variants, such as adding constraints or variations in point systems.
  • Encouragement for Discussion:

    • Encouragement to discuss the game implementation and ideas in online discussion forums.
    • Promotes clear learning and sharing knowledge among the programming community.

Now, let's provide a sample Python code based on the explanation:

import random

def create_question(movie_name):
    question = ""
    for char in movie_name:
        if char == ' ':
            question += ' '
        else:
            question += '*'
    return question

def unlock_letter(movie_name, question, letter):
    output = ""
    for i in range(len(movie_name)):
        if movie_name[i] == letter:
            output += letter
        else:
            output += question[i]
    return output

def play_game():
    players = [input("Player one, enter your name: "), input("Player two, enter your name: ")]
    scores = [0, 0]
    movies = ["movie1", "movie2", "movie3", "movie4", "movie5", "movie6", "movie7", "movie8", "movie9", "movie10"]

    while True:
        for i in range(2):
            print(f"{players[i]}'s turn:")
            movie = random.choice(movies)
            question = create_question(movie)
            print(question)

            while '*' in question:
                letter = input("Guess a letter: ")
                if letter in movie:
                    print("Correct!")
                    question = unlock_letter(movie, question, letter)
                else:
                    print("Letter not found.")
                print(question)

            scores[i] += 1
            print(f"{players[i]} guessed the movie: {movie}")
            print(f"{players[i]}'s score is {scores[i]}")

        choice = input("Continue the game (1) or quit (0)? ")
        if choice == '0':
            break

    print(f"Game Over!\n{players[0]}'s score: {scores[0]}\n{players[1]}'s score: {scores[1]}")

play_game()

Please note that this is a simplified version of the game, and you can enhance it with additional features and improvements as mentioned in the text. The code allows two players to take turns guessing movie names. It will continue until the players decide to quit.