Apr-22-2020, 10:56 AM
Hello.
In a mixed string(digits and English alphabet) sort only the digits.
Example:
"3b2c1" => "1b2c3"
"7Mkx1Intv2n5D" => "1Mkx2Intv5n7D"
I came up with:
The other idea I had, was to extract all the digits into a list, sort it and go over the string, replacing the digits with values from sorted list.
In a mixed string(digits and English alphabet) sort only the digits.
Example:
"3b2c1" => "1b2c3"
"7Mkx1Intv2n5D" => "1Mkx2Intv5n7D"
I came up with:
s = "7Mkx1Intv2n5D" arr = list(s) sorted(arr, key=lambda val:'0123456789'.index(val) if val.isdigit() else ...)I can't think of a working else condition.
The other idea I had, was to extract all the digits into a list, sort it and go over the string, replacing the digits with values from sorted list.