IN101 - TD01 ´
Enonc´
e
Instructions g ´
en´
erales
En cas de probl`
emes, demandez de l’aide! Mais n’oubliez pas de vous r´
ef´
erer, en premier lieu, au diapos-
itives du cours magistral et aux r ´
ef´
erences bibliographiques. L’index disponible en ligne vous aidera `
a lier
concepts abord´
es et suports de cours.
Utilisez l’´
editeur de texte de votre choix pour les exercices. En cas d’h´
esitation, privil ´
egiez gedit.
Si ce n’est pas encore le cas, cr´
eez un dossier IN101/.
Dans ce dossier IN101/, cr´
eez un nouveau sous-dossier pour la s´
eance de TD, que vous nommerez TD01/.
Pour les questions du type : ”Write, in natural language, an algorithm that. . . ”, ouvrez un nouveau fichier
texte (d’extension ”.txt”) plutˆ
ot qu’un fichier python (d’extension ”.py”).
Exemple : helloworld.txt
Documentez et commentez pr´
ecis´
ement votre code : d ´
ecrivez l’objectif g´
en´
eral de l’algorithme, expliquez
les points-clefs de son impl´
ementation, d´
etaillez les conditions normales d’ ´
ex´
ecution du script (c.- `
a-d. le
fichier ”.py”) et exposez les cas extrˆ
emes et les erreurs pr´
evus.
Basic
1 Hello world
Aims: write a first Python script, call the interpreter.
An old programming tradition is that the first program you write prints “Hello World”. Here’s
how to do so in Python:
1. Make a new file called “hello world . py
2. In the file, write “print( ’Hello World!)
3. Open a terminal, and go to the directory where your file is
4. Call “python3 hello world.py
1
2 Some loops
Aims: write for-loops, learn to use range, write while-loops
Q1 In a script called “loops.py”, write 4 for loops1that
print the numbers 0. . . 4 (without the range function)
print the numbers 0. . . 4 (use the range function; see the CM for an example)
print the numbers 4. . . 10 (again with range)
print the numbers 4, 7, 10 ,13 ,16 (again with range)
Q2 Print the same sequence of numbers above, but now with while loops.
3 Count numbers in a list
Aims: use a for-loop in combination with an if statement (your first algorithm!)
Q3 Write, in natural language, an algorithm that counts the number of occurences of a number
in a list.
Q4 Program this algorithm in a Python script called occurencecount.py You can loop over the
items in a list as follows:
l = [ 1 , 8 , 3 , 2 , 7 , 2 , 7 , 2]
fo r i tem i n l :
# Do som ething w i t h i tem here .
occurencecount.py
# I n p u t
a=2 # Number t o search f o r
l = [ 1 , 8 , 3 , 2 , 7 , 2 , 7 , 2] # A l i s t o f numbers
# Al g o r i t hm
# Outpu t : number o f t im es ’ a o ccurs i n l
# For in st an ce , i n the l i s t above , 2 occurs 3 times
print ( occurence count )
1Hint: a typical error that people make is to forget the ‘:’ after the for loop definition, i.e. it is ‘for i in [0,1,2]:
and not for i in [0,1,2]’ The latter will make the Python interpreter raise an error ‘SyntaxError: invalid
syntax’.
2
Everyone
4 Approximating π
Aims: Learn to use for loops and range in an algorithm
The constant πcan be computed by the following infinite series. The more terms that are
included, the closer the series will approximate π.
π=4
14
3+4
54
7+4
94
11 +· · · (1)
Q5 Design an algorithm that approximates πusing the first nterms in the formula above.
Q6 Implement an algorithm that prints the approximation of π. It may be useful to use the
range function, where
range(1,13,2) yields the list [1,3,5,7,9,11] (i.e. increments of 2)
range(3,13,4) yields the list [3,7,11] (i.e. increments of 4)
# I n p u t
n = 12
# W r i te your a l g o r i t h m here
# Outpu t : a p p ro x im at io n o f p i
print ( p i a p p r o x )
Advanced
5 The modulo operator
Aims: Write your first algorithm (with a while loop)
Given the dividend aand divisor n(both positive whole numbers ) the modulo operator a
mod nis defined as the remainder of the Euclidean division of aby n. For instance, 26 mod 7
is 5, because you can divide 26 by 7exactly 3times, the remainder being 5 = 26 (7 ×3).
Further examples are: 6 mod 4 2,6 mod 3 0, and 5 mod 4 1
Q7 Design an algorithm to implement the modulo operation. Write it in English/French as
Python comments at the top of a Python script modulo.py
Q8 Write a Python program in modulo.py that implements this algorithm (you may assume that
a>n). You may not use the built-in Python modulo operator %.
3
modulo.py
# I n p u t
d i v i d e n d = 26
divisor = 7
# Al g o ri th m t o compute di vi de nd mod d i v i s o r
remainder = remainder divisor
# Output
6 Check if a number is prime
Aims: Write algorithms involving several loops
A prime is defined is a natural number greater than 1 that has no positive divisors other
than 1 and itself.
Q9 Design an algorithm that determines where a number nis a prime or not. Write it in
English/French, not Python.
Q10 Implement an algorithm that prints an output whether a number is a prime. The algorithm
for the modulo operator you wrote may be useful here. Alternatively, you may also use the built-
in Python modulo operator %.
isprime.py
# I n p u t
number = 21
# Check i f th e number i s v a l i d
i f ( number <2):
print ( Please use a number l a r g e r than 1 ! Re sult s may be unexpected . . . )
# Al g o r i t hm
# Output
i f isprime :
print ( number , ’ i s a prime number ! ’ )
else :
print ( number , ’ i s not a prime number . ’ )
Q11 Modify your algorithm (but put it in a new Python script first) so that it prints all the prime
numbers up to n. That is, if n= 20, it should print 3,5,7,11,13,17,19.
4
1 / 4 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans linterface ou les textes ? Ou savez-vous comment améliorer linterface utilisateur de StudyLib ? Nhésitez pas à envoyer vos suggestions. Cest très important pour nous !