Download Solution - ENSTA ParisTech

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
IN101 - TD01
Solution
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”
2
Some loops
Aims: write for-loops, learn to use range, write while-loops
Q1 In a script called “loops.py”, write 4 for loops1 that
• 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.
Solution
loops.py
print ( ’ F i r s t f o r loop ’ )
f o r number i n [ 0 , 1 , 2 , 3 , 4 ] :
p r i n t ( number )
p r i n t ( ’ Second f o r l o o p ’ )
f o r number i n range ( 5 ) :
p r i n t ( number )
print ( ’ Third f o r loop ’ )
f o r number i n range ( 4 , 1 1 ) :
1
Hint: 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’.
1
p r i n t ( number )
print ( ’ Fourth f o r loop ’ )
f o r number i n range ( 4 , 1 7 , 3 ) :
p r i n t ( number )
print ( ’ F i r s t while loop ’ )
number=0
while ( number <5):
p r i n t ( number )
number = number+1
p r i n t ( ’ Second w h i l e l o o p ’ )
number=4
while ( number <11):
p r i n t ( number )
number = number+1
print ( ’ Fourth while loop ’ )
number=4
while ( number <17):
p r i n t ( number )
number = number+3
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]
for item in l :
# Do something w i t h ” i t e m ” here .
Solution
occurencecount.py
# Input
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
# Algorithm
occurence count = 0
for item in l :
i f ( a== i t e m ) :
# I f t h i s i t e m i n t h e l i s t i s equal t o ’ a ’ , then
# t h e occurence c o u n t e r i s incremented by 1
occurence count = occurence count +1
2
# Output : number o f t i m e s ’ a ’ occurs i n ’ l ’
# For i n s t a n c e , i n t h e l i s t above , ’ 2 ’ occurs 3 t i m e s
p r i n t ( occurence count )
3
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 4 4 4 4
4
− + − + −
+ ···
1 3 5 7 9 11
(1)
Q5 Design an algorithm that approximates π using the first n terms 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)
Solution
There are many solutions to this problem. I’ve added three in the archive, and only the most
elegant one below.
approximatepi version0.py
# Make sure t o use python3 ; w i l l n o t work w i t h python2
# I n p u t : number o f terms t o use i n a p p r o x i m a t i o n
n = 11
# Algorithm
# Compute p i i n c r e m e n t a l l y , s t a r t i n g w i t h 0 . 0
pi approx = 0.0
f o r i i i n range ( n ) : # Generates 0 , 1 , 2 , 3 , 4 e t c .
numerator
= 4 ∗ ( −1)∗∗ i i # −1ˆ i i generates 1 , −1 ,1 , −1 ,1 e t c .
denominator = 2∗ i i + 1
# generates 1 , 3 , 5 , 7 , e t c
pi approx
= p i a p p r o x + numerator / denominator
# P r i n t a l l t h e terms , one by one ( n o t necessary , b u t h e l p s t o debug )
p r i n t ( ’ term ’ , ( i i + 1 ) , ’ = ’ , numerator , ’ / ’ , denominator )
# Output
print ( ’ pi i s approximately ’ , pi approx )
Advanced
4
5
The modulo operator
Aims: Write your first algorithm (with a while loop)
Given the dividend a and divisor n (both positive whole numbers ) the modulo operator a
mod n is defined as the remainder of the Euclidean division of a by n. For instance, 26 mod 7
is 5, because you can divide 26 by 7 exactly 3 times, 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 %.
Solution
modulo.py
# Input
d i v i d e n d = 26
divisor = 7
#
#
#
#
#
#
#
#
#
#
#
#
A l g o r i t h m t o compute ’ d i v i d e n d mod d i v i s o r ’
Main i d e a : s u b t r a c t t h e d i v i s o r from t h e d i v i d e n d
u n t i l t h e d i v i d e n d becomes s m a l l e r than t h e d i v i s o r .
The number t h a t i s l e f t i s t h e remainder .
Example :
26 − 7 −> 19
19 − 7 −> 12
12 − 7 −> 5
Can ’ t s u b t r a c t anymore , because 5 < 7
Therefore , 5 i s t h e remainder
remainder = d i v i d e n d
while ( d i v i s o r < remainder ) :
remainder = remainder − d i v i s o r
# Output
p r i n t ( remainder )
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 n is 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
5
for the modulo operator you wrote may be useful here. Alternatively, you may also use the builtin Python modulo operator %.
Solution
isprime.py
# Input
number = 21
# Check i f t h e number i s v a l i d
i f ( number <2):
p r i n t ( ’ Please use a number l a r g e r than 1 ! R e s u l t s may be unexpected . . . ’ )
# Algorithm
# Assume t h e number i s prime u n t i l proven o t h e r w i s e
i s p r i m e = True
# Rest o f t h e a l g o r i t h m f o l l o w s . . .
# Try t o d i v i d e t h e number by a l l d i v i s o r 2 . . ( number−1)
# I f we can d i v i d e i t w i t h one o f t h e d i v i s o r s ( which
# means t h e remainder o f t h e d i v i s i o n i s 0 ) , i t i s n o t a prime
# Once we know i t i s n o t a prime , we can s t o p t e s t i n g w i t h t h e o t h e r d i v i s o r s
divisor = 2
while d i v i s o r <number and i s p r i m e :
# Compute : remainder = number mod d i v i s o r
remainder = number
while ( remainder >= d i v i s o r ) :
remainder = remainder − d i v i s o r
# I f d i v i s o r d i v i d e s number w i t h o u t a
# remainder , then number i s n o t a prime
i f ( remainder == 0 ) :
i s p r i m e = False
#
#
#
#
#
The above can be done much s h o r t e r when u s i n g t h e modulo o p e r a t o r %
The code b l o c k would then s i m p l y be :
i f ( number%d i v i s o r = = 0 ) :
i s p r i m e = False
T h i s would be t h e p r e f e r r e d v e r s i o n , b u t % wasn ’ t presented y e t . . .
d i v i s o r += 1
# s h o r t way o f s a y i n g d i v i s o r = d i v i s o r + 1
# Output
i f isprime :
p r i n t ( number , ’ i s a prime number ! ’ )
else :
p r i n t ( number , ’ i s n o t 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.
6
Solution
allprimes.py
# Input
up to number = 21
p r i n t ( ’ Here comes a l i s t o f prime numbers up t o ’ , up to number , ’ : ’ )
# Algorithm
f o r number i n range ( 2 , up to number + 1 ) :
# Assume t h e number i s prime u n t i l proven o t h e r w i s e
i s p r i m e = True
# Try t o d i v i d e t h e number by a l l d i v i s o r 2 . . ( number−1)
# I f we can d i v i d e i t w i t h one o f t h e d i v i s o r s ( which
# means t h e remainder o f t h e d i v i s i o n i s 0 ) , i t i s n o t a prime
# Once we know i t i s n o t a prime , we can s t o p t e s t i n g w i t h
# the other d i v i s o r s
divisor = 2
while d i v i s o r <number and i s p r i m e :
# S h o r t v e r s i o n w i t h t h e modulo o p e r a t o r %
i f ( number%d i v i s o r = = 0 ) : # does d i v i s o r d i v i d e number w i t h o u t a remainder ?
i s p r i m e = False
# i f so , i t cannot be a prime
d i v i s o r += 1 # s h o r t way o f s a y i n g d i v i s o r = d i v i s o r + 1
# Output
i f isprime :
p r i n t ( number )
7
Related documents