This is an archived version of the course and is no longer updated. Please find the latest version of the course on the main webpage.

Selection - Exercises

Many thanks to the following people for contributing to these exercises: Joe Stacey, Oana Cocarascu

Ex1: Write a program that prints ‘empty’ if a list is empty and ‘not empty’ if the list contains elements.
Ex2: Write a program that prints a text if it has less than 15 words, or otherwise print a message that the sentence is too long.
Ex3: Given two strings, str1 and str2, write a program that prints the string with the maximum length.
  • Or if both strings have equal length, starting with the one that is first in alphabetical order.
Ex4: Write code that prints a string input only if it is a sentence, in other words if it satisfies the following critieria:
  1. It doesn’t have any spaces at the beginning or at the end,
  2. and if contains exactly 1 full stop.

Otherwise print a message saying which condition has failed (or if both conditions have failed).

Ex5: Write a program that takes three integer inputs (a, b and c), which then performs the following:
  • Puts the integers into ascending order,
  • Prints the first of these numbers that are divisible by 7.
  • If none of the numbers are divislbe by 7, print the first number that is divisible by 11.
  • If none of the numbers are divisible by 7 or 11, print this in a statement.
Ex6: Write a program that checks and prints to the console whether a year is a leap year or not:

How to tell if a year is a leap year (impress your friends with this fun fact): A year that is exactly divisible by 4 is a leap year, except for years that are exactly divisible by 100, but these centural years are leap years if they are exctly divisible by 400. For example, the year 1700, 1800, and 1900 are not leap years, but the years 1600 and 2000 are.