Write a function that split a list of length n, with n odd, into 3 parts:
def splitlist(L):
#FIXME
This can be simplified with Python list slices:
def splitlist(L):
#FIXME
L = [1, 2, 3, 4, 5, 6, 7, 8, 9]
splitlist(L)
Write two functions to search an element in a sorted (in increasing order) list:
def binarysearch(L, x, left, right):
"""Binary Search
Args:
L: List to search in
x: Value to search
left, right: Search intervalle in L [left, right[
Returns:
The position where x is or might be
"""
#FIXME
L = [-3, 0, 5, 8, 13, 24, 32, 37, 42]
binarysearch(L, 0, 0, len(L))
def listsearch(L, x):
#FIXME
Use Python "ternary" operator:
[on_true] if [expression] else [on_false]def listsearch2(x, L):
#FIXME
Write the function buildlist(nb, val = None, alea = None) that builds a new list of length nb:
Note: if a: is False when a is 0, None, [], "" ...
# Reminder on imports, random and seed
import random
help(random.randint)
help(random.seed)
random.seed() # do it once only!
def buildlist(nb, val = None, alea = None):
#FIXME
Python gives a short way to build list: [val] * nb
def buildlist(nb, val = None, alea = None):
#FIXME
Test the "short" version ([val] * n) with random numbers...
[random.randint(0, 10)] * 5
Test the following, then use it to write again buildlist
[i for i in range(10)]
def buildList(nb, val = None, alea = None):
#FIXME
buildlist(5), buildList(5, 0), buildList(5, alea = (0,10))
when you want to build a list of lists
L = buildList(9, [])
L
L[0].append(1)
L
Write again buildlist to avoid the problem
def buildlist(nb, val = None, alea = None):
#FIXME
Use buildlist to build a (5*5) matrix filled with None, then change a value
M = buildlist(4, buildlist(5, None))
M
M[0][0] = 5
M
Write a function buildmatrix(line, col, val = None) that builds a (line*col) matrix filled with val.
def buildmatrix(line, col, val = None):
#FIXME
M = buildmatrix(4, 5)
M[0][0] = 3
M