python实践:让所有奇数都在偶数前面,而且奇数升序排列,偶数降序排序

"""
给定一个任意长度数组,实现一个函数
让所有奇数都在偶数前面,而且奇数升序排列,偶数降序排序,如字符串'1982376455',变成'1355798642'
"""
class Solution():
    def SortNum(self,num_list):
        """
        :param num_list: 传入数组
        :return: rlist
        """
        num_odd=[]
        num_even=[]
        for num in num_list:
            if int(num)%2 == 0:
                num_even.append(num)
            else:
                num_odd.append(num)
        num_even=sorted(num_even)
        num_odd=sorted(num_odd)
        num_odd.extend(num_even)
        return ''.join(num_odd)

if __name__=='__main__':
    num_list=list('1982376455')
    solution=Solution()
    num_odd=solution.SortNum(num_list)
    print(num_odd)

 

你可能感兴趣的:(PYTHON编程实践)