December, 2021 - François HU
Master of Science - EPITA
This lecture is available here: https://curiousml.github.io/
These exercices are considered "easy" exercices.
Create a function add_more
, that produces the following results:
>>> add_more(1, 1)
3
>>> add_more(1, -1)
1
>>> add_more(5, 0)
6
def add_more(a, b):
return(a+b+1)
print(add_more(1, 1))
print(add_more(1, -1))
print(add_more(5, 0))
3 1 6
Crate a function create_list
, that produces the following results:
>>> create_list(5)
[1, 4, 9, 16, 25]
>>> create_list(10)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> create_list(-1)
[]
def create_list(a):
return [i**2 for i in range(1,a+1)]
print(create_list(5))
print(create_list(10))
print(create_list(-1))
[1, 4, 9, 16, 25] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] []
Create a function add_lists
that produces the following results (we assume that the two lists have same length)
>>> add_lists([1, 2, 3], [-1, 2, -2])
[0, 4, 1]
>>> add_lists([1, 0, 0], [-1, 2, -2])
[0, 2, -2]
>>> add_lists([1, 0, 0], [0, 1, 1])
[1, 1, 1]
def add_lists(l1, l2):
l = []
for i in range(len(l1)):
l.append(l1[i]+l2[i])
return l
print(add_lists([1, 2, 3], [-1, 2, -2]))
print(add_lists([1, 0, 0], [-1, 2, -2]))
print(add_lists([1, 0, 0], [0, 1, 1]))
[0, 4, 1] [0, 2, -2] [1, 1, 1]
Create a function add_lists_improved
that produces the following results (we don't assume that the two lists have same length)
>>> add_lists_improved([1, 2, 3], [-1, 2, -2])
[0, 4, 1]
>>> add_lists_improved([1, 2, 3], [-1, 2, -2, 1, 2])
[0, 4, 1, 1, 2]
>>> add_lists_improved([1, 0, 0, -1, 0], [0, 1, 1])
[1, 1, 1, -1, 0]
def add_lists_improved(l1, l2):
l = []
for i in range(max(len(l1), len(l2))):
if i>=len(l1):
l.append(l2[i])
elif i>=len(l2):
l.append(l1[i])
else:
l.append(l1[i]+l2[i])
return l
print(add_lists_improved([1, 2, 3], [-1, 2, -2]))
print(add_lists_improved([1, 2, 3], [-1, 2, -2, 1, 2]))
print(add_lists_improved([1, 0, 0, -1, 0], [0, 1, 1]))
[0, 4, 1] [0, 4, 1, 1, 2] [1, 1, 1, -1, 0]
We have the following dictionary students
(don't forget to execute the cell):
# DON'T MODIFY THIS PROGRAM, JUST EXECUTE IT
students = {'Name': ['Arthur',
'Marcial',
'Margaux',
'François',
'Julie',
'Sophie',
'Alex',
'Christine'
],
'Grade': [8, 2, 15, 3, 7, 10, 11, 16]}
In the field Name
of the dictionary, add (e.g. use append
command) a new name "Sacha"
.
In the field Grade
of the dictionary, add (e.g. use append
command) a new grade 19
.
students["Name"].append("Sacha")
students["Grade"].append(19)
students
{'Name': ['Arthur', 'Marcial', 'Margaux', 'François', 'Julie', 'Sophie', 'Alex', 'Christine', 'Sacha'], 'Grade': [8, 2, 15, 3, 7, 10, 11, 16, 19]}
Let us continue working with the previous dictionary students
.
Upgrade
in the dictionary. The associated value is the lists students["Grade"]
where each value is incremented by 1.students["Upgrade"] = [g+1 for g in students["Grade"]]
students
{'Name': ['Arthur', 'Marcial', 'Margaux', 'François', 'Julie', 'Sophie', 'Alex', 'Christine', 'Sacha'], 'Grade': [8, 2, 15, 3, 7, 10, 11, 16, 19], 'Upgrade': [9, 3, 16, 4, 8, 11, 12, 17, 20]}
Create a function concatenate_str
that produces the following results:
>>> concatenate_str("I", "am")
I am
>>> concatenate_str("a", "student")
a student
>>> concatenate_str("of", "EPITA")
of EPITA
def concatenate_str(s1, s2):
return s1 + " " + s2
print(concatenate_str("I", "am"))
print(concatenate_str("a", "student"))
print(concatenate_str("of", "EPITA"))
I am a student of EPITA