Real World Practice Questions in Python

  1. Audience Seating Arrangement

    You are tasked with writing a program to arrange an audience in a hall with specific seating requirements. The hall has 5 rows, and each row contains 10 seats. The audience consists of four types of attendees:

    1. Chief Guests
    2. Teachers
    3. Parents
    4. Students

    Each type of attendee should be represented by a unique number:

    • Chief Guests = 1
    • Teachers = 2
    • Parents = 3
    • Students = 4

    Your program should:

    1. Arrange attendees in rows such that:
      • Groups of each type are seated in the middle of the row when possible.
      • Overflowing attendees are moved to the next row.
      • Only one type of attendee is allowed in a row. Another type of attendee should move to the next row, if available.
    2. Ensure that the total number of attendees does not exceed the hall’s capacity (50 seats).
    3. Raise an appropriate error message if there are not enough rows to seat everyone.
    4. Return the seating arrangement as a 2D list, where each sublist represents a row and each element represents a seat.

    Input:

    Write a function arrange_audience(students, teachers, chief_guests, parents) that takes the following arguments:

    • students (int): Number of students attending.
    • teachers (int): Number of teachers attending.
    • chief_guests (int): Number of chief guests attending.
    • parents (int): Number of parents attending.

    Output:

    The function should return a 2D list representing the seating arrangement. Each element of the list represents a row of seats, with numbers indicating the attendee type or 0 for an empty seat.


    Constraints:
    1. The total audience should not exceed 50 attendees. Raise an error with the message:
      "Total number of audience exceeds the capacity of the hall."
    2. If the number of attendees cannot fit within the 5 rows, raise an error with the message:
      "Not enough rows to seat all {audience_type}."
      Replace {audience_type} with the name of the attendee type.

    Example:
    # Input
    chief_guests = 1 teachers = 8 parents = 2
    students = 11 # Function Call arrange_audience(students, teachers, chief_guests, parents) # Output Seating Arrangement: Row 1: [0, 0, 0, 0, 1, 0, 0, 0, 0, 0] Row 2: [0, 0, 2, 2, 2, 2, 2, 2, 0, 0] Row 3: [0, 0, 0, 0, 3, 3, 0, 0, 0, 0] Row 4: [4, 4, 4, 4, 4, 4, 4, 4, 4, 4] Row 5: [0, 0, 0, 0, 4, 0, 0, 0, 0, 0]

    Tasks:
    1. Write the function arrange_audience as described above.
    2. Test your function with various inputs, including edge cases:
      • Total audience is exactly 50.
      • Overflow of a single attendee type.
      • Audience counts where some rows are completely empty.
     
  2. Divide Students into Batches Based on Levels

    You are tasked with dividing students into batches based on their academic levels. The objective is to ensure that each batch contains students with levels that do not differ by more than 2 levels. Each batch should have students from consecutive levels where possible, and the total number of batches should be minimized.

    The batch assignment should follow these constraints:

    1. A batch should contain students from consecutive levels, where the difference between the highest and lowest level in any batch is at most 2.
    2. If a batch has students from different levels, the levels must be grouped in ascending order starting from any level.
    3. The student levels are integers between 1 and 10, inclusive.
    4. Each student has a unique student ID, and their levels are provided as input.
    5. The number of batches should be minimized while respecting the above constraints.
    Input:
    • An integer total_students representing the total number of students (1 <= total_students <= 1000).
    • A dictionary student_levels where the keys are unique student IDs (strings) and the values are their academic levels (integers between 1 and 10).
    Output:

    The function should return a tuple:

    1. A dictionary where the keys are batch names (e.g., “Batch 1”, “Batch 2”, …) and the values are lists of student IDs in each batch.
    2. An integer representing the total number of batches created.
    Constraints:
    • The total number of batches should be minimized.
    • The difference between the highest and lowest level in a batch should not exceed 2.
    • Student levels are integers between 1 and 10.
    Example:
    Input:
    total_students = 6
    student_levels = {
        "S001": 1,
        "S002": 3,
        "S003": 2,
        "S004": 4,
        "S005": 3,
        "S006": 6
    }
    
    Function Call:
    batches, batch_count = divide_into_batches(total_students, student_levels)
    
    Output:
    Batch Division:
    Batch 1: ['S001', 'S003', 'S002', 'S005']
    Batch 2: ['S004', 'S006']
    
    Total Batches Created: 2
    
    Task:

    Write the function divide_into_batches(total_students, student_levels) that divides students into batches as described above, ensuring that the number of batches is minimized and that each batch adheres to the level difference constraint.

    Edge Cases:
    • A single student in the input (should result in one batch).
    • All students being from the same level (should be in one batch).
    • Students with levels distributed across the full range from 1 to 10.
  1. You are tasked with writing a program to arrange an audience in a hall with specific seating requirements. The hall has 5 rows, and each row contains 10 seats. The audience consists of four types of attendees:

    1. Chief Guests
    2. Teachers
    3. Parents
    4. Students

    Each type of attendee should be represented by a unique number:

    • Chief Guests = 1
    • Teachers = 2
    • Parents = 3
    • Students = 4

    Your program should:

    1. Arrange attendees in rows such that:
      • Groups of each type are seated in the middle of the row when possible.
      • Overflowing attendees are moved to the next row.
      • Only one type of attendee is allowed in a row. Another type of attendee should move to the next row, if available.
    2. Ensure that the total number of attendees does not exceed the hall’s capacity (50 seats).
    3. Raise an appropriate error message if there are not enough rows to seat everyone.
    4. Return the seating arrangement as a 2D list, where each sublist represents a row and each element represents a seat.

    Input:

    Write a function arrange_audience(students, teachers, chief_guests, parents) that takes the following arguments:

    • students (int): Number of students attending.
    • teachers (int): Number of teachers attending.
    • chief_guests (int): Number of chief guests attending.
    • parents (int): Number of parents attending.

    Output:

    The function should return a 2D list representing the seating arrangement. Each element of the list represents a row of seats, with numbers indicating the attendee type or 0 for an empty seat.


    Constraints:
    1. The total audience should not exceed 50 attendees. Raise an error with the message:
      "Total number of audience exceeds the capacity of the hall."
    2. If the number of attendees cannot fit within the 5 rows, raise an error with the message:
      "Not enough rows to seat all {audience_type}."
      Replace {audience_type} with the name of the attendee type.

    Example:
    # Input
    chief_guests = 1 teachers = 8 parents = 2
    students = 11 # Function Call arrange_audience(students, teachers, chief_guests, parents) # Output Seating Arrangement: Row 1: [0, 0, 0, 0, 1, 0, 0, 0, 0, 0] Row 2: [0, 0, 2, 2, 2, 2, 2, 2, 0, 0] Row 3: [0, 0, 0, 0, 3, 3, 0, 0, 0, 0] Row 4: [4, 4, 4, 4, 4, 4, 4, 4, 4, 4] Row 5: [0, 0, 0, 0, 4, 0, 0, 0, 0, 0]

    Tasks:
    1. Write the function arrange_audience as described above.
    2. Test your function with various inputs, including edge cases:
      • Total audience is exactly 50.
      • Overflow of a single attendee type.
      • Audience counts where some rows are completely empty.
     
  1. You are tasked with dividing students into batches based on their academic levels. The objective is to ensure that each batch contains students with levels that do not differ by more than 2 levels. Each batch should have students from consecutive levels where possible, and the total number of batches should be minimized.

    The batch assignment should follow these constraints:

    1. A batch should contain students from consecutive levels, where the difference between the highest and lowest level in any batch is at most 2.
    2. If a batch has students from different levels, the levels must be grouped in ascending order starting from any level.
    3. The student levels are integers between 1 and 10, inclusive.
    4. Each student has a unique student ID, and their levels are provided as input.
    5. The number of batches should be minimized while respecting the above constraints.
    Input:
    • An integer total_students representing the total number of students (1 <= total_students <= 1000).
    • A dictionary student_levels where the keys are unique student IDs (strings) and the values are their academic levels (integers between 1 and 10).
    Output:

    The function should return a tuple:

    1. A dictionary where the keys are batch names (e.g., “Batch 1”, “Batch 2”, …) and the values are lists of student IDs in each batch.
    2. An integer representing the total number of batches created.
    Constraints:
    • The total number of batches should be minimized.
    • The difference between the highest and lowest level in a batch should not exceed 2.
    • Student levels are integers between 1 and 10.
    Example:
    Input:
    total_students = 6
    student_levels = {
        "S001": 1,
        "S002": 3,
        "S003": 2,
        "S004": 4,
        "S005": 3,
        "S006": 6
    }
    
    Function Call:
    batches, batch_count = divide_into_batches(total_students, student_levels)
    
    Output:
    Batch Division:
    Batch 1: ['S001', 'S003', 'S002', 'S005']
    Batch 2: ['S004', 'S006']
    
    Total Batches Created: 2
    
    Task:

    Write the function divide_into_batches(total_students, student_levels) that divides students into batches as described above, ensuring that the number of batches is minimized and that each batch adheres to the level difference constraint.

    Edge Cases:
    • A single student in the input (should result in one batch).
    • All students being from the same level (should be in one batch).
    • Students with levels distributed across the full range from 1 to 10.