Skip to content

Instantly share code, notes, and snippets.

@roman-on
Created May 17, 2020 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roman-on/42f58ee8500459e1582ef2f119d16c82 to your computer and use it in GitHub Desktop.
Save roman-on/42f58ee8500459e1582ef2f119d16c82 to your computer and use it in GitHub Desktop.
"""
The function accepts as a string list parameter, so that each string is one word (no spaces).
The function returns a list of the same strings passed, but as follows:
The list is divided into lists so that each "internal" list is composed of words that are one another's anagrams
(anagram: a word that consists of interweaving letters of another word, i.e. the same letters but in a different order).
Instructions:
Make sure the strings and lists are arranged in the order that the strings appear in the original list.
Examples of running the sort_anagrams function
list_of_words = ['deltas', 'retainers', 'desalt', 'pants', 'slated', 'generating', 'ternaries', 'smelters', 'termless', 'salted', 'staled', 'greatening', 'lasted', 'resmelts']
sort_anagrams(list_of_words)
[['deltas', 'desalt', 'slated', 'salted', 'staled', 'lasted'], ['retainers', 'ternaries'], ['pants'], ['generating', 'greatening'], ['smelters', 'termless', 'resmelts']]
"""
# The list is divided into lists so that each "internal" list is composed of words that are one anthers anagrams
def sort_anagrams(list_of_strings):
z = len(list_of_strings) # Calculating the length of the list that i get
i = 0 # Giving first start to the while loop
final_result = [] # Making a new list
while i < z: # First "While" loop
result = [] # Making a new list and MORE IMPORTANT this list is reseting the list and deviding my list into other lists inside [[],[],[]] if it will be in some other place it will ruin all the main point of this devision
for num in range(len(list_of_strings)): # Second "for" loop inside "While" loop counting the numbers of the length of my list in my exmp it's from 0-13
if sorted(list_of_strings[i]) == sorted(list_of_strings[num]): # Comparing if the "sorted word[1]" in the giving list ([i] is the variable that runs in the first "while" loop) equal to sorted word[num] in the giving list ([num] is the variable that runs in the second "for" loop) and checking if the word from 1 index equal to all the string [from 1,2,3...to 13] sorted word and than starting over and over checking if 2 word has same words from 0 index to 13
if not (list_of_strings[num] in result): # If the word[1,2,3....] in the result already dont add it
result.append(list_of_strings[num]) # Adding strings into the new list
if not (result in final_result): # If the result list in final result list already dont add the word
final_result.append(result) # Adding the result to final result
i += 1 # Continuous of the first "While loop" adding 1 to continue the counting
return final_result
def main():
<call the function>
if __name__ == "__main__":
main()
"""
list_of_words = ['deltas', 'retainers', 'desalt', 'pants', 'slated', 'generating', 'ternaries', 'smelters', 'termless', 'salted', 'staled', 'greatening', 'lasted', 'resmelts']
INPUT:
sort_anagrams(list_of_words)
OUTPUT:
[['deltas', 'desalt', 'slated', 'salted', 'staled', 'lasted'], ['retainers', 'ternaries'], ['pants'], ['generating', 'greatening'], ['smelters', 'termless', 'resmelts']]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment