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.

Exercises - Functions (2)

Here are even more exercises for functions!

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

Exercise 1

Write a function that takes a variable number of arguments and prints them.

Exercise 2

Write a function that prints the names and values of keyword arguments passed to it.

Exercise 3

Write a function that returns True if a number is prime and False otherwise.

Exercise 4

An anagram is a word formed by rearranging the letters of a different word. For example, ‘silent’ and ‘listen’ are anagrams. Write a function that takes two strings and returns True if the strings are anagrams and False otherwise.”

You may use the built-in functions len() and sorted() for this.

Exercise 5

Write a function that takes a positive integer $n$ as a parameter and computes \(\frac{1}{2}+\frac{2}{3}+\frac{3}{4}+...+\frac{n}{n+1}\).

Exercise 6

Write a function that takes a positive integer n as a parameter and computes the following:

  • f(n) = n + f(n-1) + 1 when n>0
  • f(0) = 1

Exercise 7

The Fibonacci sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … The first numbers are 0 and 1, and then each number is the sum of the two preceding ones.

Write a function using a while loop that displays the first n terms of the Fibonacci sequence. Try different values for n to check that your code behaves as expected.

Then write a recursive function to compute the sequence.

Exercise 8

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, \(371\) is an Armstrong number since \(3^3 + 7^3 + 1^3 = 371\). Write a function that takes two integers as parameters and prints all the Armstrong numbers within this interval.

Exercise 9

Given the following available notes, notes = [500, 200, 100, 50, 20, 10], write a function that returns the number of notes needed to match a given amount. Your program should use as many of the high notes as possible. You can assume that the list is sorted in descending order by the value of the note.

For example, for amount 880, your function should return 6 (because you will use all notes), and for 400 it should return 2 (because you will use 2 notes of 200).

Exercise 10

Write a function for applying the Caesar cipher on any given string. The Caesar cipher is a type of substitution cipher in which each letter in the text is replaced by a letter some fixed number of positions down the alphabet.

For example, for a left shift of 3, D would be replaced by A, E by B, etc. Since there are 26 chars in the English language, a left shift of 3 places is equivalent to a right shift of 23.

A left shift of x should have the value -x passed to the function whereas a right shift of x should be passed as x.

The output for both THE quick Brown fox JuMpS Over the lazy dog with shift -3 and for the same string with shift 23 should be QEB nrfzh Yoltk clu GrJmP Lsbo qeb ixwv ald.

You can accesss the lowercase letters and the upper case letters using the following code:

import string
lowercase_letters = string.ascii_lowercase
uppercase_letters = string.ascii_uppercase