-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2051 from AndrewB50/patch-2
Update Python Program to Sort Words in Alphabetic Order.py
- Loading branch information
Showing
1 changed file
with
31 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,42 @@ | ||
# Program to sort alphabetically the words form a string provided by the user | ||
# Program to sort words alphabetically and put them in a dictionary with corresponding numbered keys | ||
# We are also removing punctuation to ensure the desired output, without importing a library for assistance. | ||
|
||
my_str = "Hello this Is an Example With cased letters" | ||
# Declare base variables | ||
word_Dict = {} | ||
count = 0 | ||
my_str = "Hello this Is an Example With cased letters. Hello, this is a good string" | ||
#Initialize punctuation | ||
punctuations = '''!()-[]{};:'",<>./?@#$%^&*_~''' | ||
|
||
# To take input from the user | ||
#my_str = input("Enter a string: ") | ||
|
||
# remove punctuation from the string and use an empty variable to put the alphabetic characters into | ||
no_punct = "" | ||
for char in my_str: | ||
if char not in punctuations: | ||
no_punct = no_punct + char | ||
|
||
# Make all words in string lowercase. my_str now equals the original string without the punctuation | ||
my_str = no_punct.lower() | ||
|
||
# breakdown the string into a list of words | ||
words = my_str.split() | ||
|
||
# sort the list | ||
# sort the list and remove duplicate words | ||
words.sort() | ||
|
||
# display the sorted words | ||
|
||
print("The sorted words are:") | ||
new_Word_List = [] | ||
for word in words: | ||
print(word) | ||
if word not in new_Word_List: | ||
new_Word_List.append(word) | ||
else: | ||
continue | ||
|
||
# insert sorted words into dictionary with key | ||
|
||
for word in new_Word_List: | ||
count+=1 | ||
word_Dict[count] = word | ||
|
||
print(word_Dict) |