189k views
0 votes
Write a program that creates a Person class that contains string that represent the first and last name of a person and their age. You will need to create a Queue class that will store each person in the queue and can sort the queue based on last name or age.

Prompt the user of the program to add five people to the queue Your program should provide the contents of the queue and then sort the queue using the quick sort in two ways:
Descending order by last name.
Descending order by age.

User Marlhex
by
7.1k points

1 Answer

3 votes

Final answer:

In response to the question, we define a Person class and a Queue class with methods for sorting based on last name or age using quicksort.

The Queue class contains a set of Person instances and provides functionality to sort the queue in descending order by the specified attribute.

Step-by-step explanation:

To create a program that fulfills the requirements, we would define a Person class with attributes for first name, last name, and age. We would also define a Queue class to manage a collection of Person instances. Below is an example implementation with methods to sort by last name and age using quicksort.

First, we declare the Person class:

class Person:
def __init__(self, first_name, last_name, age):
self.first_name = first_name
self.last_name = last_name
self.age = age

Next, we define the Queue class:

class Queue:
def __init__(self):
self.people = []

def enqueue(self, person):
self.people.append(person)

def sort_by_last_name_desc(self):
self.people = self.quicksort(self.people, key=lambda x: x.last_name, reverse=True)

def sort_by_age_desc(self):
self.people = self.quicksort(self.people, key=lambda x: x.age, reverse=True)

# The quicksort method would be defined here

The program would prompt the user to add five people to the queue, display the contents, and sort it in descending order by last name and age.

User LanderTome
by
7.6k points