Skip to content

Exercises

Tiago Duarte da Silva edited this page Apr 29, 2020 · 3 revisions

Exercise order:

If you've finished all of our exercises and want some more, check out these exercises, they have 3 levels of complexety. You should be able to complete nearly all level 1 and various level 2 exercises by now (depending on your coding background).

1 - Hello!

Create some Python code that will prompt you to enter your name and age. Print out Hello and then the name followed by how old they'll be next year.

input:
	Jorge
	21
output:
	Hello Jorge!
	Next year you'll be 22.

2 - Odd or Even?

Write some code that checks whether or not a given number is odd or even and prints "Odd" or "Even" accordingly.

input:
	3
output:
	Odd

3 - The string's classic

Assume that everyone has 1 first name and at least 1 last name. Write a python program that given someones name prints it according to the following rules:
Note: you may find the .split method useful.

  • The first name stays as is.
  • Middle names are abbreviated to only the first letter and a dot.
  • The last name stays as is.
input:
	Maria Manuel Saavedra
output:
	Maria M. Saavedra
input:
	José Augusto dos Santos Oliveira
output:
	José A. S. Oliveira

4 - Case converter

Write a function that takes camel cased strings (i.e. ThisIsCamelCased), and converts them to snake case (i.e. this_is_camel_cased).
Modify the function by adding an argument, separator, so it will also convert to kebab case (i.e. this-is-camel-case) as well.

input:
	ThisIsCamelCased
output:
	this_is_camel_cased

5 - Octal

Implement some Python code that converts a given number in base 10 to base 8 (octal) using at least 1 'while loop'.
You can use this method to confirm your results: oct(number)

input:
	9
output:
	10

6 - Anagrams

Write Python code that will print the anagrams (words made from the same letters) of a given word from a given sentence. Be mindful of possible upper-case characters given to you (you should compare all characters as if they were the same case).
Note: you can assume that all given words are separated by a single space character and that words can only contain numbers and letters that are not repeated.

input:
	lVe
	lorem ipsum dolor sit amet consectetur adipiscing elit vestibulum orci veL pellentesque nunc sodales Elv quis felis ac gravida
output:
	veL
	Elv

7 - Sort and comparison functions

a) Instantiate a python list object. Read an integer, n, that represents the number of tuples you're gonna read next (each tuple is comprised of 1 string without whitespaces and 1 float). Each tuple you read, will have to be appended to the list you instantiated.
Print the last element of your list object (note: you may try out the index -1 in your list to get the last element).

input:
	3
	Joao 21.3
	Cucs 912.4
	Tiago 22.6
output:
	Tiago 22.6

b) Now try using the built-in "sorted" function to sort your list in descending order, considering only the floats, before printing the last element of the list. For this, you'll need to define a comparison function that takes a tuples as an argument and returns a value to be used for the sort (in this case, return the float value from the tuple).

input:
	3
	Joao 21.3
	Cucs 912.4
	Tiago 22.6
output:
	Joao 21.3