The challenge
When provided with a letter, return its position in the alphabet.
Input :: “a”
Ouput :: “Position of alphabet: 1”
The solution in Python code
Option 1:
def position(alphabet):
return "Position of alphabet: {}".format(ord(alphabet) - 96)
Option 2:
from string import ascii_lowercase
def position(char):
return "Position of alphabet: {0}".format(
ascii_lowercase.index(char) + 1)
Option 3:
def position(alphabet):
return "Position of alphabet: %s" % ("abcdefghijklmnopqrstuvwxyz".find(alphabet) + 1)
Test cases to validate our solution
import test
from solution import position
@test.describe("Fixed Tests")
def fixed_tests():
@test.it('Basic Test Cases')
def basic_test_cases():
tests = [
# [input, expected]
["a", "Position of alphabet: 1"],
["z", "Position of alphabet: 26"],
["e", "Position of alphabet: 5"],
]
for inp, exp in tests:
test.assert_equals(position(inp), exp)