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

Practical Class #02 - Conditionals and Program Flow (29/09 to 03/10)


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: October 18th (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 have received from other colleagues or LLMs 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 some math operators such as integer remainder (%) and integer division (//)
  2. Explore the usage of boolean expressions (and operators such as and, or)
  3. Explore the usage of of conditionals (such as if, elif, else)
  4. Explore the usage of program flow cycle instructions (such as for, while, range)
  5. Explore the creation of simple algorithms
  6. Consolidate your knowledge of coding, debugging and problem solving

If you feel stuck, go back and revise the lectures T02: The basics | T03: Conditionals | T04: Program Flow


1) Conditional Statements

For this first batch of four problems you will mainly explore the usage of conditional statements (such as if, elif and else) and boolean expressions.


Read the statements, code and try to submit Accepted solutions to all of the following problems. Don't forget to test first on your computer!

  1. [IP008] Absolute-ly
  2. [IP009] Best of 3
  3. [IP010] Grading Papers
  4. [IP011] Buy Me Flowers

We encourage you to speak with your colleagues and with your professors if you need help (including using Discord), but be aware that you should be trying to solve the exercises by yourself!

You get from a course what you invest on it and by actually trying to solve the problems you will learn a lot (including some of the things you will not get correct the first time, which will become lessons you will not forget).

This course is all about "putting your hands dirty" in terms of actually coding, and if you rely on other's code (or tools such as ChatGPT) you will be most of all just fooling yourself... Try to do it and enjoy the rich and dynamic experience we prepared for you! 😊

😁 suprised pikachu...



2) Program Flow and Cycle Instructions

For this second batch of four more problems you will now explore the usage of program flow instructions (such as for, while and range), combined with what you already know.


Read the statements, code and try to submit Accepted solutions to all of the following problems. Don't forget to test first on your computer!

  1. [IP012] FizzBuzz
  2. [IP013] Multiply Mayhem!
  3. [IP014] Odd Feelings
  4. [IP015] Behold a Square

Again, try to do it it by yourself and revisit the lectures, seeking help and hints if you need help.

😅 no pain, no gain!



3) Leveling up

We now propose two more problems which are still more than doable with what you already learned, but might be a bit more difficult from the point of view of the algorithm associated (so it is more about the idea that just the code and the syntax of Python).

These problems include hints that are initially hidden so that they don't become spoilers if you want to try the problems by yourself first. If you get stuck, click the button to open the hint! 🦮


Read the statements, code and try to submit Accepted solutions to all of the following problems. Don't forget to test first on your computer!

  1. [IP016] Like the others?
  2. Hints

    - Keep three variables while you are traversing the numbers:
        . cur giving the size of the current stretch of identical numbers
        . best with the best stretch length until now
        . last with the last number seen

    - At the start, read the first number and assign cur=1 and best=1
    (this is where you are AFTER reading the first number: on a stretch of length 1, which is also the best)

    - Each time you read a new number:
        . if it is the same as the last number, increment cur (the current stretch grows)
        . if it is different, than cur=1 (you start a new stretch)

    - After this, before reading a new number, update the last number and verify if your best was improved


  3. [IP017] Sum me up!
  4. Hints

    - To extract the last digit you only need to... check the remainder of dividing by 10 (n % 10)

    - To remove the last digit and keep the other... divide the number by 10 (n // 10)

    - You can now make a cycle where you repeatedly do this to keep extracting all the numbers until you reach zero:
        Imagine for example that the number is 123. Then:
        . 123 % 10 = 3 and 123 // 10 = 12, leaving you with 12
        . 12 % 10 = 2 and 12 // 10 = 1, leaving you with 1
        . 1 % 10 = 1 and 1 // 10 = 0, leaving you with 0

    - All that's left is to keep summing the last digits whyle you are traversing the number, right?


⭐ Level Up!



Extra exercise for consolidating your knowledge [extra]

So you enjoyed the main exercises so much, that you want more problems to submit? Don't worry we've gor you covered! Here are some exercises to consolidade your knowledge and to show you are coding for more than simply your grade 😃


Read the statements, code and try to submit Accepted solutions to all of the following problems. Don't forget to test first on your computer!

  1. [IP018] More Circle Math
  2. [IP019] I am root
  3. [IP020] Perfection
  4. [IP021] Pointy Business


Challenge exercise [challenge]

So you want one more challenge? Here you go! It is not too hard, but if you can do it you are showing you truly mastered this part of the material!


Read the statement, code and try to submit an Accepted solution to the following problems. Don't forget to test first on your computer!

  1. [IP022] Spin me right round

Happy coding! 😊