Last active
April 1, 2020 11:33
-
-
Save xquery/e0ecef4e8aa313e0195140a3659986f7 to your computer and use it in GitHub Desktop.
Sort a list of the strings "a", "bcd", "ef", and "ghij" in descending order of length.
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
""" | |
How do you sort a list of the strings "a", "bcd", "ef", and "ghij" in descending order of length? | |
(https://twitter.com/paulg/status/1244921240227282944) | |
""" | |
def sort_desc_by_string_length(data): | |
''' sorts an array by string length and return in descending order ''' | |
# use python 'sorted' builtin to sort by string length and return in reverse order | |
# (https://docs.python.org/3.8/library/functions.html?highlight=sorted#sorted) | |
return sorted(data, key=len, reverse=True) | |
data = [ "a", "bcd", "ef", "ghij"] | |
expected = ['ghij', 'bcd', 'ef', 'a'] | |
result = sort_desc_by_string_length(data) | |
# should pass if correct | |
assert( expected==result) | |
print("success") | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
test out via python repl - https://repl.it/repls/ImpeccableCyanPatterns