 ## Lesson 31. Practice Writing Simple Programs Pages 63, 64 SBT Informatics 10 Connecting Knowledge to Life>

Select a sentence to see the solution faster

31.1

Write a program that requires the input of a positive real number a. The program needs to control data entry errors as follows:

If the entered number is less than or equal to 0, the message: Entered incorrectly, the number a must be greater than 0. Please enter again.

The program needs to control input errors until the correct input is complete.

Solution method:

Use what you have learned to write a program

Detailed explanation:

The program can be written as follows: 31.2

Sorting Problem: Given a sequence of numbers A, write a program to sort this sequence of numbers in ascending order.

Suppose we will apply insertion sort algorithm to install this program. The idea of ​​this algorithm is as follows: It will traverse each element of the sequence A, from the 2nd position onwards. For each element (assuming A[k]), will try to insert this element at the correct position of the preceding subsequence AA…, A[k-1 ] such that the subsequence is properly sorted. After traversing the elements of the sequence, the original sequence has been sorted.

For each element A[k]insertion at the correct position in the preceding subsequence is done by a loop as follows:

j = k – 1

while j > 0 and A[j] > A[j+1]:

A[j]A[j + 1] = A[j+l],A[j]

j = j – 1

To find the above program error, we apply the printline tool, print the intermediate values. For each main loop (the for k in range(1,n) loop), we will print the following values: k, A[k] and the resulting series A.

The program is written as follows, after adding intermediate element print commands. Please rewrite the above program and test to see if the program has errors? If so, find the error and fix it to make the program run properly.

Solution method:

Use what you have learned to write a program

Detailed explanation:

The result of running the program may look like this:

Original series: [5, 1, 8, 4, 2]

Round 1 number in consideration: 1 [5, 1, 8, 4, 2]

Round 2 numbers under consideration: 8 [5, 1, 8, 4, 2]

Round 3 numbers under consideration: 4 [5, 1, 4, 8, 2]

round 4 numbers are considered: 2 [5, 1, 2, 4, 8]

Destination range: [5, 1, 2, 4, 8]

Comment: at each loop, the A . values[k] has also been moved to be inserted a position in the subsequence AA…, A[k-1 ]however position A not changed. From there we find the error of the program at the position of the while statement:

while j > 0 and A[j] > A[j +1] :

The following should be corrected:

while j >= 0 and A[j] > A[j + 1] :

The program found the error using the printline testing tool. The official program after editing is as follows. 31.3

Write a program to print the multiplication table to the screen. Ask the multiplication table to print out into 10 blocks consisting of 2 rows and 5 columns as follows: Solution method:

From the requirements of the problem, we will set up the main printBCC() procedure that prints the multiplication table. This procedure will have two independent parts, the first part prints 5 blocks in the first row which is the multiplication table of the numbers 1, 2, 3, 4, 5. The second part of the procedure will print 5 blocks in the second row. is the multiplication table of the numbers 6, 7, 8, 9, 10.

Detailed explanation:

In order to show it accurately and evenly on the screen, we set up two more functions:

– The st(num) function to create a character string representing the number num. If num is a 1-digit number, then st(num) will insert a space in front of num.

– The space(k) function represents k spaces on the screen

Enter, run, and check the following program output: 31.4

Given any sequence of numbers A. Write a program to find and show the first position of sequence A where 3 consecutive terms have the values ​​1, 2, 3 (can be generalized to any 3 numbers p, q, s). If found, report the location found, otherwise it says “pattern not found”.

Solution method:

Use what you have learned to write a program

Detailed explanation:

To check at an index i, 3 ​​consecutive elements equal to 1, 2, 3 have different ways of checking, for example:

Method 1. Use logical expressions.

A[i] == 1 and A[i] == 2 and A[i] == 3

Method 2. Use a comparison that contains the index range.

A[i:i+3] == [1,2,3]

The program can be written as follows: 31.5

Write a program to input any string of characters from the keyboard, if you need to rearrange the digits, they will be put to the left, the English letters are to the right of the string, the other characters will stay in place.

Solution method:

We already know between characters can also be compared. The two characters ch1 and ch2 are compared by their position in the ASCII or Unicode encoding. The natural comparison relationship between alphanumeric characters is as follows: “0” < “1” < … < “9”.

“A” < “B” < … < “Z” < “a” < “b” < … < “z”.

Detailed explanation:

To solve this exercise we need to create memory variables used to store the intermediate character strings s_tr (string used to temporarily store numeric characters), s_ph (string used to temporarily store alphanumeric characters) and s_gi (string used to store characters that are neither alphanumeric nor alphabetic).

The algorithm of the program is as follows: iterates once all the characters of the original string s, then inserts these characters into the intermediate strings s_tr, s_ph, s_gi. The resulting string will be s_tr + s_gi + s_ph.

Open the editor and enter the following program. 31.6

Write a program to input natural numbers n, then enter n full names of students in the class. When finished, it will print a list of students with 2 columns, column 1 is the name, column 2 is the middle name of this student.

Solution method:

Each entered last name will be split into a first and middle name using the split() function. The student names will be put into the ten sequence, the middle names will be put into the hostem sequence. After entering, print out this list of first and last names.

Detailed explanation:

The program can be written as follows: 31.7

Write a program to input two natural numbers Y1, Y2 is the number of years, Y2 > Y1. Calculate how many leap years there are in the period from year Y1 to year Y2. Apply the calculation to see how many leap years there are in the 21st century.

Solution method:

Apply knowledge learned to write programs

Detailed explanation:

The program can be written as follows: 31.8

Let UCLN(a, b) be the GCC function of two natural numbers a, b. It is easy to see that we have GCLN(a, b) = GCLN(b, a%b) if b > 0 and GCLN(a, 0) = a. From there, write a program to input two numbers a and b and calculate the GCC of a and b.

Solution method:

Apply knowledge learned to write programs

Detailed explanation:

The program can be written as follows: 31.9

Write a program to input the natural number n > 1, then print out all the different prime divisors of n horizontally on the screen. Eg:

– If n = 10, then print 2 5.

– If n = 12, then print 2 3.

Solution method:

Note the requirement of the problem that not to print all prime divisors in the expansion of n as a product of prime factors, but only print each prime divisor once. So, if you have found a prime divisor k of n, you need to reduce n by dividing n by k until it is no longer divisible, then continue to find the next number.

Detailed explanation:

The program can be written as follows: 31.10

The problem of finding the greatest subtotal.

Suppose a travel agency has designed a fixed tour program that passes through n places in turn. Each customer has different reviews for each of these places. Suppose a customer named An has rated the places in the travel program according to the range of values:

A,AA[n – 1]

The company wants to arrange for customer An to go a part of the travel program by following a continuous sub-sequence of locations, for example:

i, i + 1, i + 2, …, j

The purpose of choosing a program for An customer is to make the total value

A[i]+A[i +1] + …+A[j] (first)

is the largest possible.

Given a sequence of tour program ratings of n locations, design a customer tour subroutine such that sum (1) is the maximum. For example if the sequence of evaluations is:

-5, -9, 3, -1, 10, -6, 5

then, the sub-tour program that goes through the places with the rating of 3, -1, 10 has the largest total, ie the most satisfied customer.

Solution method:

Apply knowledge learned to write programs

Detailed explanation:

Let S(i,j) = A[i]+A[i + 1] + … +A[j]

Then the problem is to find i, j such that the maximum value S(i, j). From this, deduce the following simple solution 