Chapitre 9 : Pseudo-code exercises in Python


 Project implementation 

At the end of this chapter, the learner will be able to master an important part of the Python language by implementing in this language the 20 statements encountered in the chapter on pseudo-code. The learner should be able to develop strategies for finding information on the Internet or in books to be able to solve all these exercises. Some of the information on lists in the next chapter may also be useful for certain exercises.

 Objective 

The aim of this chapter is to find the resources on the Internet or in books to solve correctly and completely the 20 exercises encountered in the chapter on pseudo-code. These are listed below.

 Pseudo-code statements 

Statement 1: The program must read numbers from the keyboard until it finds the value 0 (zero). The program must then indicate in which position this 0 appeared in the list of numbers read. A preliminary analysis is essential (in particular to identify the "trick(s)" required).

Statement 2: the program must read a maximum of 3 words, stopping reading as soon as it finds the word "stop". The program must then indicate whether the word "stop" was among the words read.

Statement 3: The program must read words until it finds 1 word previously requested by the user. The program must then report whether this word was in the first 2 words read, between the 3rd and 5th or further down the list.

Statement 4: the program must read words until it finds the word "stop", provided it has read the word "attention" at least once before, in any position. Use only one REPEAT.

Statement 5: The program must read numbers, stopping at a number previously requested by the user. The program must then report how many times it has encountered the number 0 (zero) in the list of numbers read.

Statement 6: The program must ask for 1 number between 2 and 10 inclusive (make sure your program is robust!). The program then reads numbers until it finds 3 multiples of the requested number. Use % (short for modulo).

Statement 7: The program must read words until it encounters the word "stop", provided that the word "attention" is just before it in the list. Report how many times the word "attention" has been read.

Statement 8: The program must read numbers until it finds the number 0 (zero). The program then displays the average of all the numbers read, regardless of the 0. Pay attention to the robustness of your program.

Statement 9: The program must read numbers by first asking the user how many numbers are to be read (robustness!). The program then displays the smallest and largest numbers read.

Statement 10: The program must read words until it finds the word "stop", provided that the word "attention" occurs at least 1 time in the list. Report the positions of the first and last "attention" words read in the list.

Statement 11: The program must read numbers until the sum of the numbers read equals a number previously requested from the user. Report how many of these numbers were positive, negative and equal to 0.

Statement 12: The program must read numbers until it finds a number previously requested by the user, provided that this number is equal to the sum of the last 2 numbers read. Example: stop number = 12 and numbers just before = 8 and 4 or 6 and 6 or 7, 0, 12 and 12.

Statement 13: The program must read numbers until it finds 3 multiples of 5. The program must then display these 3 multiples in the order in which they were read.

Statement 14: The program must read numbers until it finds 3 multiples of a number previously requested by the user. Report how many of these multiples were also even.

Statement 15: The program must simulate the throwing of a die and display the value found at each throw. The program must display the number of throws needed to find a 6.

Statement 16: The program simulates the throwing of a die and displays the value found on each throw until the values 4, 2 and 1 are found in order. It reports the positions of the first and last pair found (2 successive identical values).

Statement 17: The program simulates the drawing of 13 spade cards (see image below), with a number of cards previously requested by the user. Each card must be displayed clearly (red display: Queen of Spades, not 12 of Spades).

Statement 18: The program simulates the throwing of a die until it finds, in order or out of order, two numbers previously requested by the user (robustness!). The program reports the number of pairs of 6 encountered.

Statement 19: The program simulates the tossing of a 1 euro coin until the number of "Heads" equals the number of "Tails" + 3. The program displays the result of each throw and the number of throws required.

Statement 20: The program simultaneously rolls 3 dice, displaying their values until 421 is obtained. 421 will be obtained if the sum of the 3 dice = 7 and their product = 8.

Good luck, be brave... :O)

Return