Chapter 8 Assignments: Core Data Structures
Basic Questions
- Write a program to create a list of numbers from 1 to 10 using the
range()
function.
- Create a tuple with the names of five fruits and print the third fruit.
- Write a function that takes two numbers and returns their sum.
- Create a set with five unique integers and print its length.
- Write a dictionary to store three student names as keys and their scores as values.
- Convert a list of numbers to a tuple and print the tuple.
- Use a
for
loop to iterate over a list of strings and print each string.
- Create an OrderedDict with three key-value pairs and print its keys in insertion order.
- Write a function that returns the maximum of three numbers.
- Create a set, add two elements to it, and check if a specific element exists in the set.
Intermediate Questions
- Write a function that returns a list of even numbers from 1 to 50 using
range()
and list comprehension.
- Create a dictionary where keys are numbers from 1 to 5 and values are their squares.
- Given a tuple of integers, write code to find the sum of all elements.
- Write a function to merge two sets and return the resulting set without duplicates.
- Use an OrderedDict to maintain and print the order of insertion of items added dynamically in a loop.
- Create a function that accepts a dictionary and returns a list of keys sorted alphabetically.
- Write code to convert a string to a list of characters and then to a set to remove duplicates.
- Define a function that returns the first n Fibonacci numbers as a list using tuples.
- Use a list and dictionary to count the frequency of each character in a given string.
- Write a function to clear all items from a set and verify it is empty.
Advanced Questions
- Implement a function to reverse the keys and values of a dictionary and return a new dictionary.
- Write code to sort a list of tuples by the second element using
sorted()
and lambda functions.
- Create a nested dictionary representing a student's details (name, subjects, and scores) and print the average score.
- Write a function that finds common elements in two lists using set operations and returns them sorted.
- Use OrderedDict to implement a simple Least Recently Used (LRU) cache with capacity 3 for integer keys.
- Implement a function to flatten a nested list into a single list using recursion.
- Write code to count the frequency of words in a large text using a dictionary and then print the top 5 most common words.
- Create a function to perform deep copy of a dictionary containing nested dictionaries without using
copy.deepcopy()
.
- Write a function that accepts any number of lists and returns a merged set of unique elements.
- Implement a generator function that yields tuples of (index, value) for a list, mimicking
enumerate()
.