Exercises

Validation Exercises

Has Vowels

Create a function has_vowel, that accepts a string and returns True if the string contains a vowel (a, e, i, o, or u) returns False otherwise.

Your function should work like this:

>>> has_vowel("rhythm")
False
>>> has_vowel("exit")
True

Hint

Match objects are always "truthy" and None is always "falsey". Truthy meas when you convert something to a boolean, it'll be True.

Is Integer

Create a function is_integer that accepts a string and returns True if the string represents an integer.

By our definition, an integer:

  • Consists of 1 or more digits
  • May optionally begin with -
  • Does not contain any other non-digit characters.

Your function should work like this:

>>> is_integer("")
False
>>> is_integer(" 5")
False
>>> is_integer("5000")
True
>>> is_integer("-999")
True
>>> is_integer("+999")
False
>>> is_integer("00")
True
>>> is_integer("0.0")
False

Is Fraction

Create a function is_fraction that accepts a string and returns True if the string represents a fraction.

By our definition a fraction consists of:

  1. An optional - character
  2. Followed by 1 or more digits
  3. Followed by a /
  4. Followed by 1 or more digits, at least one of which is non-zero (the denominator cannot be the number 0).

Your function should work like this:

>>> is_fraction("")
False
>>> is_fraction("5000")
False
>>> is_fraction("-999/1")
True
>>> is_fraction("+999/1")
False
>>> is_fraction("00/1")
True
>>> is_fraction("/5")
False
>>> is_fraction("5/0")
False
>>> is_fraction("5/010")
True
>>> is_fraction("5/105")
True
>>> is_fraction("5 / 1")
False

Valid Time

Create a function that returns True if given a valid 24 hour time in the format HH:MM.

Example usage:

>>> is_valid_time("00:19")
True
>>> is_valid_time("01:37")
True
>>> is_valid_time("10:60")
False
>>> is_valid_time("23:59")
True
>>> is_valid_time("24:00")
False

Search Exercises

Each of these exercises involves searching in a dictionary.

You can download the dictionary.txt file here.

You can open and read from the dictionary file like this:

with open('dictionary.txt') as dict_file:
    dictionary = dict_file.read()

Tetravocalic

Find all lowercase words (no proper nouns) that include four consecutive vowels.

Hexadecimal Words

Find every word that consists solely of the letters A, B, C, D, E, and F.

Examples: decaf, bead, cab

Hexaconsonantal

Find at least one word with 6 consecutive consonants. For this problem treat y as a vowel.

Crossword Helper

Make a function possible_words that accepts a partial word with underscores representing missing letters and returns a list of all possible matches.

Use your crossword helper function to solve the following:

  1. water tank: CIS____
  2. pastry: ___TE
  3. temporary: __A_S_E__

Repeat Letter

Find every word with 5 N's.