[PythonEdu] IP sorting with lambda | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

[PythonEdu] IP sorting with lambda

Another problem-solving code - IP sorting. We have a list of IP addresses parsed from the net and they are stored as strings, like '', '', '', etc. We want to sort them in ascending order, treating them as numbers - from to Sorting them as strings won't do - '192.x.x.x' will come before '23.x.x.x'. We have to be smart and tricky about it :) Take a look at the code here and let me know how it works for you: https://code.sololearn.com/cJqu6Q00Gidt/#py

5th Mar 2017, 8:41 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
2 Answers
+ 6
What it does it sorts the list based on an obtained key. Now, how to calculate the key? 1. split each item into parts (IP chunks) 2. add '00' at the beginning of each of them 3. take last three chars from each of them for further consideration (3-digit figures will be taken as they are, while 2- and 1-digit - with relevant zeros in front of them) 4. join the chunks into 12-char strings, i.e. 192168001001, 023044123073, etc. 5. such a string is ready to act as the sorting key
6th Mar 2017, 8:10 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
Impressive, I don't even understand what is going on in your code
5th Mar 2017, 10:40 PM
Amaras A
Amaras A - avatar