1

Actual question:

I have a string s and I need to sort it using below 3 criteria. I already have the solution but I need to understand a part of that solution. How is it working?

  1. All sorted lowercase letters are ahead of uppercase letters.
  2. All sorted uppercase letters are ahead of digits.
  3. All sorted odd digits are ahead of sorted even digits.
s="Sa27"

print(*sorted(s, key = lambda x: ( x.isdigit() and int(x)%2==0
, x.isdigit(),x.isupper(),x.islower(),x)), sep = '')

I am getting the expected out from the above code: aS72

The stuff inside the lambda gives tuple of True, false values shown below. I want to know how these tuples are actually determining the order/priority of the element.

(False, False, True, False, 'S')
(False, False, False, True, 'a')
(True, True, False, False, '2')
(False, True, False, False, '7')
1
  • 1
    Tuples are sorted lexicographically, i.e. comparing first elements first, second elements second, etc. (there is a caveat for unequal length, but it does not apply here). Also, False < True holds. Commented Aug 14, 2019 at 0:47

1 Answer 1

2

I've laid out the differing sections with some printing commentary like this:

s="Sa27"

for x in s:
    print( 'first group by if this is an even digit\t' + str(x.isdigit() and int(x)%2==0) + '\n',     
           'then group by if this is a digit or not\t' + str(x.isdigit()) + '\n' ,
           'next group by if this is uppercased\t' + str(x.isupper()) +'\n',
           'and last group by if this is lowercased\t' + str(x.islower()) +'\n',
           'print the charachter\t\t' + str(x)+'\n\n')

print(*sorted(s, key = lambda x: ( x.isdigit() and int(x)%2==0
, x.isdigit(),x.isupper(),x.islower(),x)), sep = '')

When it's returning that Tuple, you can look at this as sorting it at each level in turn. A False value goes ahead of the True value something like this:

After the first round, "Sa7" were all not even digits, ie False, and while "2" is True. So we sort:

"Sa7" into group_0
"2" into group_1

Next we'll sort on it it's a digit. In group_0 "Sa" are False, while "7" is True. IN group_1 "2" is also True. Let's sort:

"Sa" into group_0_0
"7" into group_0_1
"2" into group_1_1 to keep with the convention.

Next we do the same with upper case, so we get "a" False, "S" True, "7" and "2" False so we'll now have:

"a" into group_0_0_0
"S" into group_0_0_1
"7" into group_0_1_0
"2" into group_1_1_0

For this example string you could stop now.

However, last from your example code, we do the same with lower case, so we get "a" True, "S" False, "7" and "2" False and end up with:

"a" into group_0_0_0_1
"S" into group_0_0_1_0
"7" into group_0_1_0_0
"2" into group_1_1_0_0

You'll notice maps out to your True/False tuples. So you're just sorting on each "row" that you've got there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.