-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting alphabetically
More file actions
47 lines (40 loc) · 1.12 KB
/
Sorting alphabetically
File metadata and controls
47 lines (40 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Sorting Alphabetically
# Benneth Chibueze
"""
sort an input text by alphabetical order
first of all map the letters to numbers on a dictionary
then implement insertion sort to sort it
"""
Alphabets = {
"a":1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6, 'g':7, 'h':8, 'i':9, 'j':10,
'k':11, 'l':12, 'm':13, 'n':14, 'o':15, 'p':16, 'q':17, 'r':18, 's':19,
't':20, 'u':21, 'v':22, 'w':23, 'x':24, 'y':25, 'z':26
}
UserInput = input("input a sentence").lower().split(" ")
WordList = []
for word in UserInput:
WordArray = [Alphabets[letter] for letter in word]
WordList.append(WordArray)
def InsertSort(arr):
N = len(arr)
for i in range(1,N):
current = arr[i]
left = i - 1
while arr[left] > current and left >= 0:
arr[left + 1] = arr[left]
left -= 1
arr[left+1] = current
def num_to_word(arr):
word = ""
for num in arr:
for key, value in Alphabets.items():
if num == value:
word += key
return word
output = ""
for array in WordList:
InsertSort(array)
# print(array)
output += num_to_word(array)
output += " "
print(f"The Inverted sentence is: {output}")