Software Engineering

The best way to Discover an merchandise’s Alphabet Place in Python

The best way to Discover an merchandise’s Alphabet Place in Python
Written by admin


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

Choice 1:

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

Choice 2:

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

Choice 3:

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

Take a look at instances to validate our answer

import take a look at
from answer import place

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

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

About the author

admin

Leave a Comment