Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Data structures exercises

1.   Make a list L1 that contains every integer from 1 to 30.

2.   Make a second list L2 containing only the even numbers from L1 and print it.

3.   Make a third list L3 that contains the first 5 elements of L2. Make L3 = []; Use the append method and indexing or slicing.

4.   Save the word computingin a variable. Using slicing, print only put’ .

5.   Given a list of integers and a number N, find the Nth largest element in the list. The element will be stored in the Nth_max variable. E.g. 1st  largest, 2nd  largest, etc.

You are allowed to use all list operations and built in functions; your code should work for any value of N (assuming N isn’t larger than the list length).

Try to make this into a function: find_n_max(list, n)

My_list = [30, 21, 16, 66, 78, 109, 1, 4, 52]

6.   Make a function that finds the largest number in the list from ex. 5, using a For loop:

a.   You will need to keep a record of the largest value seen so far;

b.   You will need to use if statements to compare the current value to the largest value seen so far.

Your function should return the largest number.

7.   Given a list of integers, separate and count values in the following categories:

If a number is divisible by 4 or bigger than 50 – high category; If a number is neither of the above low category.

Make and print two lists, one for each category, containing all numbers that fit in it. Print a list containing how many numbers of each category exist in the format:

H_L = [number_of_high, number_of_low].

Integers = [20, 9, 51, 81, 50, 42, 77]

8.   Write a program that will unpack a tuple into separate variables:

Tuple = (1,3,5,7); create 4 variables, each containing one of the elements.

9.   Modify your code for exercise 4 such that it creates a new tuple containing the squared values of the original elements, in the same order.

E.g. new_tup = (1,9,25,49)

10. Given a list of tuples produce a new list of tuples where each tuple element is multiplied by the tuple’s index in the list.

L = [(2,4,6), (1,3,5), (10,20,30)]

Your output should look like [(0,0,0), (1,3,5), (20,40,60)].

11. Given two lists: [1,2,3,4,5] and [1,2,3,4,5]:

Use for loops to print the result from multiplying each value of list 1 with each value of list 2, e.g. :  1*1, 1*2, 1*3 … 2*1, 2*2, … 5*5.

12. Make a while loop that counts down from 10. Print each number to the console, but:

Skip 6, stop the loop when you reach 2.

Extension exercises:

13. Write a Python program to print the following pattern, using a nested for loop.

*

* *

* * *

* * * *

* * * * *

* * * *

* * *

* * *

14. Given a string containing only square brackets, [], you must check whether the brackets are balanced or not. The brackets are said to be balanced if, for every opening bracket, there is a closing bracket.

You will write your code in the check_balance() function, which returns True if the brackets are balanced and False if they are not.

For an empty string, the function will return True.

For the sake of simplicity, you can assume that the string will not contain any other characters.

Examples :

[ [ [ ] ] ] – balanced; [[][]] – balanced; [[] – unbalanced; ][][ - unbalanced.

15. You must implement the check_sum() function which takes in a list and returns True if the sum of two numbers in the list is zero. If no such pair exists, return False.

Example: The list L = 10,15,-6, 2, 6, 7, 21 contains -6 and 6, which summed are 0. The function should return True here.

16. the Fibonacci sequence is a series of numbers where every number is the sum of the two numbers before it. Here are the first few numbers: 0, 1, 1, 2, 3, 5, 8, 13 …  You must write the fib() function which takes in a positive integer, n, and returns the n-th Fibonacci number. Your function must use any of the loops.

Example: n = 7, output = 8.