So, another challenge from my professor. I've finished it, but wanted to get some feedback, and see how other people might work this problem.
Assume you have a string, such as "Hello 34215 World 5620 From 384 Bloomington"
Use list comprehension to separate the words from the numbers, and the numbers from the words. Then order the numbers from smallest to largest. Print out these words and numbers separated by a "|". The output for the example given above would be:
>>>
Hello World From Bloomington | 012233445568
The challenge is to write the code in two lines, one of the lines being the definition of the string.
This is what I wrote:
string = "Hello 34215 World 5620 From 384 Bloomington"
print str(" | ".join(["".join([letter for letter in string if letter.isalpha() or letter.isspace()]), "".join(sorted([num for num in string if num.isdigit()]))])).replace(" ", " ")
It works! The output is identical to the output displayed. But, I'd love to hear feedback about how this is done, or if there are any pieces of redundant code, etc. I don't use lambda, map, set, etc. nor do I import any libraries, but if you find that there is a way to do it with any of those pieces, feel free to share!
"".join(x for x in s if not x.isdigit()) +" | "+ "".join(sorted(x for x in s if x.isdigit() ))
\$\endgroup\$" ".join(x for x in s.split() if not x.isdigit()) +" | "+ "".join(sorted(x for x in s if x.isdigit() ))
\$\endgroup\$Hello World From Bloomington | 384 5620 34215
. \$\endgroup\$