Skip to content

Instantly share code, notes, and snippets.

@plant99
Created November 27, 2019 16:40
Show Gist options
  • Save plant99/6eba4291a5bdfb9e1b953514520a7f2e to your computer and use it in GitHub Desktop.
Save plant99/6eba4291a5bdfb9e1b953514520a7f2e to your computer and use it in GitHub Desktop.
from functools import cmp_to_key
def sort_cmp(elem1, elem2):
if len(elem1) > len(elem2):
return -1
elif len(elem1) < len(elem2):
return 1
else:
if elem1 > elem2:
return -1
else:
return 1
def immutable_list(string_list, add_list, remove_list):
for elem in add_list:
if elem not in string_list:
string_list.append(elem)
for elem in remove_list:
if elem in string_list:
# can safely use this as elements are unique in list
string_list.remove(elem)
# sort according to follwing rules
"""
List shall be ordered as follows
--- Most character count to least character count
--- In the event of a tie, reverse alphabetical
"""
string_list = sorted(string_list, key=cmp_to_key(sort_cmp))
return string_list
print(immutable_list(["one","two", "three"], ["one","two","five","six"], ["two","five"]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment