Mnemonic Phrase Validator

Welcome to the Mnemonic Phrase Validator Challenge! This Python challenge is designed to help you practise your coding skills while creating a useful tool for validating mnemonic phrases.

Mnemonic Phrase?

Mnemonic phrases are often used to remember specific sequences or information, such as the order of the planets in the solar system.

The following Mnemonic phrase makes it easier to remember the order of the planets as each word in this sentence starts with the letter corresponding to a planet in the solar system, matching their position from the Sun: from Mercury, closest planet to the Sun to Pluto, furthest planet to the Sun.

My Very Easy Method Just Speeds Up Naming Planets.

Mnemonic Phrase

As you can see this phrase contains all the planets in order: My (Mercury) Very (Venus) Easy (Earth) Method (Mars) Just (Jupiter) Speeds (Saturn) Up (Uranus) Naming (Neptune) Planets (Pluto – dwarf planet)

Python Challenge

For this challenge, you are going to write a Python program based on the Input/Process/Output model:

  1. Input: Accept a phrase from the user.
  2. Process: Define and implement validation rules to check the phrase.
  3. Output: Provide feedback to the user indicating whether the phrase is valid or not.

Validation Checks

To validate a mnemonic phrase we will use the following validation rules:

  • Length: The phrase should be between 10 and 50 characters long.
  • Unique Words: Each word in the phrase should be unique.
  • Alphabetical Characters Only: The phrase should only contain alphabetic characters and spaces.
  • Sequence Matching: Each word must start with the letter corresponding to the sequence we want to learn, in the same order. For example, if remembering the order of the planets, the phrase “My Very Easy Method Just Speeds Up Naming Planets” is valid because each word starts with the first letter of a planet’s name in order.

Let’s see how we can implement these four types of validation checks in Python:

Length CheckUnique WordsAlphabetical Characters OnlySequence Matching
Our first check consists of checking that our mnemonic phrase is long enough but not too long, in other words a valid mnemonic phrase should be between 10 and 100 characters long.

In Python, we can use the len() function to find out the number of characters in a string.

phrase = "My Very Easy Method Just Speeds Up Naming Planets"
if len(phrase)<10 or len(phrase)>100:
      print("Invalid: Phrase length should be between 10 and 100 characters.")
      return False
Our next check consists of checking that the phrase given only include unique words. To implement this check in Python we will first need a list of all the words contained in our phrase. We can create such a list of words using the split() function using the space character to split our phrase into separated words.

phrase = "My Very Easy Method Just Speeds Up Naming Planets"
words = phrase.split()

We can then use the set() function in python which returns a set of unique values from a given list by removing duplicate values. If the resulting set contains as many words as the initial list of words, we can deduct that all the words in the words list must have been unique.

phrase = "My Very Easy Method Just Speeds Up Naming Planets"
# Check for unique words
words = phrase.split()
if len(words) != len(set(words)):
   print("Invalid: Each word should be unique.")
   return False
To check if each word of our phrase only contains alphabetical characters we can use the isalpha() function that returns True if a given string value only contains characters from the alphabet (lowercase or uppercase).

To check the whole sentence, we will first create a list of words as we did in the previous check, using the split() function. We will then iterate through this list of words, to check one word at a time.

phrase = "My Very Easy Method Just Speeds Up Naming Planets"
 # Check for special characters
words = phrase.split()

for i in range(0,len(words)):
   if not words[i].isalpha():
      print("Invalid: Phrase should only contain alphabetic characters and spaces.")
      break #No need to check other words
To check that each word from the phrase starts with the same letter as each word in the given sequence we will loop through our list of words and extract the first characters (at position 0) from the list of words and from the given sequence.

phrase = "My Very Easy Method Just Speeds Up Naming Planets"
sequence = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune","Pluto"]
valid = True
# Check sequence matching
for i in range(0,len(words)):
   if not words[i][0] == sequence[i][0]:
      print("Invalid: Each word must start with the corresponding letter in the sequence.")
      valid=False
      break #No need to check other words
if valid==True:
   print("Valid Sequence Matching!")

Python Code

Your task is to use the techniques mentioned in the four tabs above to complete this Mnemonic Phrase Validator challenge.

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 3.8 / 5. Vote count: 13

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: