Introduction to Programming 2024/2025 (CC1024) - DCC/FCUP

Practical Class #08 - (Advanced) Strings (18/11 to 22/11)


Exercises for submission

In what concerns the "exercises during classes" component of the evaluation, the exercises that you can submit for this class are:

Deadline for submission: December 7th (submit to IP's Mooshak)

You are encouraged to talk to the professors and your colleagues if you encounter difficulties. However, any more direct help that you receive should be acknowledged in the comments of the code you submit.
After the deadline the problems will still be available on Mooshak, but the submissions will not count towards your grade.
Each practical class is worth 10% of your final component for exercices during class. Since there will be 11 classes with submissions, you can achieve maximum grade even without doing all classes.
For a problem to count you must pass all tests (that is, to have an accepted). Even if you solve all problems, the maximum on one class is 100%.
To obtain 100% it will always be enough to solve the main exercises.


With the exercises in this class you will develop the following skills:

  1. Explore function definition and function calling
  2. Understand the strings and how to create them
  3. Know how to go from a character to an integer code and vice-versa
  4. Know how to split and join strings
  5. Know how to use a multitude of string methods
  6. Know how to use string formatting
  7. Gain experience with some algorithmic patterns

If you feel stuck, go back and revise all the lectures, but in particular the following ones: T14: More (on) Strings


This week's theme for the exercises is Game of Thrones.

Our suggestion is that you first try to solve on you own but if you have difficulties or after having Accepted, you should check the hints that are provided here.


1) A first problem: lower and upper cases [main]


For this exercise you will be solving the problem [IP075] Ancient Texts.
Read the statement, code and try to submit an Accepted solution. Don't forget to test first on your computer!

Hints

- Use three variables to keep track of the three numbers you need to compute

- Just traverse all the strings in the list and use the str.islower() and str.isupper() to update the variables

- In the end just return a tuple with everything



2) Finding substrings [main]


For this exercise you will be solving the problem [IP076] Keyword Occurrences.
Read the statement, code and try to submit an Accepted solution. Don't forget to test first on your computer!

Hints

- Use str.find() to find the first ocurrence of the string

- Afterwards, keep checking for the next occurrence starting on... the position right after the last occurrence!

- Keep adding the positions to a list until you get a -1 ("not found")



3) Unique Words [main]


For this exercise you will be solving the problem [IP077] The Art of Storytelling.
Read the statement, code and try to submit an Accepted solution. Don't forget to test first on your computer!

Hints

- Use str.split() to obtain a list of words

- Use a set to keep track of existing words

- Traverse the list of words and insert them all on the set (what happens to the repeated words?)

- In the end the result is just the number of elements of the set! [that you can obtain with (len)]



4) Caesar Cipher [main]


For this exercise you will be solving the problem [IP078] Secret Message.
Read the statement, code and try to submit an Accepted solution. Don't forget to test first on your computer!

Hints

- Use ord() to convert each letter to its numeric form

- To make a shift of k you just need to add k and then use chr() to return to a letter

- Be careful with the "wrap-around" when the summation moves you beyonf the last letter (suggestion: use the % operator; can you see how?)

- Don't forget to only convert letters (in this problem you can use str.islower() to check



5) Reading a CSV [main]


For this exercise you will be solving the problem [IP079] Mixed Up Columns.
Read the statement, code and try to submit an Accepted solution. Don't forget to test first on your computer!

Hints

- Use str.split(",") to split the CSV into a list

- You know how to swap two list valies, right?

- Afterwards, just use ",".join() to put the list back into a string



Extra exercises for consolidating your knowledge [extra]

Do you want to continue your adventure on the world of Westeros? Here are more exercises 🙂

6) Parsing and converting to numbers


For this exercise you will be solving the problem [IP080] Intercepted Messages.
Read the statement, code and try to submit an Accepted solution. Don't forget to test first on your computer!

Hints

- What if you convert any non digit to a space? How can you then obtain a list of the integers in string form?

- To convert a number you can simply use... int()


7) Sorting first letters


For this exercise you will be solving the problem [IP081] A Girl has no Name.
Read the statement, code and try to submit an Accepted solution. Don't forget to test first on your computer!

Hints

- Use a map to track the frequency of all first letters

- In the end produce a list of tuples with the frequency and letters: since the normal sort will use increasing order, what should you store on each tuple an in what order? Do you remember something similar we did on the lectures?


8) A neatly formatted table


For this exercise you will be solving the problem [IP082] Night's Watch.
Read the statement, code and try to submit an Accepted solution. Don't forget to test first on your computer!

Hints

- Start by finding the maximum length of a string in the list

- With that length calculated, can you draw the first line?

- Now you can just cycle trough all columns and check whenever you need to change rows 😉


9) Vigènere Cipher


For this exercise you will be solving the problem [IP083] Three-Eyed Raven.
Read the statement, code and try to submit an Accepted solution. Don't forget to test first on your computer!

Hints

- What about solving one problem on your own? 😁



10) Challenge exercise [challenge]

Congratulations, brave solver of the Game of Strings! 🎉

You have demonstrated the skill and the mind of a true Maester, solving each riddle from the cold North to the burning South. You have decrypted messages with the wisdom of Tyrion, transformed data with the precision of Jon Snow, and outsmarted even the cleverest traps set by Littlefinger himself. The Faceless Men, the Night's Watch, and even the Three-Eyed Raven would be impressed with your mastery!

But beware, for one final challenge remains. 🐉🔥

Daenerys Targaryen, the Mother of Dragons, is not easily satisfied. There is one last puzzle you must conquer, one final problem to prove your worth. Succeed, and you will earn the respect of the Seven Kingdoms. Fail, and you may find yourself facing the fiery wrath of Drogon...


For this exercise you will be solving the problem [IP084] Dracarys!
Read the statement, code and try to submit an Accepted solution to the following problem. Don't forget to test first on your computer!

Happy coding! 😊