Python solution of sorting and searching part