Fundamentals Exercises
Fundamentals Exercises
Fundamentals Exercises
2022/2023
Fundamentals exercises
Olatz Perez de Viñaspre and Ander Soraluze
Abstract
This is the first batch of exercises of the Programming Techniques for NLP subject. All the exercises
must be submitted to eGela in a .zip file. Each exercise inside this compressed folder must be identified by
it’s corresponding number. For example: ex-01.py, ex-02.py and so on. It is not compulsory to follow file
naming guidelines but it has to be clear the exercise it corresponds to. Note that the expected input/output
for each exercise’s program is given in this document, being the bold and blue text the input from the user.
Hello World!
Enter a number: 8
The next number is 9
where
Using what you have learned so far, program the calculator that will do the translation.
To do this, we will divide the problem into small parts (algorithm) and then implement these steps step by
step. For example, the steps might be:
For example, if it is 20 degrees Celsius, your program would work like this:
On the other hand, with 0 degrees Celsius, the result would be different:
Note that even if we pass the degrees to the integer type, the result will be a real number, as we have made
an incomplete division in the formula (9/5).
2
Programming Techniques for NLP
2022/2023
1. Hello World!
To run the tests, you should do the following from the terminal:
1. Run the 00-prepare_testing.py script (it copies all your functions in a single file called functions.py):
python3 00-prepare_testing.py
2. Run the test file provided for the exercise collection setting the function to be tested. For example:
3
Programming Techniques for NLP
2022/2023
The general syntax of the alternative call is as follows (replace the bold words):
Note: the creation of the unit tests for testing the code is out of the scope of this course. In any case, if any
of you has interest on it contact the lecturers for further information.
1.7 Password
Write a program that tells you whether you have guessed the password to access the computer (kukuakEgiten-
DuKuku). If you guess, you should see the following message:
However, if you enter the wrong password, another message should be displayed:
1.8 Positive?
Write a program that asks the user for a number and says whether that number is positive. For example, if the
number entered is 10, your program should display the following:
Enter a number: 10
The entered number is positive
On the other hand, entering the number -5 would print the following:
Enter a number: -5
The entered number is not positive
• Output values: a Boolean, being True if the input number is positive and False otherwise.
4
Programming Techniques for NLP
2022/2023
Enter a number: 10
The number entered is positive and even
If the number entered is 31, your program will display the following:
Enter a number: 31
The number entered is positive and odd
On the other hand, entering the number -5 would print the following:
Enter a number: -5
The number entered is not positive
1.10 Inhabitants
Write a program that includes the name of a city or town that will display the name of its inhabitants (demonym).
Here are some things to keep in mind:
Aberdeen: Aberdonian Istanbul: Istanbulite Donostia: Donostiarra Sydney: Sydneyite
If you enter the name of a town that is not on the list, you will need to write that the program does not
recognise you.
For example, if you enter the town of Donostia, the following would be displayed:
And if you include a town that does not appear in the list:
5
Programming Techniques for NLP
2022/2023
1.11 Transportation
Write a program that will help you choose the type of transportation to get to work.
The first condition for making the decision will be the weather. If it’s raining, you should take the bus.
If it’s not raining, then your type of transportation will decide the distance. If the distance is more than 10
kilometres, you should take the bus. On the other hand, if the distance is between 2 and 10 kilometres (both
inclusive), then you should go by bike, and if it is less than 2 kilometres on foot. The distance must always be
an integer.
Your program will ask for distance, only when necessary. That is, if it is raining, then it will also ask the
user for the distance.
Is it raining? Yes
Better to go by bus.
Is it raining? No
How many kilometres is the workplace? 8
Better to ride a bike.
Is it raining? No
How many kilometres is the workplace? 1
Better to walk.
The answers given will always be Yes, No or an integer for distances, so you do not have to worry about errors.
Information about the function for the tests:
Optional: Make sure the answer is Yes or No, and that it is a positive number before going from each
question on.
6
Programming Techniques for NLP
2022/2023
2 Loops
2.1 First and last characters
Make a program that asks the user for a word, and print the first and last character of that word.
For example, if you enter the word "Home", your program should print the following:
Enter a word: Home
First character: H
Last character: e
On the other hand, with the word "elephant", it should work like this:
Enter a word: elephant
First character: e
Last character: t
2.2 Cheerleader
Write a program that will cheer for your favourite sports team as a cheerleader. Your program should work like
this:
Your team: baskonia
Give me a(n) b, b!
Give me a(n) a, a!
Give me a(n) s, s!
Give me a(n) k, k!
Give me a(n) o, o!
Give me a(n) n, n!
Give me a(n) i, i!
Give me a(n) a, a!
What’s that spell?
baskonia! baskonia!
7
Programming Techniques for NLP
2022/2023
2.3 Counter
Make a program that asks for a positive number, and prints the numbers from 0 to that number. For example:
Enter a number: 5
0
1
2
3
4
5
Enter a number: 0
0
Don’t worry if the number you entered is negative. Your code should not print anything (without controlling
anything).
Information about the function for the tests:
Current floor: 3
Target floor: 6
Floor number: 3
Floor number: 4
Floor number: 5
Floor number: 6
8
Programming Techniques for NLP
2022/2023
Current floor: 0
Target floor: 1
Floor number: 0
Floor number: 1
Current floor: 3
Target floor: 6
Floor number: 3
Floor number: 4
Floor number: 5
Floor number: 6
Current floor: 4
Target floor: 2
Floor number: 4
Floor number: 3
Floor number: 2
For this exercise, you will have to put together the different concepts learned: conditions, loops, range
parameters, ...
Information about the function for the tests:
• Function name: lift_up_and_down
• Input values: two numbers, the source and target floors
• Output values: a String, being the output message to be printed by the main program. Remember to
append the newlines (\n)
Note: Use a for loop in this exercise (you will be asked to implement with a while loop in the 2.7 exercise).
9
Programming Techniques for NLP
2022/2023
Enter a number: 9
Double factorial: 945
Enter a number: 10
Double factorial: 3840
You can find more information about double factoring at the following Wikipedia link:
https://en.wikipedia.org/wiki/Double_factorial
Information about the function for the tests:
• Function name: double_factorial
• Input values: a number
• Output values: a number, being it the double factorial of the input value (you will write the output
message in the main program)
Note: There are multiple ways of solving the exercises.
Some of the solution will need to determinate whether a number is odd or even. Remember that in Python
the % symbol is used to calculate the remainder between two numbers. For example, to print the remainder
between numbers 10 and 2:
print(10 % 2)
Current floor: 3
Target floor: 6
Floor number: 3
Floor number: 4
Floor number: 5
Floor number: 6
Current floor: 4
Target floor: 2
Floor number: 4
Floor number: 3
Floor number: 2
10
Programming Techniques for NLP
2022/2023
In this case, we will do an exception, and all the input() and print() function calls will be allowed inside
the function (otherwise it will not work). Information about the function for the tests:
• Function name: guess_game
• Input values: None
• Output values: None
• 1x8=8
• 2x4=8
• 1 x 24 = 24
• 2 x 12 = 24
• 3 x 8 = 24
• 4 x 6 = 24
The simplest way to find the factors would be to test with all numbers less than n even if there are more
efficient ways to get it. Try it!
Remember that you can use the % operator to calculate the remainder of a division:
10 % 2
11
Programming Techniques for NLP
2022/2023
Your program will print all the factors of this number one by one:
Enter a number: 8
1
2
4
8
Enter a number: 24
1
2
3
4
6
8
12
24
2.10 Steps
Given the number of levels, write a program that draws the ladder. The user will always enter an integer that
is 1 or greater.
Here are two examples of what the program should write.
12
Programming Techniques for NLP
2022/2023
Figure 1: Output of the Visual Studio Code’s terminal Figure 2: Output of the common terminal
Note: you need to run this exercises in a common terminal, as Visual Studio Code’s terminal may not show
some of the underlines you are printing.
Help: Examine carefully what to write on each line, and keep in mind that once the line break is added,
nothing can be changed backwards. Horizontal lines are drawn with an underscore (_) and vertical lines with
the | character.
In addition, remember that you can also do maths with character strings:
text = "bye"*3
print(text)
byebyebye
13
Programming Techniques for NLP
2022/2023
• Text size
• In the text, if the word kuku (cuckoo in basque) appears, how many times does the word "kuku" appear
(in uppercase or lowercase), and the message that does not appear otherwise
• Whether the text (in its entirety) is written in uppercase, lowercase or mixed letter
Here are some examples of how the program works (the texts in the examples are proverbs in Basque):
Optional (not tested): instead of one single function, you can improve it dividing it in smaller functions.
The optimal way of programming functions is that each of them performs a single task.
• a = %%
• e = ##
• u = ###
14
Programming Techniques for NLP
2022/2023
• -zio = Basque
• -cion = Spanish
• -tion = English
For example:
15
Programming Techniques for NLP
2022/2023
• Output values: an integer, being the number of days that will no rain next week.
Note: Don’t pay attention to what the user writes, but rather how many words they type
3.6 Palindromes
Request a character string and run a program that displays whether it is a palindrome. To do this, the program
will not take into account spaces.
Palindrome: a word that is read equally from left to right and from right to left.
For example,
• Output values: a String, being the output message to be printed by the main program.)
Help: We have seen two ways to remove spaces: split + join or replace
16
Programming Techniques for NLP
2022/2023
Word: waaw
Word: deedeet
Word: jerejef
Word: waaw
Word: baxna
Word: jerejef
Word:
You know 4 different words!
Word: rafiki
Word: kinywaji
Word: nzuri
Word: kwaheri
Word: moto
Word:
You know 5 different words!
Word: Salam
Word:
You know 1 different words!
In this case, we will do an exception again, and all the input() function calls will be allowed inside the
function (otherwise it will not work). Information about the function for the tests:
• Function name: how_many_words
• Input values: None
17
Programming Techniques for NLP
2022/2023
Cars: blue green grey black red grey blue grey black grey red red
Red: 3
Blue: 2
18
Programming Techniques for NLP
2022/2023
• having two words anagrams of each other: having the same characters, but in any order
Thus, Arranondo is an anagram of the word Ondarroa, and vice versa. Super Anagrams are a type of
anagram whose first and last letters are the same.
Your program will read two words in a single line. The program will have to resolve whether the two words
are Super Anagrams by displaying a message to clarify this. Note that for single-character words, the first and
last characters will be the same.
For example,
Help: Remember that strings could not be sorted, but we saw a way to do this: by turning the string into
a list (with the list function)
Information about the function for the tests:
• Function name: super_anagram
• Input values: two String
• Output values: a String, being the output message to be printed by the main program
• A function that calculates the average of the given three numbers (and returns that number).
• A function that given three numbers (they do not have to be ordered), it will calculate what the middle
number is in numerical order (and returns that number).
• A function that given a number, displays whether that number is even or odd.
• A function that given a String with three integer numbers separated by spaces, returns a String with the
information of the three previous functions.
The main program will ask the user for three numbers (in a single line), and will call the function that will
prepare the output message. To do so, this functions will store the numbers in variables, and make the necessary
calls to the functions to display the average, the middle number, and if this last number is even or odd.
For example,
19
Programming Techniques for NLP
2022/2023
Those are the functions you need to implement and the information needed to properly execute the test:
1. Average
• Function name: average
• Input values: 3 integers
• Output values: a double, being the average of the three input numbers
2. Middle number
• Function name: middle_number
• Input values: 3 integers
• Output values: an integer, being the middle one in numerical order
3. Ever or odd
• Function name: str_odd
• Input values: an integers
• Output values: a String, saying if the number is even (will return “even") or odd (will return “odd")
4. Number info
• Function name: number_info
• Input values: an integers
• Output values: a String, saying if the number is even (will return “even") or odd (will return “odd")
20
Programming Techniques for NLP
2022/2023
Or,
21
Programming Techniques for NLP
2022/2023
In this case, we will do an exception again, and all the input() function calls will be allowed inside the
function (otherwise it will not work). Information about the function for the tests:
• Function name: favourite_colour
• Input values: None
• Output values: a String, being the output message to be printed by the main program
Each name will appear only once. You can use the sorted() function to print the names in an ordered way:
Car: red
Car: white
Car: blue
Car: green
Car: white
Car: purple
Car:
white cars: 2
red cars: 1
blue cars: 1
purple cars: 1
green cars: 1
In this case, we will do an exception again, and all the input() function calls will be allowed inside the
function (otherwise it will not work). Information about the function for the tests:
22
Programming Techniques for NLP
2022/2023
In this case, we will do an exception again, and all the input() function calls will be allowed inside the
function (otherwise it will not work). Information about the function for the tests:
• Function name: words_frequency
• Input values: None
• Output values: a String, being the output message to be printed by the main program
Tomato salad
Pumpkin puree
Roast chicken
Pudding with ice cream
TOMATO SALAD
PUMPKIN PUREE
ROAST CHICKEN
PUDDING WITH ICE CREAM
23
Programming Techniques for NLP
2022/2023
4.6 Virus
The teacher asks you to list the Basque proverbs you know as homework. Apparently, a virus has entered
your computer and changed part of your work. Thus, throughout your list, it has translated some sayings into
Spanish (literaly...), beginning with "VIRUS!" .
Examine the contents of the "homework.txt" file on the left. Here is an extract:
VIRUS! Estate dormido y comerás fino
Zenbat buru, hainbat aburu
Ogi gogorrari, hagin zorrotza
VIRUS! Si estás agusto no hay cuestas
A ze parea, karakola ta barea!
Your program will have to remove the lines written by the virus, and will leave the statements only in Basque
in the "correct.txt" file. To facilitate correction, in addition to writing to the file, you will also need to display
the correct lines.
The output of your program will be as follows:
• Output values: a String, being the output message to be printed by the main program
24
Programming Techniques for NLP
2022/2023
• 3 times "e"
• one "l"
• one "f"
• one "a"
• one "n"
• one "t"
• Input values: a String containing the file name including the path
• Output values: a String, being the output message to be printed by the main program
Notes: Remember that you can concatenate as many conditions as you want:
Review the method that gives the number of appearances of a character. You can also ask Google... ;-)
25
Programming Techniques for NLP
2022/2023
Word: kaixo
Points in Basque: 15
Points in English: 16
Or,
Word: house
Points in Basque: 13
Points in English: 8
5.2 Usernames
We have a list of students in the class_list.csv file. This file, instead of being separated by commas, is separated
by whitespaces.
Write a program that will suggest usernames based on the names and surnames of classmates. It will use
the following criteria:
• The username will be the student’s name, without spaces, and in lower case.
• If this username is already in use, then the first letter of the surname will be added, also in lower case.
• If this username is already in use, then a number will be added to the first letter of the last name, starting
with 1, and ascending if necessary.
Considering the content of the class_list.csv file, the output of your program should be:
malen
malena
ane
anea
anea1
anea2
aimar
aitor
aitoro
aitoro1
jon
jonander
26
Programming Techniques for NLP
2022/2023
• Input values: a String containing the file name including the path
• Output values: a String, being the output message to be printed by the main program
Note:
See how quotation marks are used in compound names or surnames. Don’t worry, the CSV library will
separate the fields well, and remove the quotation marks.
Help:
To add the number, you can use a loop, which will start testing from 1 and stop when you reach the unsaved
number. There are other options, do it your way!
Year Month Day Hours Minutes Temperature Humidity Pressure Precipitation Cloud-min Sun-min
Wind-speed Wind-direction
Your program will ask the user for a temperature, and then for the records with a temperature above it, the
program must store in another csv file the following information (the header is opstional):
In addition, it will need to display the following information about the entries that have met the temperature
condition:
Enter temperature: 20
Total entries: 43
Average temperature: 22.58
Total precipitation: 0.10
Average wind-speed: 12.72
Note that the file is not separated by commas, but by a tab ("\t").
Information about the function for the tests:
• Function name: londons_weather
27
Programming Techniques for NLP
2022/2023
• Input values: an Integer and two Strings. The integer contains the temperature, the first String contains
the input filename including the path and the second Sting contains the output filename including the
path
• Output values: a String, being the output message to be printed by the main program
Help: To print only two decimals of the double numbers you should use the format function. For example:
out_str = "Rounded value: {:.2f}".format(variable)
28