Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Fundamentals Exercises

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Programming Techniques for NLP

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.

1 Basic data and operations


1.1 Hello World
When learning to program, the first most common program that students do is “Hello World”. That will be the
first program we do.
Make a program that prints “Hello World!”.

Hello World!

1.2 Hello my friend


Write a program that will ask the user for a name and say "Hello" to. For example, if you have the name "Peru",
the communication with your program would be something like this:

What’s your name? Peru


Hello Peru

The communication with Amaia, would be:

What’s your name? Amaia


Hello Amaia

Hint: To ask the user for a name, use input.

1.3 Next number


Write a program that will ask the user for a number and it will print the next number. For example, by entering
the number 8, the program returns 9.

Enter a number: 8
The next number is 9

Year: 2021/2022 GIPUZKOAKO CAMPUSA


Lecturer: Ander Soraluze and Olatz Perez de Viñaspre CAMPUS DE GUIPUZCOA
Telephone: 943 01 {7114, 5089} Paseo Manuel de Lardizabal, 1
E-mail: {ander.soraluze,olatz.perezdevinaspre}@ehu.eus 20018 Donostia (Gipuzkoa)
Programming Techniques for NLP
2022/2023

1.4 Year of birth


Write a program that will ask the user how old he or she is, and calculate and print his or her year of birth
(regardless of months). For example, at the age of 33, your program will print 1987.

How old are you? 34


Your year of birth is 1987.

1.5 Degree calculator


Make a program that will translate degrees from the Celsius scale to the Fahrenheit scale. To do this, the
formula is as follows:

where

• C is the temperature measured in Celsius


• F is the temperature in Fahrenheit

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:

1. Read degrees in Celsius format from the keyboard


2. Convert text to number (int)
3. Apply the formula to calculate the Fahrenheit

4. Print the result

For example, if it is 20 degrees Celsius, your program would work like this:

How many Celsius? 20


68.0º Fahrenheit

On the other hand, with 0 degrees Celsius, the result would be different:

How many Celsius? 0


32.0º Fahrenheit

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.6 Functions till now


Repeat exercises from 1 to 5 using functions.
You can name the new files in this way: ex-06-01.py, ex-06-02.py and so on. For the name of the func-
tions, use the exercise name in lower case and replacing white-spaces with underscores “_" (hello_world,
hello_my_friend, next_number, year_of_birth and degree_calculator)
From now on, for all the exercises we will provide you a test code. For the correct execution of the tests,
you should give to your functions certain name we will determine for each exercise (exactly that name, letter
by letter), and you should follow the instructions for input and output values. For the previous exercises this is
the information needed to run the tests:

1. Hello World!

• Function name: hello_world


• Input values: None
• Output values: a String containing the message to be printed
2. Hello my friend

• Function name: hello_my_friend


• Input values: a String containing the name
• Output values: a String containing the message to be printed
3. Next number

• Function name: next_number


• Input values: a number
• Output values: a number, that is the following to the input
4. Next number

• Function name: year_of_birth


• Input values: a number containing the age
• Output values: a number, that is the year of birth only considering the year
5. Degree calculator

• Function name: degree_calculator


• Input values: a String containing the age
• Output values: a number, that is the year of birth only considering the year

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:

python3 -m unittest -v test_fundamentals.py -k degree_calculator


The general syntax of the call is as follows (replace the bold words):

python3 -m unittest -v test_file.py -k function_name

3
Programming Techniques for NLP
2022/2023

If it does not work, use the following alternative command:


python3 -m unittest -v test_fundamentals.TestCode.degree_calculator

The general syntax of the alternative call is as follows (replace the bold words):

python3 -m unittest -v test_file.TestCode.test_function_name

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:

Enter the password: kukuakEgitenDuKuku


Access allowed

However, if you enter the wrong password, another message should be displayed:

Enter the password: helloFriend


Access denied

Information about the function for the tests:

• Function name: password


• Input values: a String
• Output values: a String, determining if the access has been allowed or denied.

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

In the case of the number 0, say it is positive.


Information about the function for the tests:

• Function name: is_positive


• Input values: a number

• Output values: a Boolean, being True if the input number is positive and False otherwise.

4
Programming Techniques for NLP
2022/2023

1.9 Positive and even?


Write a program that asks the user for a number and says whether that number is positive. In cases where it
is positive, the program must indicate whether this number is odd or even.
To find out if a number is even, we need to calculate the remainder of it. The % symbol is used for this.
For example, the code
remainder = number % 2
will store the remainder of the division between the number variable and 2 in the variable remainder. In the
case of positive numbers, the remainder will be 0, and in the case of odd numbers 1.
For example, if the number entered is 10, your program should display the following:

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

In the case of the number 0, treat it as positive.


Information about the function for the tests:

• Function name: positive_and_even


• Input values: a number
• Output values: a String, being the output message to be printed by the main program

Optional: you can reuse the 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:

Where are you from? Donostia


Hello Donostiar!

If you were from Aberdeen, it would displey this other:

Where are you from? Aberdeen


Hello Aberdonian!

And if you include a town that does not appear in the list:

5
Programming Techniques for NLP
2022/2023

Where are you from? Azpeitia


I’m sorry, I don’t know your town...

Information about the function for the tests:

• Function name: inhabitants


• Input values: a String containing a city name
• Output values: a String, being the output message to be printed by the main program

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:

• Function name: transportation


• Input values: a Boolean and a number (use -1 to give a value in the case that is its raining)
• Output values: a String, being the output message to be printed by the main program

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

Information about the function for the tests:


• Function name: first_and_last_characters
• Input values: a String containing one word
• Output values: a String containing the message to be printed. Remember to append the newlines (\n)
Note: You do not need loops for this exercise.

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!

Another example of a program would be:


Your team: Osasuna
Give me a(n) O, O!
Give me a(n) s, s!
Give me a(n) a, a!
Give me a(n) s, s!
Give me a(n) u, u!
Give me a(n) n, n!
Give me a(n) a, a!
What’s that spell?
osasuna! osasuna!

7
Programming Techniques for NLP
2022/2023

Information about the function for the tests:


• Function name: cheerleader

• Input values: a string containing the team name


• Output values: a String, being the output message to be printed by the main program. Remember to
append the newlines (\n)
Optional: In the last line, when the group name is printed, capitalise it. To do this, examine the upper()
function.

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

If the number entered is 0, the following should be printed:

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:

• Function name: counter


• Input values: a number
• Output values: a String, being the output message to be printed by the main program. Remember to
append the newlines (\n)

2.4 Lift only up


Following the functionality of an elevator, write a program that prints the floors one by one. For this exercise,
assume that the lift only goes up.
By requesting the user the current floor and the target floor, the program will print all the intermediate
floors from the source, to the destination (both inclusive) one by one and in an orderly manner. For example,

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

Information about the function for the tests:


• Function name: lift_only_up
• 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.

2.5 Lift up and down


We will improve the lift from the previous exercise in this exercise, and allow it to work both up and down.
By requesting the user from the current floor and the target floor, the program will print all the intermediate
floors from the source, to the destination (both inclusive) one by one and in an orderly manner. For example:

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

2.6 Double factorial


In mathematics, a double factorial of a number is defined as: It is the result of multiplying all the numbers
between 1 and that number, which share the same parity (odd or even).
For example, the double factorial of the number 9 would be:
• 9!! = 9 * 7 * 5 * 3 * 1 = 945
On the other hand, the factorial of the number 10 would be as follows:
• 10!! = 10 * 8 * 6 * 4 * 2 = 3840
Your program output should be as follows for the previous two examples:

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)

2.7 Lift with while


Change the code of the exercise 2.5 to implement it with a single while loop.
By requesting the user from the current floor and the target floor, the program will print all the intermediate
floors from the source to the destination (both inclusive) one by one and in an orderly manner. For example:

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

Information about the function for the tests:


• Function name: lift_with_while
• Input values: a number
• Output values: a String, being the output message to be printed by the main program. Remember to
append the newlines (\n)

2.8 Guess game


You have to code a guessing game, which will tell the user to guess it’s favourite food.
For this specific exercise, suppose that the food to be guessed is “chocolate”. Here is an example of how the
program works:

What is my favourite food?


Try it: pasta
You are wrong.
Try it: steak
You are wrong.
Try it: chocolate
You guessed it!!!!!!

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

2.9 Look for factors


Ask for a number, and write a program that will calculate all the factors for that number. A factor in the
number n is an integer, which is multiplied by another integer to obtain the number n.
For example, for the number 8, its factors are 1, 2, 4, and 8, because:

• 1x8=8
• 2x4=8

And the factors of the number 24 times are 1, 2, 3, 4, 6, 8, 12 and 24:

• 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

Information about the function for the tests:

• Function name: look_for_factors


• Input values: a number
• Output values: a String, being the output message to be printed by the main program. Remember to
append the newlines (\n)

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.

How many levels? 4


__
|_
|_
|_
________|

How many levels? 1


__
__|

Information about the function for the tests:


• Function name: steps

• Input values: a number


• Output values: a String, being the output figure to be printed by the main program. Remember to append
the newlines (\n)

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)

If we run the code, it would display:

byebyebye

13
Programming Techniques for NLP
2022/2023

3 Strings and lists


3.1 Text info
Ask the user to write a text and make a program that will analyze it. The information to be displayed is as
follows:

• 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):

Enter a text: kukuak udaberrian kuku


Size of the text: 22
Number of kuku’s: 2
In lower case

Enter a text: Kuku miku, txoriak umeak sasian ditu


Size of the text: 36
Number of kuku’s: 1
Both lower and upper case

Enter a text: A ZE PAREA KARAKOLA TA BAREA


Size of the text: 28
No kuku this way
In upper case

Information about the function for the tests:

• Function name: text_info


• Input values: a String
• Output values: a String, being the output message to be printed by the main program. Remember to
append the newlines (\n)

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.

3.2 Broken keyboard


A friend of yours has broken the keyboard and cannot type the letters a, e and u. Prepare a translator to
understand his messages. The equivalences you have agreed on are as follows:

• a = %%

• e = ##
• u = ###

Here is an example of how your program should work:

14
Programming Techniques for NLP
2022/2023

What did he write? I s###dd##nly brok## my k##ybo%%rd


He meant: I suddenly broke my keyboard

Information about the function for the tests:


• Function name: broken_keyboard
• Input values: a String
• Output values: a String, being the translated message. Be careful: not all the string to be printed, just
the message!

3.3 Language of the word


Write a program that asks for a word, and it will have to decide what the language of that word is. Since the
program is in its beginning, it will only consider three suffixes:

• -zio = Basque
• -cion = Spanish
• -tion = English

For example:

I bet I hit the language! action


English!

I bet I hit the language! animacion


Spanish!

I bet I hit the language! burutazio


Basque!

I bet I hit the language! python


Ups... I don’t know!

Information about the function for the tests:


• Function name: language_of_the_word
• Input values: a String
• Output values: a String, being the output message to be printed by the main program.

3.4 Rainy days


Ask for the weekdays that will rain next week, and calculate the number of days without rain.
For example:

On what weekdays will it rain? Monday Tuesday Friday


Number of days without rain: 4

Information about the function for the tests:

15
Programming Techniques for NLP
2022/2023

• Function name: rainy_days


• Input values: a String

• 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.5 Order students names


Make a program that sorts students’ names in alphabetical order. To do this, ask the user for a list of names
separated by spaces, and write a program that will print them one by one in alphabetical order.
For example,

Students: Mikel Ane Mattin Jule Aintzane


List of students:
Aintzane
Ane
Jule
Mattin
Mikel

Information about the function for the tests:


• Function name: order_students_names

• Input values: a String


• Output values: a String, being the output message to be printed by the main program. Remember to
append the newlines (\n)

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,

Do you know any palindrome? Able was I ere I saw Elba


Good!!

Do you know any palindrome? zozoak beleari ipur beltz


No, you missed it...

Information about the function for the tests:


• Function name: palindromes
• Input values: a String

• 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

3.7 Total spent


Receive the amount of money spent separately, and write a program that will calculate the total amount.
For example,

Enter the spent money: 12 15 10 5 3 8


Total: 53€

Information about the function for the tests:


• Function name: total_spent

• Input values: a String


• Output values: an integer, being the total amount of the spent money.

3.8 How many words


You are learning a new language, and you want to count the different words you know. Write a program that
will help you in this story, to which you will give the words one by one (until you enter the empty text), and at
the end, it will print the number of different words included.
For example,

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

• Output values: an integer, being the total amount of different words.

17
Programming Techniques for NLP
2022/2023

3.9 Red and blue cars


As you wait to cross the road, you see cars passing in front of you, and you’re curious to know if the red or blue
cars are the most widespread. In a character string, write a program that recives the colors of the cars in past
order, and will print the quantities of both red and blue cars.
For example,
Cars: grey red white white blue white black green yellow grey white
Red: 1
Blue: 1

Cars: blue green grey black red grey blue grey black grey red red
Red: 3
Blue: 2

Cars: blue green grey grey


Red: 0
Blue: 1

Information about the function for the tests:


• Function name: red_and_blue_cars
• Input values: a String
• Output values: two integers, being the first one the number of red cars and the number of blue cars the
second one

3.10 Secret message


You have been asked to perform a program to decrypt hidden messages. This is the code they use to encode
the message:
• the words are read in reverse order
• only words that start with a capital letter are noticed
So the following string, DA seFEd DIBERTIGARIA ySvEEsN PYtHoN, carries another message: python diber-
tigarria da ("python is fun" in Basque).
An example of a program:
code: tHIs PrOGRam teACheS To LeaRN pYThOn To cATs . IMPORTANT : Is baD pROGRAMING
ExERcIsES aNd DoING trICKs
message: doing exercises is important to learn to program

Another example with the text in Basque:


code: oNDo Ikasteko pyTHoNeZ ProGRaMatZEN eZ Da GaRRaNTziTSua eZER EGiTea eTa ARikETaK eRE
eZ
message: ariketak egitea garrantzitsua da programatzen ikasteko

To better read the message, we will change all characters to lowercase.


Information about the function for the tests:
• Function name: secret_message
• Input values: a String
• Output values: a String, being the the message encrypted in the original string

18
Programming Techniques for NLP
2022/2023

3.11 Super Anagrams


Can you raed tihs senentce? Apparently, the human brain can read misspelled words correctly if two conditions
are met:

• having two words anagrams of each other: having the same characters, but in any order

• the first and last characters being the same

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,

Enter two words: lehoia leihoa


Super Anagram!

Enter two words: arranondo ondarroa


What?

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

3.12 Middle number


We will create a function that uses three more functions:

• 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,

Enter the numbers: 2 6 4


The average of the three numbers: 4.0
Middle number: 4
It is even

19
Programming Techniques for NLP
2022/2023

Enter the numbers: -5 -7 3


The average of the three numbers: -3.0
Middle number: -5
It is odd

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

4 Dictionaries and files


4.1 Word2SMS
Do you remember how we used to write text messages on our cell phones? In this exercise you will ask the user
for a word and tell them which numbers to press (only which key to press, and in what order, not how many
times).
For example,

Enter a word: KAIXO


52496

Or,

Enter a word: PYTHON


798466

Figure 3: Retro telephone’s keyboard

Information about the function for the tests:


• Function name: word2sms
• Input values: a String
• Output values: a String, being the numbers to press to code the input word
Optional: what if the input string is in lower case? Could you fix it?

4.2 Favourite colour


Write a program that will save the favorite name color pairs in a dictionary. Afterthat, it will print everyone’s
favorite color, in alphabetical order.
For example,

21
Programming Techniques for NLP
2022/2023

Name and Colour: Olatz Purple


Name and Colour: Ane Blue
Name and Colour: Mikel Pink
Name and Colour:
Ane Blue
Mikel Pink
Olatz Purple

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:

for .... in sorted(colours):

4.3 Cars colours


Make a program to tell the most common colour of cars. The program should read the color of each car until
a blank line is entered. It will then have to print all the different colors along with the quantities.
The tests will not look at the order of the results, but it is recommended to sort them by numbers (from
highest to lowest). To do this, examine the sorted() function.
For example,

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:

• Function name: cars_colours


• Input values: None
• Output values: a String, being the output message to be printed by the main program

22
Programming Techniques for NLP
2022/2023

4.4 Words frequency


Write a program that tells the frequency of the words used in a text. The program should read several lines
written by the user (via input, until a blank line is entered), and will print the frequency of each word. In this
case, print the words and their frequency in reversed alphabetical order. Don’t worry about punctuation.
For example,

Enter a line: hello friends


Enter a line: how are you going
Enter a line: friends
Enter a line:
are 1
friends 2
going 1
hello 1
how 1
you 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:
• Function name: words_frequency
• Input values: None

• Output values: a String, being the output message to be printed by the main program

4.5 Shout requests


Make a program that will read and "shout" the file of requests. To do this, read the file line by line, and print
the entire line in uppercase.
Take a look at the content on the left in the requests.txt tab:

Tomato salad
Pumpkin puree
Roast chicken
Pudding with ice cream

Your program should print the following:

TOMATO SALAD
PUMPKIN PUREE
ROAST CHICKEN
PUDDING WITH ICE CREAM

Information about the function for the tests:

• Function name: shout_requests


• Input values: a String containing the file name including the path (p.e. "data/requests.txt"
• Output values: a String, being the output message to be printed by the main program

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:

Abendua jai huts eta gau huts


Txapela buruan, ibili munduan
Alferrarentzat jana eta langilearentzat lana ez da inoiz faltako
Usteak erdia ustel
Zozoak beleari ipurbeltz
Zenbat buru, hainbat aburu
Ogi gogorrari, hagin zorrotza
A ze parea, karakola ta barea!

Information about the function for the tests:


• Function name: virus
• 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

4.7 Word in the book


Write a program that asks the user for a word that will tell us if that word appears in the book, and will also
tell you which lines in the book appear.
The book is stored in the book.txt file.
For example,

Word to search: eder


Found in line: 3
Found in line: 39
Found in line: 44
Found in line: 46
Found in line: 71

If it does not appear, this other message will be displayed:

Word to search: python


Not found

24
Programming Techniques for NLP
2022/2023

Help: You can use a boolean to control whether it is found or not


Information about the function for the tests:

• Function name: virus


• Input values: two Strings, the first containing the file name including the path and the second the word
to be searched
• Output values: a String, being the output message to be printed by the main program

4.8 Spanish elephants in file


Elephants have joined us in the file. Write a program that will examine the file line by line, and examine whether
the elephants are hidden in each line.
To do this, the condition is to find all the characters that make up the Spanish word for elephant "elefante"
in that line. That is:

• 3 times "e"
• one "l"

• one "f"
• one "a"
• one "n"

• one "t"

At least these quantities should appear, but not exactly those.


The contents of the "sayings.txt" file should be analized, and the output of your program should be:

Spanish elephant in this line: 3


Spanish elephant in this line: 7
Spanish elephant in this line: 10

Information about the function for the tests:


• Function name: spanish_elephants_in_file

• 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:

if x >= 0 and x <10:


print("A single digit")

Review the method that gives the number of appearances of a character. You can also ask Google... ;-)

25
Programming Techniques for NLP
2022/2023

5 Advanced files and objects


5.1 Scrabble game
To play international scrabble, write a program that will calculate points based on both English and Basque
points. To do this, you must read the files points_en.txt and points_eu.txt and display the two punctuations
of the word entered by the user according to its content.
For example,

Word: kaixo
Points in Basque: 15
Points in English: 16

Or,
Word: house
Points in Basque: 13
Points in English: 8

Information about the function for the tests:


• Function name: scrabble_game
• Input values: a String containing the word
• Output values: two integers, being the first the points gotten in Basque and the second the points in
English
You can improve your code if you load the files info in a separate function.

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

Information about the function for the tests:


• Function name: usernames

• 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!

5.3 London’s weather


We have received some information about the weather in London in the weather.csv file. It contains hourly
statistics, which includes the following header:

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):

• Date in Year-Month-Day format (2018-4-17, p.e)


• Temperature
• Precipitation
• Wind-speed

In addition, it will need to display the following information about the entries that have met the temperature
condition:

• How many entries have met the condition


• The average temperature of those entries
• The summation of all the precipitation of those entries

• The average of the wind-speed of those entries


For example,

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

You might also like