w3resource

Python: Data Structures - Exercises, Practice, Solution

Data Structures: [7 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

1. Write a Python program to locate the left insertion point for a specified value in sorted order. Go to the editor
Expected Output:
4
2
Click me to see the sample solution

2. Write a Python program to locate the right insertion point for a specified value in sorted order. Go to the editor
Expected Output:
3
2
Click me to see the sample solution

3. Write a Python program to insert items into a list in sorted order. Go to the editor
Expected Output:
Original List:
[25, 45, 36, 47, 69, 48, 68, 78, 14, 36]
Sorted List:
[14, 25, 36, 36, 45, 47, 48, 68, 69, 78]
Click me to see the sample solution

4. Write a Python program to create a queue and display all the members and size of the queue. Go to the editor
Expected Output:
Members of the queue:
0 1 2 3
Size of the queue:
4
Click me to see the sample solution

5. Write a Python program to find whether a queue is empty or not. Go to the editor
Expected Output:
True
False
Click me to see the sample solution

6. Write a Python program to create a FIFO queue. Go to the editor
Expected Output:
0 1 2 3
Click me to see the sample solution

7. Write a Python program to create a LIFO queue. Go to the editor
Expected Output:
3 2 1 0
Click me to see the sample solution

 

Python Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Python: Time it

timeit library is great for timing Python execution times. Just pass a function in string format to timeit.timeit() method and it will carry out 1 million executions to report the minimum time the code takes.

Its very useful to compare small code pieces and different functions but can be sluggish with big code.

Check out the example below demonstrating the execution time difference between 2 very similar list comprehension methods in Python:

import timeit
lst1='''list(range(100))'''
lst2='''[i for i in range(100)]'''
a=timeit.timeit(lst1)
b=timeit.timeit(lst2)
print(a, b, sep="------")

Output:

1.9301698543131351------5.389458132907748