Python Functions Exercises

Exercise 1

Functions Challenge 1 – Kilometers to miles converter

Write a function called km_to_miles that takes kilometers as a parameter, converts it into miles and returns the result.

 

 

 

2

Functions Challenge 2 – Divisible by 11 checker

Write a function called is_divisable_by_11 that takes an integer as an parameter and returns whether it is divisible by 11 or not.

 

 

 

3

Functions Challenge 3 – Highest number

Write a function called get_highest that takes 2 numbers as parameters and returns the highest of the 2 numbers.

 

 

 

4

Functions Challenge 4 – Hexagon area calculator

Write a function called hexagon_area that takes the length of a side of a regular hexagon as a parameter and returns the area of the hexagon.

Hint: Video for area of a hexagon calculation

 

5

Functions Challenge 5 – Sleeps until Christmas

Write a function called sleeps_until_xmas that calculates how many days it is until Christmas and returns the number of sleeps left until the big day.

 

You may want to use the Datetime module to help you.

 

 

6

Functions Challenge 6 – Is a palindrome?

Create a function called is_palindrone that checks to see if a given string is a palindrome or not.

 

 

You might want to take a look at the String methods video for some help.

 

 

7

Functions Challenge 7 – Fuel cost calculator

Write a function called fuel_cost that takes a distance as a required argument, mpg (default 50 mpg) and fuel cost (default $1 a litre) as optional arguments. The function should return the cost in dollars.

8

Functions Challenge 8 – Most common character

Create a function called most_common_char that takes a string as a argument and the returns the most common character in that string.

 

 

 

 

9

Functions Challenge 9 – Is a prime number?

Write a function called is_prime that takes an integer as an argument and checks if it is a prime number or not, returning a Boolean.

 

 

 

10

Functions Challenge 10- All Prime numbers

Write a function called primes that takes an integer value as an argument and returns a list of all prime numbers up to that number.

Hint: You might want to use modulo

 

 

11

Functions Challenge 11 – Highest Common Factor

Write a function called hcf that takes 2 integer values and returns the highest common factor of the numbers.

 

12

Functions Challenge 11 – Valid Date

Write a function called is_valid_date that takes a date in string format DD/MM/YYYY and checks to see if it is a valid date and in the correct format.

Your function should take account of leap years.

 

You may want to look at the date conversion video for help.

 

13

Functions Challenge 13 – Postcode Checker

Write a function called is_valid_postcode that takes a post code as an argument and returns whether it is a valid post code. 

Valid format:

[2 letters]  [1 or 2 Digits] [Space] [1 Digit] [2 letters]

(This is a slight simplification from the real UK postcode system to make the challenge a little easier – otherwise you would need to use a pretty complicate regular expression)

 

Hint: You might want to use on or more of the follow:

 

14

Functions Challenge 14 – Check Valid Email Address

Write a function called is_valid_email  that takes an email address as an argument and returns True/False depending on whether it is a valid email address.

Check rules:

  • Must contain at least 1 character before the at symbol
  • Must contain an @ symbol
  • Must have at-least 1 character after the @ symbol and before the period(.)
  • Must contain at least 1 character after the last period(.).
  • Maximum 256 characters
  • Must start with a letter or a number

15

Functions Challenge 15 – Caesar Cipher Encryptor / Decryptor

Create a function called encrypt that takes some text(String) and a Shift(Integer)and then encrypts the text using the Caesar Cipher algorithm, returning the encrypted text.

Create a second function to decrypt an encrypted string, using the same input parameters and returning the decrypted text.

 

16

Functions Challenge 16 – Find all palindromes

Create a function called get_palindrones that is given a string and returns a list of all the palindromes in the string.