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:
1
2
|
def position(alphabet):
return "Position of alphabet: {}".format(ord(alphabet) - 96)
|
Option 2:
1
2
3
4
|
from string import ascii_lowercase
def position(char):
return "Position of alphabet: {0}".format(
ascii_lowercase.index(char) + 1)
|
Option 3:
1
2
|
def position(alphabet):
return "Position of alphabet: %s" % ("abcdefghijklmnopqrstuvwxyz".find(alphabet) + 1)
|
Test cases to validate our solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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)
|