Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconAlgorithms and Data Structures with Python
Algorithms and Data Structures with Python

Chapter 3: Elementary Data Containers

Chapter 3: Practical Exercises of Elementary Data Containers

Exercise 1

Create a Python list of the first ten prime numbers and then use slicing to obtain the 3rd to 7th prime numbers from the list.

Solution:

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
subset_primes = primes[2:7]
print(subset_primes)  # Output: [5, 7, 11, 13, 17]

Exercise 2

Create a set of vowels and then determine if the letter 'a' and letter 'b' are in the set.

Solution:

vowels = {'a', 'e', 'i', 'o', 'u'}
print('a' in vowels)  # Output: True
print('b' in vowels)  # Output: False

Exercise 3

Define a dictionary that stores the atomic number (as the key) and symbol (as the value) for the elements Hydrogen, Helium, and Carbon. Fetch the symbol for Helium.

Solution:

elements = {
    1: 'H',    # Hydrogen
    2: 'He',   # Helium
    6: 'C'     # Carbon
}
print(elements[2])  # Output: He

Exercise 4

Create a basic class Rectangle that has attributes for height and width and a method to calculate the area.

Solution:

class Rectangle:
    def __init__(self, height, width):
        self.height = height
        self.width = width

    def area(self):
        return self.height * self.width

# Example Usage:
rect = Rectangle(5, 10)
print(rect.area())  # Output: 50

Exercise 5

Implement a simple stack using Python lists. The stack should have methods to push, pop, and check if the stack is empty.

Solution:

class Stack:
    def __init__(self):
        self.items = []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        if not self.is_empty():
            return self.items.pop()

    def is_empty(self):
        return len(self.items) == 0

# Example Usage:
stack = Stack()
stack.push(5)
stack.push(10)
print(stack.pop())  # Output: 10
print(stack.is_empty())  # Output: False

Exercise 6

Create a singly linked list with nodes that store integers. Implement a method to append a new node to the end of the list.

Solution:

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last_node = self.head
        while last_node.next:
            last_node = last_node.next
        last_node.next = new_node

# Example Usage:
ll = LinkedList()
ll.append(5)
ll.append(10)

These exercises are designed to reinforce the concepts discussed in Chapter 3 and provide hands-on experience with Python's built-in data structures, object-oriented principles, and some elementary data structures.

Chapter 3: Practical Exercises of Elementary Data Containers

Exercise 1

Create a Python list of the first ten prime numbers and then use slicing to obtain the 3rd to 7th prime numbers from the list.

Solution:

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
subset_primes = primes[2:7]
print(subset_primes)  # Output: [5, 7, 11, 13, 17]

Exercise 2

Create a set of vowels and then determine if the letter 'a' and letter 'b' are in the set.

Solution:

vowels = {'a', 'e', 'i', 'o', 'u'}
print('a' in vowels)  # Output: True
print('b' in vowels)  # Output: False

Exercise 3

Define a dictionary that stores the atomic number (as the key) and symbol (as the value) for the elements Hydrogen, Helium, and Carbon. Fetch the symbol for Helium.

Solution:

elements = {
    1: 'H',    # Hydrogen
    2: 'He',   # Helium
    6: 'C'     # Carbon
}
print(elements[2])  # Output: He

Exercise 4

Create a basic class Rectangle that has attributes for height and width and a method to calculate the area.

Solution:

class Rectangle:
    def __init__(self, height, width):
        self.height = height
        self.width = width

    def area(self):
        return self.height * self.width

# Example Usage:
rect = Rectangle(5, 10)
print(rect.area())  # Output: 50

Exercise 5

Implement a simple stack using Python lists. The stack should have methods to push, pop, and check if the stack is empty.

Solution:

class Stack:
    def __init__(self):
        self.items = []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        if not self.is_empty():
            return self.items.pop()

    def is_empty(self):
        return len(self.items) == 0

# Example Usage:
stack = Stack()
stack.push(5)
stack.push(10)
print(stack.pop())  # Output: 10
print(stack.is_empty())  # Output: False

Exercise 6

Create a singly linked list with nodes that store integers. Implement a method to append a new node to the end of the list.

Solution:

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last_node = self.head
        while last_node.next:
            last_node = last_node.next
        last_node.next = new_node

# Example Usage:
ll = LinkedList()
ll.append(5)
ll.append(10)

These exercises are designed to reinforce the concepts discussed in Chapter 3 and provide hands-on experience with Python's built-in data structures, object-oriented principles, and some elementary data structures.

Chapter 3: Practical Exercises of Elementary Data Containers

Exercise 1

Create a Python list of the first ten prime numbers and then use slicing to obtain the 3rd to 7th prime numbers from the list.

Solution:

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
subset_primes = primes[2:7]
print(subset_primes)  # Output: [5, 7, 11, 13, 17]

Exercise 2

Create a set of vowels and then determine if the letter 'a' and letter 'b' are in the set.

Solution:

vowels = {'a', 'e', 'i', 'o', 'u'}
print('a' in vowels)  # Output: True
print('b' in vowels)  # Output: False

Exercise 3

Define a dictionary that stores the atomic number (as the key) and symbol (as the value) for the elements Hydrogen, Helium, and Carbon. Fetch the symbol for Helium.

Solution:

elements = {
    1: 'H',    # Hydrogen
    2: 'He',   # Helium
    6: 'C'     # Carbon
}
print(elements[2])  # Output: He

Exercise 4

Create a basic class Rectangle that has attributes for height and width and a method to calculate the area.

Solution:

class Rectangle:
    def __init__(self, height, width):
        self.height = height
        self.width = width

    def area(self):
        return self.height * self.width

# Example Usage:
rect = Rectangle(5, 10)
print(rect.area())  # Output: 50

Exercise 5

Implement a simple stack using Python lists. The stack should have methods to push, pop, and check if the stack is empty.

Solution:

class Stack:
    def __init__(self):
        self.items = []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        if not self.is_empty():
            return self.items.pop()

    def is_empty(self):
        return len(self.items) == 0

# Example Usage:
stack = Stack()
stack.push(5)
stack.push(10)
print(stack.pop())  # Output: 10
print(stack.is_empty())  # Output: False

Exercise 6

Create a singly linked list with nodes that store integers. Implement a method to append a new node to the end of the list.

Solution:

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last_node = self.head
        while last_node.next:
            last_node = last_node.next
        last_node.next = new_node

# Example Usage:
ll = LinkedList()
ll.append(5)
ll.append(10)

These exercises are designed to reinforce the concepts discussed in Chapter 3 and provide hands-on experience with Python's built-in data structures, object-oriented principles, and some elementary data structures.

Chapter 3: Practical Exercises of Elementary Data Containers

Exercise 1

Create a Python list of the first ten prime numbers and then use slicing to obtain the 3rd to 7th prime numbers from the list.

Solution:

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
subset_primes = primes[2:7]
print(subset_primes)  # Output: [5, 7, 11, 13, 17]

Exercise 2

Create a set of vowels and then determine if the letter 'a' and letter 'b' are in the set.

Solution:

vowels = {'a', 'e', 'i', 'o', 'u'}
print('a' in vowels)  # Output: True
print('b' in vowels)  # Output: False

Exercise 3

Define a dictionary that stores the atomic number (as the key) and symbol (as the value) for the elements Hydrogen, Helium, and Carbon. Fetch the symbol for Helium.

Solution:

elements = {
    1: 'H',    # Hydrogen
    2: 'He',   # Helium
    6: 'C'     # Carbon
}
print(elements[2])  # Output: He

Exercise 4

Create a basic class Rectangle that has attributes for height and width and a method to calculate the area.

Solution:

class Rectangle:
    def __init__(self, height, width):
        self.height = height
        self.width = width

    def area(self):
        return self.height * self.width

# Example Usage:
rect = Rectangle(5, 10)
print(rect.area())  # Output: 50

Exercise 5

Implement a simple stack using Python lists. The stack should have methods to push, pop, and check if the stack is empty.

Solution:

class Stack:
    def __init__(self):
        self.items = []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        if not self.is_empty():
            return self.items.pop()

    def is_empty(self):
        return len(self.items) == 0

# Example Usage:
stack = Stack()
stack.push(5)
stack.push(10)
print(stack.pop())  # Output: 10
print(stack.is_empty())  # Output: False

Exercise 6

Create a singly linked list with nodes that store integers. Implement a method to append a new node to the end of the list.

Solution:

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last_node = self.head
        while last_node.next:
            last_node = last_node.next
        last_node.next = new_node

# Example Usage:
ll = LinkedList()
ll.append(5)
ll.append(10)

These exercises are designed to reinforce the concepts discussed in Chapter 3 and provide hands-on experience with Python's built-in data structures, object-oriented principles, and some elementary data structures.