Tuesday, April 23, 2024
HomeSoftware EngineeringDiscover an merchandise’s Alphabet Place in Python

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

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 check
from answer import place

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

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments