## Saturday, October 17, 2020

Exercises On Python Lists

#### Execise 1 || Solution

write python algorithm to display all element of given list by two differents ways.

#### Exercise 2 || Solution

write python algorithm to swap the first element with the last element of given list. Example: if L =
``["Python" , "Java" , "C++" , "Javascript"]``
, the algorithm returns the list:
``["Javascript",  "Java" , "C++" , "Python"]``

#### Exercise 3 || Solution

write a python algorithm as function that takes as parameter a list l and returns a tuple of two lists (l_old , l_even) where l_old is make up by the elements of l of even index and l_old is make up by the list element of odd index. Example: if:
``L = ["Python" , "Java" , "C++" , "C#" , "VB.Net" , "Javascript"]``
the algorithm return :
``(['Python', 'C++', 'VB.Net'], ['Java', 'C#', 'Javascript'])``

#### Exercise 4 || Solution

Write a program in Python that asks the user to enter 5 integers of their choice and display the list of numbers entered.

#### Exercise 5 || Solution

Given a list of integers L, write a program in Python that returns the sum of the list L elements.

#### Exercise 6 || Solution

Write a Python algorithm that returns the length of a given list without using the len() method.

#### Exercise7 || Solution

Write a Python algorithm that returns the list of divisors of a given integer. Example if n = 18, the algorithm returns the list [1, 2, 3, 6, 9, 18]

#### Exercise8 || Solution

Write an algorithm in python that returns the average of the terms in a list of numbers.

#### Exercise9 || Solution

Write an algorithm in python as a function which takes as parameter a tuple (L, a) formed by a list L and an element a and which returns True if the element a is present in the list L and False if no.

#### Exercise 10 || Solution

Write an algorithm in python as a function which takes as a parameter a tuple (L, x) formed by a list L and an element x and which returns the position of the element x in the list L. The function must return -1 if element x is not in the list.

#### Exercise 11 || Solution

Write an algorithm in python as a function which takes as parameter a tuple (L, n) formed by a list L of numbers and an integer n and which returns the list obtained from L by multiplying its elements by n . Example if L = [3, 9, 5, 23] and n = 2 the function returns the list: [6, 18, 10, 46]

#### Exercise 12 || Solution

Using the sort () method, write an algorithm in python as a function which takes as parameter a list L of numbers and which returns the couple (min, max) formed by the minimum and the maximum of the list.

#### Exercise 13 || Solution

Write a python algorithm to remove duplicate elements from a list.

#### Exercise14 || Solution

Write a Python program as a function that takes two lists as parameters and returns True if the two lists have at least one common element and False if not.

#### Exercise 15 || Solution

Write a Python program in the form of a Python function which takes two lists as parameters and returns the list of elements common to these two lists.

#### Exercise 16 || Solution

Write a program in python that randomly mixes the elements of a given list.

#### Exercise17 || Solution

Write a Python program that determines the difference of two lists. Example if: L1 = [11, 3, 22, 7, 13, 23, 9] L2 = [5, 9, 19, 23, 10, 23, 13] the program returns the list: [11, 3, 22, 7]

#### Exercise 18 || Solution

Write a Python program that determines the symmetric difference of two lists L1 and L2, i.e. the list made up of the elements of L1 which are not in L2 and the elements of L2 which are not in L1 Example if: L1 = [11, 3, 22, 7, 13, 23, 9] L2 = [5, 9, 19, 23, 22, 23, 13] The program returns the list [11, 3, 7, 5, 19]

#### Exercise 19 || Solution

Write an algorithm in python as a function which takes as parameter a list of numbers L and which returns the minimum of the list without using any predefined function in Python.

#### Exercise 20 || Solution

Write a Python algorithm which returns the maximum of the list without using any predefined function in Python.

#### Exercise 21 || Solution

Write a Python algorithm that returns the list of duplicate elements of a given list. Example if L = [7, 23, 5, 12, 7, 19, 23, 12, 29], the algorithm returns the list: [7, 23, 12]

#### Exercise 22 || Solution

Write an algorithm in python that returns the list of duplicate elements of a given list without using any predefined functions in Python. Example if L = [7, 23, 5, 12, 7, 19, 23, 12, 29], the algorithm returns the list [7, 23, 12].

#### Exercise 23 || Solution

Write an algorithm in python that returns the number of occurrences of an 'a' element in a given list L without using any predefined functions in Python.

#### Exercise 24 || Solution

Write a Python algorithm as a function which takes as parameter a couple (L, a) formed by a list L and an element 'a' and which returns the position of the element 'a' in the list L without using the index() method or any other built-in Python method. The function should return -1 if element 'a' is not present in the list L.

#### Exercise 25 || Solution

Write a Python program that determines the list of words not containing the letter 'a' in a given text.

#### Exercise 26 || Solution

For a given list L of integers, write an algorithm in Python that return the tuple of lists (l_even, l_odd) where l_even designates the list of even integers of L and l_odd designates the list of odd integers of L. Example for L = [11, 3, 22, 14, 31, 18, 12, 7] the program returns the pair of lists: ([22, 14, 18, 12], [11, 3, 31, 7])

#### Exercise 27 || Solution

Write a Python algorithm that determines the list of words starting with a capital letter in a given text T.

#### Exercise 28 || Solution

Write a Python algorithm that determines the list of words containing at least one capital letter in a given T text.

#### Exercise 29 || Solution

Write a Python algorithm that determines the list of words containing at least one digit in a given T text.

#### Exercise 30 || Solution

Write a Python algorithm that determines the list of words containing no digits in a given T text.

#### Exercise 31 || Solution

For given a list of integers L, write a Python algorithm that returns the list of tuples (n, m) verifying n + m <10. Example: if L = [11, 3, 2, 22, 4, 31, 18, 6, 12, 1, 7], the algorithm returns: the list: [(3, 3), (2, 3), (4, 3), (6, 3), (1, 3), (3, 2), (2, 2), (4, 2), (6, 2), (1, 2), (7, 2), (3, 4), (2, 4), (4, 4), (1, 4), (3, 6), (2, 6), (1, 6), (3, 1), (2, 1), (4, 1), (6, 1), (1, 1), (7, 1), (2, 7), (1, 7)]

#### Exercise 32 || Solution

Write a Python program that move the first 3 elements of a given list and place them at the end of the list. Example if L = [25, 13, 11, 1, 4, 31, 18, 6, 12, 1, 7], the program returns: the list: [1, 4, 31, 18, 6, 12, 1, 7, 25, 13, 11]

#### Exercise 33 || Solution

Write a Python program as a function which takes a list L as a parameter and returns the list obtained by performing a circular permutation on the list L. Example if L = [41, 11, 34, 20, 18, 6], the program returns: the list: [6, 41, 11, 34, 20, 18]

#### Exercise 34 || Solution

Write a Python algorithm as a function which takes a string text T as a parameter and which returns the list of words containing at least two digits. Example: if T = 'Python2.7 is replaced by Python3.X since 2018', the function returns the list ['Python2.7', '2018']

#### Exercise 35 || Solution

Write a python algorithm as a function which takes as parameter a string text T and which returns the list of words containing at least one number and one capital letter. Example: if T = 'Python2.7 has been replaced by Python3.X since 2018', the algorithm return: ['Python2.7', 'Python3.X']

#### Exercise 36 || Solution

Write a Python algorithm as a function which takes as parameter a couple (listScores, listCoefficients) and which returns the average obtained, where listScores designates the list of scores obtained by a students and listCoefficients designates the list of associated coefficients

#### Exercise 37 || Solution

Write a python algorithm that extracts from a given list of numbers the sublist made up of numbers that contain the digit 3.

#### Exercise 40 || Solution

Write a Python algorithm that extracts from a list of numbers the sublist of numbers that end with an even number. Example: if L = [21, 14, 346, 728, 13, 19], the algorithm returns the list: [14, 346, 728]

#### Exercise 41 || Solution

Write an algorithm in python that extracts from a list of numbers the sublist of numbers whose last digit is even and the second is odd. Example if: L = [21, 14, 346, 728, 136, 19], the algorithm returns the list: [14, 136]

#### Exercise 42 || Solution

Write a Python algorithm as a function which takes as a parameter a list of integers L and which returns the list obtained from L by inserting just after each number the string 'even' or 'odd' depending on the parity of number. Example if L = [2, 11, 25, 6, 14], the algorithm returns the list: [2, 'even', 11, 'odd', 25, 'odd', 6, 'even', 14, 'even']

#### Exercise 43 || Solution

Write a Python algorithm as a function which takes as parameter a list of integers L and which returns the list obtained from L by removing all negative numbers. Example if L = [7, -2, 11, -25, 16, -3, 14], the algorithm returns the list: [7, 11, 16, 14]

#### Exercise 44 || Solution

Write a Python algorithm as a function which takes as a parameter a list of integers L and which returns the list obtained from L by moving all zeros to the beginning of the list. Example if L = [7, 0, 11, 0, 25, 16, 0, 14], the algorithm returns the list: [0, 0, 0, 7, 11, 25, 16, 14]

#### Exercise 45 || Solution

Write a Python algorithm as a function which takes as parameter a list of real numbers L and which returns the list obtained from L by removing all integers.

#### Exercise 46 || Solution

Write a Python algorithm as a function which takes as parameter a string variable s and which returns the list of all uppercase characters found in s.

#### Exercise 47 || Solution

Write a Python algorithm as a function that takes a list L as a parameter and returns the sum of elements of the list L with odd index. Example if L = [3, 2, 5, 11, 21, 4, 7], the algorithm returns the number L[1] + L[3] + L[5] = 2 + 11 + 4 = 17.

#### Exercise 48 || Solution

Write a Python algorithm as a function that takes a list L of numbers as a parameter and returns the sum of elements of L with odd index. Example if L = [3, 2, 5, 11, 21, 4, 7], the algorithm returns the number 17.

#### Exercise 49 || Solution

Write a Python algorithm as a function that takes a list L as a parameter and returns the maximum number of elements with even index without using any predefined functions in Python. Example if L = [13, 2, 31, 120, 4, 97, 15], the algorithm returns the number 31.

#### Exercise 50 || Solution

Write a Python algorithm which returns the list of elements duplicated at least 3 times within a given list L .

#### Exercise 51 || Solution

Write a Python algorithm as a function which takes a list L as a parameter and which returns the list of elements duplicated at least 3 times without using neither the count() method nor any predefined method in Python. (you can use exercise 23)

#### Exercise 52 || Solution

Write a Python program as a function which takes as parameters a tuple (L, a) formed by a list L and an element a of L and which returns the list of indexes of a in the list L. Example if L = [2, 7, 11, 7, 21, 39, 7] and a = 7 the function returns [1, 3, 6]

#### Exercise 53 || Solution

Write an algorithm in Python allowing to determine the penultimate index of an element in a list without using any predefined function in Python. Example if L = [2, 7, 11, 7, 21, 39, 7] and a = 7 the algorithm returns 3.

#### Exercise 54 || Solution

Write a Python algorithm as a function which takes as parameter a list of integers L = [n1, n2, n3, ...., np] and which returns the list: [n1, n1 + n2, n1 + n2 + n3, ...., n1 + n2 + ... + np]

#### Exercise 55 || Solution

Write an algorithm in Python to sort a list according to the insertion sort algorithm.

#### Exercise 60 || Solution

Write a Python algorithm that reverse the order of the elements of a list using the reverse() method. Example if L = ['Java', 'Python', 'PHP', 'C ++'], the algorithm returns the list: ['C ++', 'PHP', 'Python', 'Java']

#### Exercise 61 || Solution

Resume the previous exercise (Exercise60) without using the reverse() method in python.

#### Exercise 62 || Solution

Given a list of integers L, write an algorithm in Python which multiplies the elements of L of even index by 2 and those of odd index by 3. Example if L = [3, 2, 7, 11, 5, 3], the algorithm returns the list [6, 6, 14, 33, 10, 9]

#### Exercise 63 || Solution

Write a program in python allowing to extract from a list of strings, the list of strings containing at least two vowels. Example if
``L = ["Java", "Python", "Dart", "MySql"]``
, the program returns the list
``["Java", "Python"]``
.

#### Exercise 64

Write a Python algorithm that transforms the list
`` L = [1, 2, 3, 4, 5] ``
into the list:
``[1, [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5]]``

#### Exercise 65 || Solution

Given a list of strings, write a Python algorithm allowing which replace the elements of the list with their lengths. Example: if
`` L = ["Python", "Django", "Numpy", "Sympy"]``
, the algorithm returns the list
``[6, 6, 5, 5]``

#### Exercise 66 || Solution

Write a Python algorithm to extract the list of integers from a list of numbers. Example: if
``L = [2.5 , 11.54 , 3 , 7.35 , 5 , 6.5 , 9]``
, the algorithm returns the list
``[3, 5, 9]``

#### Exercise 67 || Solution

Write without using any predefined function a Python algorithm as a function which takes as parameter a list of integers L and which returns True if the list is arranged in ascending order and False if not.

#### Exercise 68 || Solution

Write a Python algorithm which determines for a given integer, the list of perfect squares between 1 and n. Example if n = 100 the algorithm returns the list
``[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]``

#### Exercise 69 || Solution

Write a Python algorithm which determines for a given integer n the list of tuples of integers (p, q) satisfying:
``p2 + q2 = n``

#### Exercise 70 || Solution

Write a Python algorithm to test if a given list is symmetric using the reverse() method. Example: for L1 = [2, 5, 11, 5, 2] the algorithm returns True and for L2 = [2, 23, 11, 51, 7] the algorithm returns False.

#### Exercise 72 || Solution

Write a function which takes an element x and a list L as a parameter, and returns the list of positions of x in L. The function must return the empty list if the element x does not appear in the list L.

#### Exercise 73 || Solution

Write a function which takes as parameter a variable of type string s and another string T, and returns the list of positions of a pattern s in the string T. The function must return the empty list if the pattern s does not appear in the text T.

#### Exercise 74 || Solution

Write a Python algorithm that asks the user to enter successively 7 integers and display the list of numbers entered by ignoring repeated numbers. Example if the user enters the numbers: 2, 11, 3, 2, 3, 5, 2. the algorithm returns the list
``[2, 11, 3, 5]``

#### Exercise 75 || Solution

Write a Python algorithm to reverse a list without using the reverse() method.

#### Exercise 76 || Solution

Write a Python algorithm that determines the first index of the maximum in a given list using the max () method. Example if
``L = [22, 7, 88, 41, 14, 88, 9]``
, the algorithm returns the first index of the maximum which is 2.

#### Exercise 77 || Solution

Repeat the previous exercise (Exercise 76) without using the max() method.

#### Exercise 78 || Solution

Given a list made up of the students' averages avg_list, write a python algorithm that determines the first index of the average which is less than 10. Example if
``avg_list = [12, 17, 10, 7, 11, 14, 15, 9]``
, the algorithm returns index 3.

#### Exercise 79 || Solution

Write using the count() method, a python algorithm as a function which takes as parameter a list of integers L and which returns without repetitions the list of tuples (n, occ_n) formed by the elements n of L and their occurrence occ_n. Example: if
``L = [22, 7, 14, 22, 7, 14, 7, 14, 11, 7],``
the algorithm returns the list
``[(22, 2), (7, 4), (14, 3 ), (22, 1), (11, 1)]``

#### Exercise 80

Resume the previous exercise (Exercise 79)  without using the count() method or any other predefined method in Python.

#### Exercise 81

Write a Python algorithm that transforms a string into a list without using the split () method or any other predefined method in Python.

#### Exercise 82 || Solution

Write a program in Python which determines the list of words containing two successive identical characters in a character string s. Example if s = "Python is the most recommended programming language", the algorithm returns the list
`` ["recommended", "programming"].``

#### Exercise 83 || Solution

Write a python algorithm which replaces the elements of even index by 2 and the elements of odd index by 1.

#### Exercise 84 || Solution

96 is an integer whose tens digit is divisible by 3 (property (*)) Given an integer n, write an algorithm in python which returns the list of integers less than or equal to n composed of two digits and checking the property (*)

#### Exercise 85 || Solution

Write a python program which that move the null values of a list to the end of the list while keeping the order of the other non-null elements. Example if the list is: L = [7, 0, 11, 5, 0, 21, 0, 2, 0, 0, 9] the output is: [7, 11, 5, 21, 2, 9, 0, 0, 0, 0, 0]

#### Exercise 86 || Solution

Given a list L, write a python algorithm which returns the list of integer elements of L. Example: if
`` L = ["Python3", 91, "Java2", 95]``
, the algorithm returns the list
``[91, 95]``

#### Exercise 87 || Solution

Given a list L, write a Python algorithm to convert a list into a string without using any predefined method other than the str() method. Example if L = ["Python", "created on", 91, "by Guido Van Rosam"], the algorithm returns the string s = "Python created on 91 by Guido Van Rosam".

#### Exercise 88 * || Solution

Given a list L, write a python algorithm which returns the list of digits contained within the elements of the list L. Example if L = ["Python3", 91, "Java2", 95], the algorithm returns the list [3, 9, 1, 2, 9, 5]

#### Exercise 89 * || Solution

Given a list L, write a python algorithm which returns the list of digits contained within the elements of list L without repetition. Example if L = ["Python3", 91, "Java2", 95], the algorithm returns the list [3, 9, 1, 2, 5]

#### Exercise 90 * || Solution

Given a list of integers
``L = [n1, n2, n3, ..., np]``
, write an algorithm in python which returns the list:
``L_sum = [n1, n1 + n2, n1 + n2 + n3, .. ., n1 + n2 + n3 + ... + np]``

#### Exercise 91 ** || Solution

Write an algorithm in python as a function that transforms a string text T into a list where words containing numbers are placed at the end of the list. Example if T = "Python_1 created in 1991. Currently it is in version Python_3.9" the algorithm returns the list: ['created', 'in', 'Currently', 'it', 'is', 'in', 'version', 'Python_1', '1991.', 'Python_3.9' ]

#### Exercise 92 **|| Solution

Write a python algorithm which transforms a list of integers
``L = [n1, n2, n3, ...., np]``
into a list whose elements are the averages of the partial sums:
``[n1, average(n1, n2) , average(n1, n2, n3), ...., average(n1, n2, ..., np)]``

#### Exercise 93 * || Solution

Write an algorithm in python which transforms a list of integers L = [n1, n2, n3, ...., np] into the list of factorials: [n1! , n2! , n3!, ...., np!]

#### Exercise 94* || Solution

Write a Python algorithm which extract from a string text the list of words whose first character is identical to the last. Example if s = "radar number 212", the algorithm returns the list ['radar', '212']

Younes Derfoufi
my-courses.net