0

From a CTF challenge, a easy pwn(elf). I could probably have done it in better way, but anyway I ended up with the flag in a array looking like this.

['4:a', '27:a', '33:a', '39:b', '5:c', '31:c', '44:c', '45:c', '18:d', '37:d', '2:e', '21:e', '25:e', '29:e', '36:e', '46:e', '35:g', '3:h','32:h', '17:i', '6:k', '10:l', '24:l', '9:n', '34:n', '22:o', '16:p', '20:p', '23:p', '28:r', '13:s', '42:s', '47:s', '48:s', '14:t', '15:u','43:u', '1:w', '11:y', '40:y', '8:O', '7:{', '49:}', '12:_', '19:_', '26:_', '30:_', '38:_', '41:_','0:$']

Is it possible to sort this array in easy way?

3
  • 1
    It's easy to sort using Python sort function. What criteria do you want to use in sorting? If you just want each item treated as a string and sorted in alphabetical order than sorted(your-array) will do that. Also: your.array.sort() to sort in-place. Commented Dec 26, 2019 at 11:52
  • The first char in the flag is 0:$ the second is 1:w, so I want to sort it by the numbers Commented Dec 26, 2019 at 11:57
  • @r4113 You may accept the answer, if it solves your problem. If not, feel free to ask any question. Commented Jun 20, 2020 at 11:49

2 Answers 2

4
flagArray = ['4:a', '27:a', '33:a', '39:b', '5:c', '31:c', '44:c', '45:c', '18:d', '37:d', '2:e', '21:e', '25:e', '29:e', '36:e', '46:e', '35:g', '3:h','32:h', '17:i', '6:k', '10:l', '24:l', '9:n', '34:n', '22:o', '16:p', '20:p', '23:p', '28:r', '13:s', '42:s', '47:s', '48:s', '14:t', '15:u','43:u', '1:w', '11:y', '40:y', '8:O', '7:{', '49:}', '12:_', '19:_', '26:_', '30:_', '38:_', '41:_','0:$']```

def getNumber(x):
  return int(x.split(":")[0])

sorted(flagArray, key = getNumber)

>>>
['0:$',
 '1:w',
 '2:e',
 '3:h',
 '4:a',
 '5:c',
 '6:k',
 '7:{',
 '8:O',
 '9:n',
 '10:l',
 '11:y',
 '12:_',
 '13:s',
 '14:t',
 '15:u',
 '16:p',
 '17:i',
 '18:d',
 '19:_',
 '20:p',
 '21:e',
 '22:o',
 '23:p',
 '24:l',
 '25:e',
 '26:_',
 '27:a',
 '28:r',
 '29:e',
 '30:_',
 '31:c',
 '32:h',
 '33:a',
 '34:n',
 '35:g',
 '36:e',
 '37:d',
 '38:_',
 '39:b',
 '40:y',
 '41:_',
 '42:s',
 '43:u',
 '44:c',
 '45:c',
 '46:e',
 '47:s',
 '48:s',
 '49:}']

The array will be sorted by the number part of each element

A oneliner approach would be to use a lambda function

sorted(flagArray, key = lambda x : int(x.split(":")[0]))
Sign up to request clarification or add additional context in comments.

Comments

2

One liner solution

You can use inbuilt python functions sort or sorted along with lambda function.

my_list = ['4:a', '27:a', '33:a', '39:b', '5:c', '31:c', '44:c', '45:c', '18:d', '37:d', '2:e', '21:e', '25:e', '29:e', '36:e', '46:e', '35:g', '3:h','32:h', '17:i', '6:k', '10:l', '24:l', '9:n', '34:n', '22:o', '16:p', '20:p', '23:p', '28:r', '13:s', '42:s', '47:s', '48:s', '14:t', '15:u','43:u', '1:w', '11:y', '40:y', '8:O', '7:{', '49:}', '12:_', '19:_', '26:_', '30:_', '38:_', '41:_','0:$']

Inplace Sort

my_list.sort(key=lambda k: k.split(':')[0])

Getting sorted list as a copy

sorted_list = sorted(a, key=lambda k: k.split(':')[0])

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.