编写一个能将给定非负整数列表中的数字排列成最大数字的函数。例如,给定[50,2,1,9],最大数字为95021
运行环境: python2.7.6
运行: ./test.py [50,2,1,9]
test.py:
#! /usr/bin/python
# -*- coding:utf-8 -*-
'''
编写一个能将给定非负整数列表中的数字排列成最大数字的函数。例如,给定[50,2,1,9],最大数字为95021
test.py
'''
import sys
def totalbit(value):
if value > 0 :
#print value
value = value /10
if value == 0:
return 1;
return totalbit(value)+1
elif value == 0 :
return 1
def hbit(value):
if value > 0 :
#print value
valuetemp=value;
value = value /10
if value == 0:
return valuetemp
else:
return hbit(value)
def maxvalue(data):
mv=0;
listtemp=[]
listm=[]
print("lista = {0}".format(data));
for a in data[1:-1].split(","):
print a
listtemp.append(int(a))
print "---------------------"
for b in listtemp:
print "================"
print b
print "\n"
print totalbit(b)
print hbit(b)
listm.append(hbit(b));
print "************************"
for i in range(len(listtemp)):
print i
for j in range(i+1,len(listtemp)):
if listm[i] > listm[j]:
temp = listm[i]
listm[i] = listm[j]
listm[j]=temp
temp2 = listtemp[i]
listtemp[i]=listtemp[j]
listtemp[j]=temp2
print "************************"
for i in range(len(listtemp)):
print listtemp[i],listm[i]
if mv == 0:
mv = listtemp[i]
else:
mv = 10**(totalbit(mv)) * listtemp[i] + mv
return mv;
if __name__ == '__main__':
print("argc = {0}".format(len(sys.argv)))
print("argv[1] ={0}".format(sys.argv[1]))
mv = maxvalue(sys.argv[1])
print("{0} ----> max = {1}".format(sys.argv[1],mv));
运行环境:3.4.3
运行命令: python3 test.py [50,2,1,9]
test.py
#! /usr/bin/python3
# -*- coding:utf-8 -*-
"""
#编写一个能将给定非负整数列表中的数字排列成最大数字的函数。例如,给定[50,2,1,9],最大数字为95021
#test.py
#python 3.4.3
"""
import sys
def totalbit(value):
if value > 0 :
#print value
value = value //10
if value == 0:
return 1;
return totalbit(value)+1
elif value == 0 :
return 1
def hbit(value):
if value > 0 :
#print value
valuetemp=value;
value = value //10
if value == 0:
return valuetemp
else:
return hbit(value)
def maxvalue(data):
mv=0;
listtemp=[]
listm=[]
print("lista = {0}".format(data));
for a in data[1:-1].split(","):
print (a)
listtemp.append(int(a))
print("---------------------")
for b in listtemp:
print("================")
print(b)
print("{0},{1}".format(totalbit(b),hbit(b)))
listm.append(hbit(b));
print("************************")
for i in range(len(listtemp)):
print (i)
for j in range(i+1,len(listtemp)):
if listm[i] > listm[j]:
temp = listm[i]
listm[i] = listm[j]
listm[j]=temp
temp2 = listtemp[i]
listtemp[i]=listtemp[j]
listtemp[j]=temp2
print("************************")
for i in range(len(listtemp)):
print (listtemp[i],listm[i])
if mv == 0:
mv = listtemp[i]
else:
mv = 10**(totalbit(mv)) * listtemp[i] + mv
return mv;
if __name__ == '__main__':
print("argc = {0}".format(len(sys.argv)))
print("argv[1] ={0}".format(sys.argv[1]))
mv = maxvalue(sys.argv[1])
print("{0} ----> max = {1}".format(sys.argv[1],mv));