HomeSoftware EngineeringTips on how to Discover an merchandise’s Alphabet Place in Python

Tips on how to Discover an merchandise’s Alphabet Place in Python


The problem

When supplied with a letter, return its place within the alphabet.

Enter :: “a”

Ouput :: “Place of alphabet: 1”

The answer in Python code

Possibility 1:

def place(alphabet):
    return "Place of alphabet: {}".format(ord(alphabet) - 96)

Possibility 2:

from string import ascii_lowercase
def place(char):
    return "Place of alphabet: {0}".format(
        ascii_lowercase.index(char) + 1)

Possibility 3:

def place(alphabet):
    return "Place of alphabet: %s" % ("abcdefghijklmnopqrstuvwxyz".discover(alphabet) + 1)

Check instances to validate our resolution

import take a look at
from resolution import place

@take a look at.describe("Fastened Assessments")
def fixed_tests():
    @take a look at.it('Primary Check Instances')
    def basic_test_cases():

        checks = [
            # [input, expected]
            ["a", "Position of alphabet: 1"],
            ["z", "Position of alphabet: 26"],
            ["e", "Position of alphabet: 5"],
        ]
        
        for inp, exp in checks:
            take a look at.assert_equals(place(inp), exp)
RELATED ARTICLES

Most Popular

Recent Comments