Sei sulla pagina 1di 6

Computer science (code 30424) a.y.

2018-2019

Exercises lesson 13

EXERCISES ON LECTURE TOPICS


All exercises are provided with the solution script, but sometimes the one provided is not the only
way to solve the problem: you may solve it in a slightly different and equally correct way.
To verify that the code is working properly, test the possible flows and data types using different
input values. Use the course textbook1 as a support.

EXERCISE 13.1

Create in Python the program Exercise13.1.py that considers the following input string variable
(written exactly as below):
animals = 'dogcatgiraffejaguarlion'

a. use slicing to save the names of the five animals in five different variables
b. create a list that contains the five animals and rearrange them alphabetically
c. display the list on the screen, making sure that only one animal is shown per line

EXERCISE 13.2

Create in Python the program Exercise13.2.py that considers the following input string variable
(written exactly as below):
animals = 'mouse, cat, monkey, snake, wolf'

a. use a suitable string method to transform it into a list (NB: punctuation marks are not part of
the words!) and show it on the screen
b. reorder it in descending alphabetical order
c. show it on the screen making it appear all on one line as a string, using a dash (-) to separate
the words

EXERCISE 13.3

Create in Python the program Exercise13.3.py that creates a dictionary in which:

a. the keys are the integers between 1 and a number requested from the user
b. the values correspond to the respective key divided by two and elevated to the cube

The program must then display on the screen the keys and the respective values of the dictionary.
Each key-value pair must appear on a different line, in a well-formatted table.

1
Learning Python, A. Clerici, M. De Pra, M.C. Debernardi, D. Tosi, Egea, 2019
EXERCISE 13.4

Create in Python the program Exercise13.4.py that, given a list of integers, asks the user for a
number and returns (displaying it) a list containing only the numbers in the source list that are
smaller than the number entered by the user.

E.g., considering the list a = [125, 89, 2, 6, 58, 65, 137, 11, 64, 98, 17, 35] and the number entered by
the user equal to 70, the program must return the list b = [2, 6, 58, 65, 11, 64, 17, 35].

EXERCISE 13.5

Create in Python the program Exercise13.5.py that asks the user for an integer and returns a list
containing all the divisors of that number (integers for which that number is exactly divisible, without
remainder), excluding 1 and the number itself.

The program must then display on the screen the list of dividers (preceded by a message that
clarifies what you are about to view), otherwise specify that there are no divisors for the number
entered and therefore is a prime number.

EXERCISE 13.6

Create in Python the program Exercise13.6.py that asks the user for an integer and returns a list
containing all its multiples, starting from 1, which are less than 1000.

The program must then display the list of multiples (preceded by a message that clarifies what we
are about to display), otherwise specify that there are no multiples less than 1000.

EXERCISE 13.7

Create in Python the program Exercise13.7.py that asks the user for an integer and returns a
dictionary containing all its multiples, starting from 1, which are less than 1000. Use multipliers as
keys.

The program must then show the table of multiples, otherwise specify that there are no multiples
less than 1000. The multipliers must be aligned to the right in a 3 characters wide field, while the
results must be aligned to the right in a 4 characters wide field, as in the following output example
where the chosen value is 50:
Table of 50
50 x 1 = 50
50 x 2 = 100

50 x 9 = 450
50 x 10 = 500
50 x 11 = 550

50 x 19 = 950
50 x 20 = 1000

2
EXERCISE 13.8

Create in Python the program Exercise13.8.py that progressively adds to the tuple:
tuplex = (4, 9, ['a','b'], 123.45, 0)

the following values, remembering which type of sequences in Python is editable and which not:

a. the value 7 at the end of the sequence


b. the tuple (10, 100, 1000) in the fourth position
c. the string "bob" in index 2
d. element 3.5 in first position
e. False in position -1

It must also delete the value 9 element and the index -4 element.

Display each change on the screen and view the final tuplex content which must again be a tuple type
object.

EXERCISE 13.9

Create in Python the program Exercise13.9.py that adds up all the elements of a list of 5 numbers
provided as input by the user. More specifically, the program must:

a. fill the list using a conditional loop (while) and display it


b. convert the elements of the list into a decimal type
c. if all five values are numbers, it must display the sum, otherwise it must communicate that an
error has been made in entering the data

EXERCISE 13.10

Create in Python the program Exercise13.10.py containing the palindrome function to visualize
whether a word, provided as input by the user, is or is not a palindrome (a word that is read in
reverse to keep the meaning unchanged).

Test the function with the words “kajak” and “canoe”.

EXERCISE 13.11

Create in Python the program Exercise13.11.py that asks the user to enter a string and view its
length, until the user enters the string “end” or “exit”.

EXERCISE 13.12

Create in Python the program Exercise13.12.py to get a string composed of the first 2 and the last
2 characters of a string supplied as input by the user. If the length of the input string is less than 4
characters, it must return an empty string as a result.

3
EXERCISE 13.13

Create in Python the program Exercise13.13.py that:

a. saves the five numbers requested by the user in the start_list list (initially full of 0)
b. creates a second list (square_list) containing the values of the first list squared
c. sorts the values of the second list in ascending order and displays them

EXERCISE 13.14

Create in Python the program Exercise13.14.py to manage the list of party guests. The program
must:

a. consider that two people have already been invited to the party: Jack and Daniel
b. ask the user if he/she wants to invite another person
c. with an affirmative answer (allow different possible answers, for example "Yes", "yes", etc.),
ask the user for the name of that person and add it to the other guests, then ask if they would
like to invite another person (the number of guests can be infinite)
d. with a negative answer, it must show the list of guests on video

EXERCISE 13.15

Create in Python the program Exercise13.15.py containing the total_list function that adds the value
of all the elements of a list passed as a mandatory argument and returns the total. Use a loop so
that the function can be used with lists of different lengths.

Complete the program by calling the newly created function with at least two numeric lists of
different lengths.

EXERCISE 13.16

Create in Python the program Exercise13.16.py that must:

a. ask the user to enter a string


b. define the char_frequency function that returns a dictionary in which the keys are the letters
that make up the string (without repetitions) and the values are the number of times that the
letter appears in the string
c. display the dictionary

EXERCISE 13.17

Create in Python the program Exercise13.17.py to draw horizontal (stylized) histograms in the shell.
Specifically it must:

a. contain the function create_list that asks the user how many values he/she wants to insert and
return the list of integers chosen by the user
b. contain the function draw_hist which must use the symbol "*" (to be inserted as an optional
parameter in the function) to draw the horizontal histograms, using the values of the list
created by calling the function create_list

4
E.g., using the list [2,7,1,4] and the optional ‘#’ argument, it must produce the following output:
##
#######
#
####

EXERCISE 13.18

Create in Python the program Exercise13.18.py containing the weighted_average function, which
calculates the weighted arithmetic mean of a series of values. Specifically, the function must contain
the following mandatory parameters:

• a list with all the values on which to calculate the average


• a second list with the weights to be used (must be positive values between 0 and 1)

In the function it is necessary that the following checks are carried out: the sum of the weights must
be equal to 100%; the list containing the weights must have the same number of items than the
value list. In both cases, if the check fails, a message explaining the error must be shown to the user
and the average (obtained as the sum of each value multiplied by the respective weight) must not
be calculated.

Complete the program by calling the newly created function with at least three data examples to
test all possible alternatives.

EXERCISE 13.19

Create in Python the program Exercise13.19.py containing the digits_sum function that receives a
positive integer as an argument and returns the sum of its digits.

E.g., if the number entered is 17, the function must return 8 (1 + 7); if the number entered is 153, it
must return 9 (1 + 5 + 3).

EXERCISE 13.20

Create in Python the program Exercise13.20.py containing the function digits_sum_bis that asks the
user for a positive integer and returns the sum of the digits that compose it.

Provide specific messages, rather than attempts to calculate the result, for the following cases:

• entering an empty string (press Enter without writing anything)


• entering a decimal number
• entering a negative integer
• entering a sequence composed only of zeros
• entering any string

NB: remember that the conversion of a float number into an integer truncates the decimals, so
int(5.34) gives 5, which is obviously different from 5.34!

5
EXERCISE 13.21

Create in Python the program Exercise13.21.py containing a function to count, given a list or a tuple,
how many elements are numbers and how many strings. The function must both display the results
and make them available saved in a list object.

E.g., using the list [2, 'a', 'b', 7, 1, 5, 4, 'z'] we must obtain the following output:
Of the 8 elements analyzed:
5 are numbers
3 are strings

EXERCISE 13.22

Create in Python the program Exercise13.22.py that creates an Italian-English dictionary


(dict_ita_eng) containing at least 5 terms in Italian (keys) and as many English translations (values).

The program must also contain three functions to interact with the newly created dictionary:

• translate, without arguments, to consult the dictionary


a. when it is called, it must ask the user if he wants to have the translation of an Italian
term
b. with a negative answer it must not ask for anything else and the function ends
c. with affirmative answers it must instead ask the user the Italian term that wants to
translate and show on video the English translation
d. it must then ask the user if he/she wants to translate another term, and continue until
the user decides to stop
e. if the searched word is not present in the dictionary, it must not raise exceptions but
communicate that it is missing
• addNew(term), to add a new word in Italian and its translation in English
a. must add the word passed as an argument with relative translation and communicate
that the insertion was successful
b. must allow the user to add other new pairs of terms (Italian-English) until the user
decides to stop
c. use the error handling features to prevent the program from raising an exception in the
event of an error
• checkWords, to check if a word is already present in the dictionary and in the negative case
must use the addNew function to insert it
a. if an Italian word (key) is already present in the dictionary, it does not have to do
anything else
b. if it is not present, it must ask the user if he/she wants to insert the new term in the
dictionary and, if so, allow it

Attention: dictionary additions take effect only until the work session is closed, by closing Python or
simply restarting. Each time the program is re-run the dictionary will contain only the five initial
words. To make the additions definitive, it is necessary to save the dictionary dict_ita_eng in a text
file to be read by the program at the next execution, a topic covered in lesson 14.

Potrebbero piacerti anche